b934a70635c15f5a5acff8eee7bae4931e338c1b
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / EntityConfigBase.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\language\ConfigurableLanguageManager;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\MigrateException;
13 use Drupal\migrate\Plugin\MigrateIdMapInterface;
14 use Drupal\migrate\Row;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Base destination class for importing configuration entities.
19  *
20  * Available configuration keys:
21  * - translations: (optional) Boolean, if TRUE, the destination will be
22  *   associated with the langcode provided by the source plugin. Defaults to
23  *   FALSE.
24  *
25  * Examples:
26  *
27  * @code
28  * source:
29  *   plugin: d7_block_custom
30  * process:
31  *   id: bid
32  *   info: info
33  *   langcode: language
34  *   body: body
35  * destination:
36  *   plugin: entity:block
37  * @endcode
38  *
39  * This will save the migrated, processed row as a block config entity.
40  *
41  * @code
42  * source:
43  *   plugin: d6_i18n_profile_field
44  *   constants:
45  *     entity_type: user
46  *     bundle: user
47  * process:
48  *   langcode: language
49  *   entity_type: 'constants/entity_type'
50  *   bundle: 'constants/bundle'
51  *   field_name: name
52  *   ...
53  *   translation: translation
54  * destination:
55  *   plugin: entity:field_config
56  *   translations: true
57  * @endcode
58  *
59  * Because the translations configuration is set to "true", this will save the
60  * migrated, processed row to a "field_config" entity associated with the
61  * designated langcode.
62  */
63 class EntityConfigBase extends Entity {
64
65   /**
66    * The language manager.
67    *
68    * @var \Drupal\Core\Language\LanguageManagerInterface
69    */
70   protected $languageManager;
71
72   /**
73    * The configuration factory.
74    *
75    * @var \Drupal\Core\Config\ConfigFactoryInterface
76    */
77   protected $configFactory;
78
79   /**
80    * Construct a new entity.
81    *
82    * @param array $configuration
83    *   A configuration array containing information about the plugin instance.
84    * @param string $plugin_id
85    *   The plugin_id for the plugin instance.
86    * @param mixed $plugin_definition
87    *   The plugin implementation definition.
88    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
89    *   The migration.
90    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
91    *   The storage for this entity type.
92    * @param array $bundles
93    *   The list of bundles this entity type has.
94    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
95    *   The language manager.
96    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
97    *   The configuration factory.
98    */
99   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory) {
100     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles);
101     $this->languageManager = $language_manager;
102     $this->configFactory = $config_factory;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
109     $entity_type_id = static::getEntityTypeId($plugin_id);
110     return new static(
111       $configuration,
112       $plugin_id,
113       $plugin_definition,
114       $migration,
115       $container->get('entity.manager')->getStorage($entity_type_id),
116       array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)),
117       $container->get('language_manager'),
118       $container->get('config.factory')
119     );
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function import(Row $row, array $old_destination_id_values = []) {
126     if ($row->isStub()) {
127       throw new MigrateException('Config entities can not be stubbed.');
128     }
129     $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
130     $ids = $this->getIds();
131     $id_key = $this->getKey('id');
132     if (count($ids) > 1) {
133       // Ids is keyed by the key name so grab the keys.
134       $id_keys = array_keys($ids);
135       if (!$row->getDestinationProperty($id_key)) {
136         // Set the ID into the destination in for form "val1.val2.val3".
137         $row->setDestinationProperty($id_key, $this->generateId($row, $id_keys));
138       }
139     }
140     $entity = $this->getEntity($row, $old_destination_id_values);
141     // Translations are already saved in updateEntity by configuration override.
142     if (!$this->isTranslationDestination()) {
143       $entity->save();
144     }
145     if (count($ids) > 1) {
146       // This can only be a config entity, content entities have their ID key
147       // and that's it.
148       $return = [];
149       foreach ($id_keys as $id_key) {
150         if (($this->isTranslationDestination()) && ($id_key == 'langcode')) {
151           // Config entities do not have a language property, get the language
152           // code from the destination.
153           $return[] = $row->getDestinationProperty($id_key);
154         }
155         else {
156           $return[] = $entity->get($id_key);
157         }
158       }
159       return $return;
160     }
161     return [$entity->id()];
162   }
163
164   /**
165    * Get whether this destination is for translations.
166    *
167    * @return bool
168    *   Whether this destination is for translations.
169    */
170   protected function isTranslationDestination() {
171     return !empty($this->configuration['translations']);
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function getIds() {
178     $id_key = $this->getKey('id');
179     $ids[$id_key]['type'] = 'string';
180     if ($this->isTranslationDestination()) {
181       $ids['langcode']['type'] = 'string';
182     }
183     return $ids;
184   }
185
186   /**
187    * Updates an entity with the contents of a row.
188    *
189    * @param \Drupal\Core\Entity\EntityInterface $entity
190    *   The entity to update.
191    * @param \Drupal\migrate\Row $row
192    *   The row object to update from.
193    */
194   protected function updateEntity(EntityInterface $entity, Row $row) {
195     // This is a translation if the language in the active config does not
196     // match the language of this row.
197     $translation = FALSE;
198     if ($row->hasDestinationProperty('langcode') && $this->languageManager instanceof ConfigurableLanguageManager) {
199       $config = $entity->getConfigDependencyName();
200       $langcode = $this->configFactory->get('langcode');
201       if ($langcode != $row->getDestinationProperty('langcode')) {
202         $translation = TRUE;
203       }
204     }
205
206     if ($translation) {
207       $config_override = $this->languageManager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $config);
208       $config_override->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $row->getDestinationProperty('property')), $row->getDestinationProperty('translation'));
209       $config_override->save();
210     }
211     else {
212       foreach ($row->getRawDestination() as $property => $value) {
213         $this->updateEntityProperty($entity, explode(Row::PROPERTY_SEPARATOR, $property), $value);
214       }
215       $this->setRollbackAction($row->getIdMap());
216     }
217   }
218
219   /**
220    * Updates a (possible nested) entity property with a value.
221    *
222    * @param \Drupal\Core\Entity\EntityInterface $entity
223    *   The config entity.
224    * @param array $parents
225    *   The array of parents.
226    * @param string|object $value
227    *   The value to update to.
228    */
229   protected function updateEntityProperty(EntityInterface $entity, array $parents, $value) {
230     $top_key = array_shift($parents);
231     $entity_value = $entity->get($top_key);
232     if (is_array($entity_value)) {
233       NestedArray::setValue($entity_value, $parents, $value);
234     }
235     else {
236       $entity_value = $value;
237     }
238     $entity->set($top_key, $entity_value);
239   }
240
241   /**
242    * Generates an entity ID.
243    *
244    * @param \Drupal\migrate\Row $row
245    *   The current row.
246    * @param array $ids
247    *   The destination IDs.
248    *
249    * @return string
250    *   The generated entity ID.
251    */
252   protected function generateId(Row $row, array $ids) {
253     $id_values = [];
254     foreach ($ids as $id) {
255       if ($this->isTranslationDestination() && $id == 'langcode') {
256         continue;
257       }
258       $id_values[] = $row->getDestinationProperty($id);
259     }
260     return implode('.', $id_values);
261   }
262
263   /**
264    * {@inheritdoc}
265    */
266   public function rollback(array $destination_identifier) {
267     if ($this->isTranslationDestination()) {
268       // The entity id does not include the langcode.
269       $id_values = [];
270       foreach ($destination_identifier as $key => $value) {
271         if ($this->isTranslationDestination() && $key === 'langcode') {
272           continue;
273         }
274         $id_values[] = $value;
275       }
276       $entity_id = implode('.', $id_values);
277       $language = $destination_identifier['langcode'];
278
279       $config = $this->storage->load($entity_id)->getConfigDependencyName();
280       $config_override = $this->languageManager->getLanguageConfigOverride($language, $config);
281       // Rollback the translation.
282       $config_override->delete();
283     }
284     else {
285       $destination_identifier = implode('.', $destination_identifier);
286       parent::rollback([$destination_identifier]);
287     }
288   }
289
290 }