UserData.php

Same filename in this branch
  1. 10 core/modules/user/src/Plugin/views/field/UserData.php
  2. 10 core/modules/user/src/UserData.php
Same filename in other branches
  1. 9 core/modules/user/src/Plugin/views/field/UserData.php
  2. 9 core/modules/user/src/Plugin/migrate/destination/UserData.php
  3. 9 core/modules/user/src/UserData.php
  4. 8.9.x core/modules/user/src/Plugin/views/field/UserData.php
  5. 8.9.x core/modules/user/src/Plugin/migrate/destination/UserData.php
  6. 8.9.x core/modules/user/src/UserData.php
  7. 11.x core/modules/user/src/Plugin/views/field/UserData.php
  8. 11.x core/modules/user/src/Plugin/migrate/destination/UserData.php
  9. 11.x core/modules/user/src/UserData.php

Namespace

Drupal\user\Plugin\migrate\destination

File

core/modules/user/src/Plugin/migrate/destination/UserData.php

View source
<?php

namespace Drupal\user\Plugin\migrate\destination;

use Drupal\migrate\Attribute\MigrateDestination;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\user\UserData as UserDataStorage;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
 * Migration destination for user data.
 */
class UserData extends DestinationBase implements ContainerFactoryPluginInterface {
    
    /**
     * @var \Drupal\user\UserData
     */
    protected $userData;
    
    /**
     * Builds a user data entity destination.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin ID for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\migrate\Plugin\MigrationInterface $migration
     *   The migration.
     * @param \Drupal\user\UserData $user_data
     *   The user data service.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, UserDataStorage $user_data) {
        parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
        $this->userData = $user_data;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ?MigrationInterface $migration = NULL) {
        return new static($configuration, $plugin_id, $plugin_definition, $migration, $container->get('user.data'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function import(Row $row, array $old_destination_id_values = []) {
        $uid = $row->getDestinationProperty('uid');
        $module = $row->getDestinationProperty('module');
        $key = $row->getDestinationProperty('key');
        $this->userData
            ->set($module, $uid, $key, $row->getDestinationProperty('settings'));
        return [
            $uid,
            $module,
            $key,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getIds() {
        $ids['uid']['type'] = 'integer';
        $ids['module']['type'] = 'string';
        $ids['key']['type'] = 'string';
        return $ids;
    }
    
    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'uid' => 'The user id.',
            'module' => 'The module name responsible for the settings.',
            'key' => 'The setting key to save under.',
            'settings' => 'The settings to save.',
        ];
    }

}

Classes

Title Deprecated Summary
UserData Migration destination for user data.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.