You are here

public function ExistingUser::onParseCdf in Acquia Content Hub 8.2

Parses the CDF representation of Content Entities.

Parameters

\Drupal\acquia_contenthub\Event\ParseCdfEntityEvent $event: Event object.

File

src/EventSubscriber/Cdf/ExistingUser.php, line 35

Class

ExistingUser
Prevent user name conflicts.

Namespace

Drupal\acquia_contenthub\EventSubscriber\Cdf

Code

public function onParseCdf(ParseCdfEntityEvent $event) {
  $cdf = $event
    ->getCDF();

  // Bail early if this isn't a user entity.
  if ($cdf
    ->getAttribute('entity_type')
    ->getValue()['und'] !== 'user') {
    return;
  }
  $username = $cdf
    ->getAttribute('username')
    ->getValue()['und'];

  /** @var \Drupal\user\UserInterface $account */
  $account = user_load_by_name($username);
  if (!$account) {

    // No local user by that name, proceed.
    return;
  }
  if ($account
    ->uuid() === $event
    ->getEntity()
    ->uuid()) {

    // If the uuids are the same, these are the same user.
    return;
  }
  if ($account
    ->getEmail() !== $event
    ->getEntity()
    ->getEmail()) {

    /** @var \Drupal\user\Entity\User $entity */
    $entity = $event
      ->getEntity();
    $username = $this
      ->generateUsername(self::GENERATED_USER_PATTERN, $cdf
      ->getUuid(), $username);
    $entity
      ->setUsername($username);
    $event
      ->setEntity($entity);
  }
}