Home Drupal 8: Extend user class
Reply: 1

Drupal 8: Extend user class

Maverick
1#
Maverick Published in 2018-02-12 13:00:03Z

Is there any way to extend User class form a custom module? I need to create a custom user class (e.g. Mymodule\User) that will use/override the existing User class methods or add some other custom methods.

Thank you

Reece Marsland
2#
Reece Marsland Reply to 2018-02-15 13:54:52Z

You need to implement hook_entity_type_build().

Create a new class in your module that extends \Drupal\user\Entity\User.

use Drupal\user\Entity\User as BaseUser;

class MyUser extends BaseUser {
}

Then replace existing class with yours

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * @param EntityTypeInterface[] $entity_types
 */
function my_module_entity_type_build(&$entity_types) {
  if (isset($entity_types['user'])) {
    $entity_types['user']->setClass('Drupal\my_module\Entity\MyUser');
  }
}

Make sure you rebuild your caches.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300628 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO