3 namespace Drupal\migrate_drupal\Plugin\migrate;
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;
14 * Deriver for entity reference field translations.
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
25 * id: d7_entity_reference_translation
26 * label: Entity reference translations
29 * - Follow-up migration
30 * deriver: Drupal\migrate_drupal\Plugin\migrate\EntityReferenceTranslationDeriver
33 * - d7_node_translation
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'.
46 class EntityReferenceTranslationDeriver extends DeriverBase implements ContainerDeriverInterface {
48 use StringTranslationTrait;
51 * The entity field manager.
53 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
55 protected $entityFieldManager;
58 * The entity type manager.
60 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
62 protected $entityTypeManager;
65 * EntityReferenceTranslationDeriver constructor.
67 * @param string $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.
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;
82 public static function create(ContainerInterface $container, $base_plugin_id) {
85 $container->get('entity_field.manager'),
86 $container->get('entity_type.manager')
93 public function getDerivativeDefinitions($base_plugin_definition) {
94 // Get all entity reference fields.
95 $field_map = $this->entityFieldManager->getFieldMapByFieldType('entity_reference');
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');
103 // If the field's target type is not supported, skip it.
104 if (!array_key_exists($target_type, $base_plugin_definition['target_types'])) {
108 // Key derivatives by entity types and bundles.
109 $derivative_key = $entity_type . '__' . $bundle;
111 $derivative = $base_plugin_definition;
112 $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type);
114 // Set the migration label.
115 $derivative['label'] = $this->t('@label (@derivative)', [
116 '@label' => $base_plugin_definition['label'],
117 '@derivative' => $derivative_key,
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;
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;
133 if ($entity_type_definition->isTranslatable()) {
134 $langcode_key = $entity_type_definition->getKey('langcode');
135 $derivative['process'][$langcode_key] = $langcode_key;
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;
143 if ($entity_type_definition->isTranslatable()) {
144 $derivative['destination']['translations'] = TRUE;
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;
151 // Add the entity reference field to the process pipeline.
152 $derivative['process'][$field_name] = [
153 'plugin' => 'sub_process',
154 'source' => $field_name,
158 'plugin' => 'migration_lookup',
159 'source' => 'target_id',
160 'migration' => $base_plugin_definition['target_types'][$target_type],
164 'plugin' => 'skip_on_empty',
168 'plugin' => 'extract',
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;
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'];
190 return $this->derivatives;