bc3369586076d184064fa9e819a3f0777c2d99fd
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / EntityReferenceTranslationDeriver.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Component\Plugin\PluginBase;
7 use Drupal\Core\Entity\EntityFieldManagerInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Deriver for entity reference field translations.
15  *
16  * A migration will be created for every bundle with at least one entity
17  * reference field that is configured to point to one of the supported target
18  * entity types. The migrations will update the entity reference fields with
19  * values found in the mapping tables of the migrations associated with the
20  * target types.
21  *
22  * Example:
23  *
24  * @code
25  * id: d7_entity_reference_translation
26  * label: Entity reference translations
27  * migration_tags:
28  *   - Drupal 7
29  *   - Follow-up migration
30  * deriver: Drupal\migrate_drupal\Plugin\migrate\EntityReferenceTranslationDeriver
31  * target_types:
32  *   node:
33  *     - d7_node_translation
34  * source:
35  *   plugin: empty
36  *   key: default
37  *   target: default
38  * process: []
39  * destination:
40  *   plugin: null
41  * @endcode
42  *
43  * In this example, the only supported target type is 'node' and the associated
44  * migration for the mapping table lookup is 'd7_node_translation'.
45  */
46 class EntityReferenceTranslationDeriver extends DeriverBase implements ContainerDeriverInterface {
47
48   use StringTranslationTrait;
49
50   /**
51    * The entity field manager.
52    *
53    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
54    */
55   protected $entityFieldManager;
56
57   /**
58    * The entity type manager.
59    *
60    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
61    */
62   protected $entityTypeManager;
63
64   /**
65    * EntityReferenceTranslationDeriver constructor.
66    *
67    * @param string $base_plugin_id
68    *   The base plugin ID.
69    * @param \Drupal\core\Entity\EntityFieldManagerInterface $entity_field_manager
70    *   The entity field manager.
71    * @param \Drupal\core\Entity\EntityTypeManagerInterface $entity_type_manager
72    *   The entity type manager.
73    */
74   public function __construct($base_plugin_id, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager) {
75     $this->entityFieldManager = $entity_field_manager;
76     $this->entityTypeManager = $entity_type_manager;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public static function create(ContainerInterface $container, $base_plugin_id) {
83     return new static(
84       $base_plugin_id,
85       $container->get('entity_field.manager'),
86       $container->get('entity_type.manager')
87     );
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function getDerivativeDefinitions($base_plugin_definition) {
94     // Get all entity reference fields.
95     $field_map = $this->entityFieldManager->getFieldMapByFieldType('entity_reference');
96
97     foreach ($field_map as $entity_type => $fields) {
98       foreach ($fields as $field_name => $field) {
99         foreach ($field['bundles'] as $bundle) {
100           $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle);
101           $target_type = $field_definitions[$field_name]->getSetting('target_type');
102
103           // If the field's target type is not supported, skip it.
104           if (!array_key_exists($target_type, $base_plugin_definition['target_types'])) {
105             continue;
106           }
107
108           // Key derivatives by entity types and bundles.
109           $derivative_key = $entity_type . '__' . $bundle;
110
111           $derivative = $base_plugin_definition;
112           $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type);
113
114           // Set the migration label.
115           $derivative['label'] = $this->t('@label (@derivative)', [
116             '@label' => $base_plugin_definition['label'],
117             '@derivative' => $derivative_key,
118           ]);
119
120           // Set the source plugin.
121           $derivative['source']['plugin'] = 'content_entity' . PluginBase::DERIVATIVE_SEPARATOR . $entity_type;
122           if ($entity_type_definition->hasKey('bundle')) {
123             $derivative['source']['bundle'] = $bundle;
124           }
125
126           // Set the process pipeline.
127           $id_key = $entity_type_definition->getKey('id');
128           $derivative['process'][$id_key] = $id_key;
129           if ($entity_type_definition->isRevisionable()) {
130             $revision_key = $entity_type_definition->getKey('revision');
131             $derivative['process'][$revision_key] = $revision_key;
132           }
133           if ($entity_type_definition->isTranslatable()) {
134             $langcode_key = $entity_type_definition->getKey('langcode');
135             $derivative['process'][$langcode_key] = $langcode_key;
136           }
137
138           // Set the destination plugin.
139           $derivative['destination']['plugin'] = 'entity' . PluginBase::DERIVATIVE_SEPARATOR . $entity_type;
140           if ($entity_type_definition->hasKey('bundle')) {
141             $derivative['destination']['default_bundle'] = $bundle;
142           }
143           if ($entity_type_definition->isTranslatable()) {
144             $derivative['destination']['translations'] = TRUE;
145           }
146
147           // Allow overwriting the entity reference field so we can update its
148           // values with the ones found in the mapping table.
149           $derivative['destination']['overwrite_properties'][$field_name] = $field_name;
150
151           // Add the entity reference field to the process pipeline.
152           $derivative['process'][$field_name] = [
153             'plugin' => 'sub_process',
154             'source' => $field_name,
155             'process' => [
156               'target_id' => [
157                 [
158                   'plugin' => 'migration_lookup',
159                   'source' => 'target_id',
160                   'migration' => $base_plugin_definition['target_types'][$target_type],
161                   'no_stub' => TRUE,
162                 ],
163                 [
164                   'plugin' => 'skip_on_empty',
165                   'method' => 'row',
166                 ],
167                 [
168                   'plugin' => 'extract',
169                   'index' => [0],
170                 ],
171               ],
172             ],
173           ];
174
175           if (!isset($this->derivatives[$derivative_key])) {
176             // If this is a new derivative, add it to the returned derivatives.
177             $this->derivatives[$derivative_key] = $derivative;
178           }
179           else {
180             // If this is an existing derivative, it means this bundle has more
181             // than one entity reference field. In that case, we only want to add
182             // the field to the process pipeline and make it overwritable.
183             $this->derivatives[$derivative_key]['process'] += $derivative['process'];
184             $this->derivatives[$derivative_key]['destination']['overwrite_properties'] += $derivative['destination']['overwrite_properties'];
185           }
186         }
187       }
188     }
189
190     return $this->derivatives;
191   }
192
193 }