summaryrefslogtreecommitdiffstats
path: root/core/modules/migrate_drupal/src/Plugin/migrate/EntityReferenceTranslationDeriver.php
blob: d81b92cc10690d396a8fe79f7b0fcab4459e19fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

namespace Drupal\migrate_drupal\Plugin\migrate;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Deriver for entity reference field translations.
 *
 * A migration will be created for every bundle with at least one entity
 * reference field that is configured to point to one of the supported target
 * entity types. The migrations will update the entity reference fields with
 * values found in the mapping tables of the migrations associated with the
 * target types.
 *
 * Example:
 *
 * @code
 * id: d7_entity_reference_translation
 * label: Entity reference translations
 * migration_tags:
 *   - Drupal 7
 *   - Follow-up migration
 * deriver: Drupal\migrate_drupal\Plugin\migrate\EntityReferenceTranslationDeriver
 * target_types:
 *   node:
 *     - d7_node_translation
 * source:
 *   plugin: empty
 *   key: default
 *   target: default
 * process: []
 * destination:
 *   plugin: null
 * @endcode
 *
 * In this example, the only supported target type is 'node' and the associated
 * migration for the mapping table lookup is 'd7_node_translation'.
 */
class EntityReferenceTranslationDeriver extends DeriverBase implements ContainerDeriverInterface {

  use StringTranslationTrait;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * EntityReferenceTranslationDeriver constructor.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param \Drupal\core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct($base_plugin_id, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager) {
    $this->entityFieldManager = $entity_field_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $base_plugin_id,
      $container->get('entity_field.manager'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    // Get all entity reference fields.
    $field_map = $this->entityFieldManager->getFieldMapByFieldType('entity_reference');

    foreach ($field_map as $entity_type => $fields) {
      foreach ($fields as $field_name => $field) {
        foreach ($field['bundles'] as $bundle) {
          $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle);
          $target_type = $field_definitions[$field_name]->getSetting('target_type');

          // If the field's target type is not supported, skip it.
          if (!array_key_exists($target_type, $base_plugin_definition['target_types'])) {
            continue;
          }

          // Key derivatives by entity types and bundles.
          $derivative_key = $entity_type . '__' . $bundle;

          $derivative = $base_plugin_definition;

          // Set the migration label.
          $derivative['label'] = $this->t('@label (@derivative)', [
            '@label' => $base_plugin_definition['label'],
            '@derivative' => $derivative_key,
          ]);

          // Set the source plugin.
          $derivative['source']['plugin'] = 'content_entity' . PluginBase::DERIVATIVE_SEPARATOR . $entity_type;
          $derivative['source']['bundle'] = $bundle;

          // Set the process pipeline.
          $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type);
          $id_key = $entity_type_definition->getKey('id');
          $derivative['process'][$id_key] = $id_key;
          if ($entity_type_definition->isRevisionable()) {
            $revision_key = $entity_type_definition->getKey('revision');
            $derivative['process'][$revision_key] = $revision_key;
          }
          if ($entity_type_definition->isTranslatable()) {
            $langcode_key = $entity_type_definition->getKey('langcode');
            $derivative['process'][$langcode_key] = $langcode_key;
          }

          // Set the destination plugin.
          $derivative['destination']['plugin'] = 'entity' . PluginBase::DERIVATIVE_SEPARATOR . $entity_type;
          $derivative['destination']['default_bundle'] = $bundle;
          if ($entity_type_definition->isTranslatable()) {
            $derivative['destination']['translations'] = TRUE;
          }

          // Allow overwriting the entity reference field so we can update its
          // values with the ones found in the mapping table.
          $derivative['destination']['overwrite_properties'][$field_name] = $field_name;

          // Add the entity reference field to the process pipeline.
          $derivative['process'][$field_name] = [
            'plugin' => 'sub_process',
            'source' => $field_name,
            'process' => [
              'target_id' => [
                [
                  'plugin' => 'migration_lookup',
                  'source' => 'target_id',
                  'migration' => $base_plugin_definition['target_types'][$target_type],
                  'no_stub' => TRUE,
                ],
                [
                  'plugin' => 'skip_on_empty',
                  'method' => 'row',
                ],
                [
                  'plugin' => 'extract',
                  'index' => [0],
                ],
              ],
            ],
          ];

          if (!isset($this->derivatives[$derivative_key])) {
            // If this is a new derivative, add it to the returned derivatives.
            $this->derivatives[$derivative_key] = $derivative;
          }
          else {
            // If this is an existing derivative, it means this bundle has more
            // than one entity reference field. In that case, we only want to add
            // the field to the process pipeline and make it overwritable.
            $this->derivatives[$derivative_key]['process'] += $derivative['process'];
            $this->derivatives[$derivative_key]['destination']['overwrite_properties'] += $derivative['destination']['overwrite_properties'];
          }
        }
      }
    }

    return $this->derivatives;
  }

}