You are here

public function EntityReferenceTranslationDeriver::getDerivativeDefinitions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate_drupal/src/Plugin/migrate/EntityReferenceTranslationDeriver.php \Drupal\migrate_drupal\Plugin\migrate\EntityReferenceTranslationDeriver::getDerivativeDefinitions()

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

core/modules/migrate_drupal/src/Plugin/migrate/EntityReferenceTranslationDeriver.php, line 93

Class

EntityReferenceTranslationDeriver
Deriver for entity reference field translations.

Namespace

Drupal\migrate_drupal\Plugin\migrate

Code

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;
        $entity_type_definition = $this->entityTypeManager
          ->getDefinition($entity_type);

        // 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;
        if ($entity_type_definition
          ->hasKey('bundle')) {
          $derivative['source']['bundle'] = $bundle;
        }

        // Set the process pipeline.
        $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;
        if ($entity_type_definition
          ->hasKey('bundle')) {
          $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;
}