You are here

class ImportUserData in Acquia Content Hub 8.2

Imports user data.

@package Drupal\acquia_contenthub\EventSubscriber\ExtraCdfFields

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\EntityImport\ImportUserData implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ImportUserData

1 string reference to 'ImportUserData'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses ImportUserData
user_data.import in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\EntityImport\ImportUserData

File

src/EventSubscriber/EntityImport/ImportUserData.php, line 15

Namespace

Drupal\acquia_contenthub\EventSubscriber\EntityImport
View source
class ImportUserData implements EventSubscriberInterface {

  /**
   * UserData service.
   *
   * @var \Drupal\user\UserData
   */
  protected $userData;
  protected const IMPORT_USER_DATA_METHOD = 'onImportUserData';

  /**
   * ImportUserDataExtraField constructor.
   *
   * @param \Drupal\user\UserData $user_data
   *   The UserData object.
   */
  public function __construct(UserData $user_data) {
    $this->userData = $user_data;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::ENTITY_IMPORT_NEW][] = self::IMPORT_USER_DATA_METHOD;
    $events[AcquiaContentHubEvents::ENTITY_IMPORT_UPDATE][] = self::IMPORT_USER_DATA_METHOD;
    return $events;
  }

  /**
   * Imports User Data.
   *
   * @param \Drupal\acquia_contenthub\Event\EntityImportEvent $event
   *   Event object.
   */
  public function onImportUserData(EntityImportEvent $event) {
    $entity = $event
      ->getEntity();
    if ('user' !== $entity
      ->getEntityTypeId()) {
      return;
    }
    $cdf = $event
      ->getEntityData();
    $metadata = $cdf
      ->getMetadata();
    if (!isset($metadata['user_data'])) {
      return;
    }
    $uid = $entity
      ->id();

    // Delete all current User Data.
    $this->userData
      ->delete(NULL, $uid);

    // Import actual User Data.
    foreach ($metadata['user_data'] as $module => $data) {
      foreach ($data as $key => $value) {
        $this->userData
          ->set($module, $uid, $key, $value);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImportUserData::$userData protected property UserData service.
ImportUserData::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ImportUserData::IMPORT_USER_DATA_METHOD protected constant
ImportUserData::onImportUserData public function Imports User Data.
ImportUserData::__construct public function ImportUserDataExtraField constructor.