Thursday 19 July 2018

Programmatically User load and User load Multiple in drupal 8.

In drupal 8 User::load, we have to use library and calling name spaceuse Drupal\user\Entity\User;

So same as node::load, we can use User::load($userid);
or
we can direct call \Drupal\user\Entity\User::load($userid);

For get the user detail fields by User::load($userid) syntax is..

Example: This example is for single user load.
                $user_detail = User::load($userid);       
                                    OR
                $user_detail = \Drupal\user\Entity\User::load($userid);
           
                $user_id= $user_detail->get('uid')->value;
                $user_roles = $user->getRoles();
                $user_status = $user_detail->isActive();
                $user_name = $user_detail->getUsername();
                $user_body = $user_detail->get('body')->value;               
                $user_email = $user_detail->get('mail')->value;
                $user_created_time = $user_detail->created->value;
                $user_field = $user_detail->get('field_machine_name')->value;  

Example: This example is for multiple user load.
First you have to use user library and calling name space
use  \Drupal\user\Entity\User;

                $users = User::loadMultiple($ids);
                foreach($users as $u) {
                   $users_mailid= $u->getEmail();
                   $user_id= $u->get('uid')->value;
                   $user_name = $u->getUsername();
                   $field_value = $u->get('field_machine_name')->value;
                 } 

Note: If have any suggestions or issue regarding 'Programmatically User load and User load Multiple in drupal 8.' then you can ask by comments.

3 comments:

  1. Hii, Can you please tell how to get node field values in array in theme file ?

    ReplyDelete
  2. Hi Abhishek,

    We have an article regarding your issue, please check the link.
    https://drupalwebsolutions.blogspot.com/2018/08/create-custom-variable-in-drupal-8-and.html

    ReplyDelete
  3. Hi,
    Can you please explain how to get image uri from paragraph?

    ReplyDelete

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...