You are here

class LoadUserByNameOrMail in Acquia Content Hub 8.2

Class LoadUserByNameOrMail.

Loads a local user entity by name/mail.

@package Drupal\acquia_contenthub\EventSubscriber\LoadUserByNameOrMail

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\LoadLocalEntity\LoadUserByNameOrMail implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LoadUserByNameOrMail

1 string reference to 'LoadUserByNameOrMail'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses LoadUserByNameOrMail
acquia_contenthub.load_local_entity.by_username_or_mail in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\LoadLocalEntity\LoadUserByNameOrMail

File

src/EventSubscriber/LoadLocalEntity/LoadUserByNameOrMail.php, line 16

Namespace

Drupal\acquia_contenthub\EventSubscriber\LoadLocalEntity
View source
class LoadUserByNameOrMail implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::LOAD_LOCAL_ENTITY][] = [
      'onLoadLocalEntity',
      5,
    ];
    return $events;
  }

  /**
   * Loads local users for editing by username/mail or just mail.
   *
   * When no user of the same uuid is locally found, this method will attempt
   * to identify local existing users as candidates for matching to the
   * importing user. User are first checked by username/email match and failing
   * that will revert to just email.
   *
   * @param \Drupal\acquia_contenthub\Event\LoadLocalEntityEvent $event
   *   The local entity loading event.
   *
   * @throws \Exception
   */
  public function onLoadLocalEntity(LoadLocalEntityEvent $event) {
    $cdf = $event
      ->getCdf();
    if ($cdf
      ->getType() !== 'drupal8_content_entity') {
      return;
    }
    $attribute = $cdf
      ->getAttribute('entity_type');

    // We only care about user entities.
    if (!$attribute || $attribute
      ->getValue()['und'] !== 'user') {
      return;
    }

    // Don't do anything with anonymous users.
    if ($anonymous = $event
      ->getCdf()
      ->getAttribute('is_anonymous')) {
      return;
    }
    $mail_attribute = $cdf
      ->getAttribute('mail');
    if (!$mail_attribute) {
      return;
    }
    $mail = $mail_attribute
      ->getValue()['und'];

    /** @var \Drupal\user\UserInterface $account */
    $account = user_load_by_mail($mail);
    if ($account) {
      $event
        ->setEntity($account);
      $event
        ->stopPropagation();
      return;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoadUserByNameOrMail::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LoadUserByNameOrMail::onLoadLocalEntity public function Loads local users for editing by username/mail or just mail.