Version 1
[yaffs-website] / web / core / modules / shortcut / src / Plugin / migrate / destination / ShortcutSetUsers.php
1 <?php
2
3 namespace Drupal\shortcut\Plugin\migrate\destination;
4
5 use Drupal\shortcut\ShortcutSetStorageInterface;
6 use Drupal\user\Entity\User;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\migrate\Row;
9 use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12
13 /**
14  * @MigrateDestination(
15  *   id = "shortcut_set_users"
16  * )
17  */
18 class ShortcutSetUsers extends DestinationBase implements ContainerFactoryPluginInterface {
19
20   /**
21    * The shortcut set storage handler.
22    *
23    * @var \Drupal\shortcut\ShortcutSetStorageInterface
24    */
25   protected $shortcutSetStorage;
26
27   /**
28    * Constructs an entity destination plugin.
29    *
30    * @param array $configuration
31    *   A configuration array containing information about the plugin instance.
32    * @param string $plugin_id
33    *   The plugin_id for the plugin instance.
34    * @param mixed $plugin_definition
35    *   The plugin implementation definition.
36    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
37    *   The migration.
38    * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
39    *   The shortcut_set entity storage handler.
40    */
41   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, ShortcutSetStorageInterface $shortcut_set_storage) {
42     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
43     $this->shortcutSetStorage = $shortcut_set_storage;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
50     return new static(
51       $configuration,
52       $plugin_id,
53       $plugin_definition,
54       $migration,
55       $container->get('entity.manager')->getStorage('shortcut_set')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getIds() {
63     return [
64       'set_name' => [
65         'type' => 'string',
66       ],
67       'uid' => [
68         'type' => 'integer',
69       ],
70     ];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function fields(MigrationInterface $migration = NULL) {
77     return [
78       'uid' => 'The users.uid for this set.',
79       'source' => 'The shortcut_set.set_name that will be displayed for this user.',
80     ];
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function import(Row $row, array $old_destination_id_values = []) {
87     /** @var \Drupal\shortcut\ShortcutSetInterface $set */
88     $set = $this->shortcutSetStorage->load($row->getDestinationProperty('set_name'));
89     /** @var \Drupal\user\UserInterface $account */
90     $account = User::load($row->getDestinationProperty('uid'));
91     $this->shortcutSetStorage->assignUser($set, $account);
92
93     return [$set->id(), $account->id()];
94   }
95
96 }