You are here

class ExportUserData in Acquia Content Hub 8.2

Exports user data.

@package Drupal\acquia_contenthub\EventSubscriber\ExtraCdfFields

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\Cdf\ExportUserData implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ExportUserData

1 string reference to 'ExportUserData'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses ExportUserData
user_data.export in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\Cdf\ExportUserData

File

src/EventSubscriber/Cdf/ExportUserData.php, line 16

Namespace

Drupal\acquia_contenthub\EventSubscriber\Cdf
View source
class ExportUserData implements EventSubscriberInterface {

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * UserData service.
   *
   * @var \Drupal\user\UserData
   */
  protected $userData;

  /**
   * UsersData constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Database connection.
   * @param \Drupal\user\UserData $user_data
   *   User Data service.
   */
  public function __construct(Connection $database, UserData $user_data) {
    $this->database = $database;
    $this->userData = $user_data;
  }

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

  /**
   * Serializes User Data.
   *
   * @param \Drupal\acquia_contenthub\Event\CreateCdfEntityEvent $event
   *   Event object.
   */
  public function onCreateCdf(CreateCdfEntityEvent $event) {
    $entity = $event
      ->getEntity();
    if ('user' !== $entity
      ->getEntityTypeId()) {
      return;
    }
    $cdf = $event
      ->getCdf($entity
      ->uuid());
    $metadata = $cdf
      ->getMetadata();
    $metadata['user_data'] = [];

    /** @var \Drupal\Core\Database\Query\Select $query */
    $query = $this->database
      ->select('users_data', 'ud')
      ->fields('ud', [
      'module',
    ])
      ->condition('uid', $entity
      ->id());
    $modules = $query
      ->execute()
      ->fetchCol();
    if (!$modules) {
      $cdf
        ->setMetadata($metadata);
      return;
    }
    foreach ($modules as $module) {
      $metadata['user_data'][$module] = $this->userData
        ->get($module, $entity
        ->id());
    }
    $cdf
      ->setMetadata($metadata);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExportUserData::$database protected property Database connection.
ExportUserData::$userData protected property UserData service.
ExportUserData::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ExportUserData::onCreateCdf public function Serializes User Data.
ExportUserData::__construct public function UsersData constructor.