If you have created a Drupal site you have probably worked with all the default entities of Drupal!
Examples of entities are nodes, user, terms and a lot more.

Entities in Drupal are treated as objects / models.

You can identify an entity if it extends the following classes:
    Drupal \ Core \ Entity \ EditorialContentEntityBase; 
    Drupal \ Core \ Entity \ ContentEntityBase;

You can also tell if its an entity too by the file structure.
img

 

 

 

 

 

 

 

 

Now once we identify an entity, here are some usefull methods we can use.
In the examples below we will be using the User entity. Note that this methods are available to every entity in Drupal like node etc ..

In this example we want to load an User entity using its ID

    $ id = 1;
    $ user = \ Drupal \ user \ Entity \ User :: load ($ id); // Load user with ID 1

Now that we loaded our user entity and assigned it to variable "$ user", we can get the value of any field the user entity has.
In this case we want to get the user's birth date.

    $ bday = $ user-> field_birthdate-> value;
    echo $ bday;

We can also load multiple entities using entity query which is a Drupal function.
In this example, we want to load all users which has a birthdate of 09/25/2020

    $ query = \ Drupal :: entityQuery ('user')-> condition ('field_birthdate', '09/25/2020 ');
    $ uids = $ query-> execute ();

    // Now you can use loadMultiple method to load all users with birthday 09/25/2020!
    $users = \Drupal\user\Entity\User::loadMultiple($uids);

Now since we know how to load a user and get its value, its time for us to update a field value!
In this example we will be updating a user's name

    // We load the user we want to update
    $user = \Drupal\user\Entity\User::load(1);

    // We use the machine name of the field we want to update
    $user->set('field_username', 'ILoveDrupal');
    $user->save();

How about if we want to create a user entity programatically!
    
    $ User = \ Drupal \ user \ Entity \ User :: create ([
        'preferred_langcode' =>'ja',
        'preferred_admin_langcode' =>'ja',
        'name' = >'Jane',
        'mail' =>'drupal_lover@acret-ph.com',
        'field_user_points' => '999',
    ]);
    $ user-> save ();

Note that fields may vary and values ​​will depend on what kind of field you have added to the entity.

Now we delete a User!
    
    // Delete user with ID 1
    $ user = \ Drupal \ user \ Entity \ User :: load (1);
    $ user-> delete ();