You are here

protected function EntityLookup::determineLookupProperties in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::determineLookupProperties()
  2. 8.2 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::determineLookupProperties()
  3. 8.3 src/Plugin/migrate/process/EntityLookup.php \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup::determineLookupProperties()

Determine the lookup properties from config or target field configuration.

Parameters

string $destinationProperty: The destination property currently worked on. This is only used together with the $row above.

1 call to EntityLookup::determineLookupProperties()
EntityLookup::transform in src/Plugin/migrate/process/EntityLookup.php
Performs the associated process.

File

src/Plugin/migrate/process/EntityLookup.php, line 196

Class

EntityLookup
This plugin looks for existing entities.

Namespace

Drupal\migrate_plus\Plugin\migrate\process

Code

protected function determineLookupProperties($destinationProperty) {
  if (isset($this->configuration['access_check'])) {
    $this->accessCheck = $this->configuration['access_check'];
  }
  if (!empty($this->configuration['value_key'])) {
    $this->lookupValueKey = $this->configuration['value_key'];
  }
  if (!empty($this->configuration['bundle_key'])) {
    $this->lookupBundleKey = $this->configuration['bundle_key'];
  }
  if (!empty($this->configuration['bundle'])) {
    $this->lookupBundle = $this->configuration['bundle'];
  }
  if (!empty($this->configuration['entity_type'])) {
    $this->lookupEntityType = $this->configuration['entity_type'];
  }
  if (empty($this->lookupValueKey) || empty($this->lookupBundleKey) || empty($this->lookupBundle) || empty($this->lookupEntityType)) {

    // See if we can introspect the lookup properties from destination field.
    if (!empty($this->migration
      ->getProcess()[$this->destinationBundleKey][0]['default_value'])) {
      $destinationEntityBundle = $this->migration
        ->getProcess()[$this->destinationBundleKey][0]['default_value'];
      $fieldConfig = $this->entityManager
        ->getFieldDefinitions($this->destinationEntityType, $destinationEntityBundle)[$destinationProperty]
        ->getConfig($destinationEntityBundle);
      switch ($fieldConfig
        ->getType()) {
        case 'entity_reference':
          if (empty($this->lookupBundle)) {
            $handlerSettings = $fieldConfig
              ->getSetting('handler_settings');
            $bundles = array_filter((array) $handlerSettings['target_bundles']);
            if (count($bundles) == 1) {
              $this->lookupBundle = reset($bundles);
            }
            elseif (!empty($handlerSettings['auto_create']) && !empty($handlerSettings['auto_create_bundle'])) {
              $this->lookupBundle = reset($handlerSettings['auto_create_bundle']);
            }
          }

          // Make an assumption that if the selection handler can target more
          // than one type of entity that we will use the first entity type.
          $this->lookupEntityType = $this->lookupEntityType ?: reset($this->selectionPluginManager
            ->createInstance($fieldConfig
            ->getSetting('handler'))
            ->getPluginDefinition()['entity_types']);
          $this->lookupValueKey = $this->lookupValueKey ?: $this->entityManager
            ->getDefinition($this->lookupEntityType)
            ->getKey('label');
          $this->lookupBundleKey = $this->lookupBundleKey ?: $this->entityManager
            ->getDefinition($this->lookupEntityType)
            ->getKey('bundle');
          break;
        case 'file':
        case 'image':
          $this->lookupEntityType = 'file';
          $this->lookupValueKey = $this->lookupValueKey ?: 'uri';
          break;
        default:
          throw new MigrateException(sprintf('Destination field type %s is not a recognized reference type.', $fieldConfig
            ->getType()));
      }
    }
  }

  // If there aren't enough lookup properties available by now, then bail.
  if (empty($this->lookupValueKey)) {
    throw new MigrateException('The entity_lookup plugin requires a value_key, none located.');
  }
  if (!empty($this->lookupBundleKey) && empty($this->lookupBundle)) {
    throw new MigrateException('The entity_lookup plugin found no bundle but destination entity requires one.');
  }
  if (empty($this->lookupEntityType)) {
    throw new MigrateException('The entity_lookup plugin requires a entity_type, none located.');
  }
}