You are here

protected static function MediaTarget::prepareTarget in Media Feeds 8

@inheritdoc

Overrides EntityReference::prepareTarget

1 call to MediaTarget::prepareTarget()
MediaTarget::targets in src/Feeds/Target/MediaTarget.php
Returns the targets defined by this plugin.

File

src/Feeds/Target/MediaTarget.php, line 262

Class

MediaTarget
Defines a wrapper target around a paragraph bundle's target field.

Namespace

Drupal\media_feeds\Feeds\Target

Code

protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
  $type = $field_definition
    ->getType();
  $name = $field_definition
    ->getName();
  if ($name === 'field_teaser_media') {
    $stop = null;
  }
  if (isset($field_definition->media_feeds_info)) {
    $info = $field_definition->media_feeds_info;
    $type = $info['type'];
  }
  if ($type === 'entity_reference' && !self::isMedia($field_definition)) {

    // the field is normal entity reference
    $def = parent::prepareTarget($field_definition);
    $def
      ->setPluginId('entity_reference');
    return $def;
  }
  else {
    if (self::isMedia($field_definition)) {

      // the field is media:
      $subFields = self::getSubFields($field_definition);
      $fields = array();
      foreach ($subFields as $id => $subField) {
        $target = self::callPrepareTarget($subField);
        if (!isset($target)) {
          continue;
        }
        $target
          ->setPluginId("entity_reference");

        // we set our important settings name like [plugin_id]_handler_settings,
        // so plugins like feeds_para_mapper can find these settings:
        $info = $info = $subField->media_feeds_info;
        $handler_settings = [
          'type' => $info['type'],
          'host_bundle' => $info['host']['bundle'],
          'host_field' => $info['host']['field'],
        ];
        $subField->entity_reference_handler_settings = $handler_settings;
        $fields[] = $target;
      }
      if (count($fields)) {
        $field_definition->sub_fields = $fields;
      }
      return FieldTargetDefinition::createFromFieldDefinition($field_definition);
    }
    else {
      if ($field_definition instanceof BaseFieldDefinition) {
        $def = self::callPrepareTarget($field_definition);
        $def
          ->setPluginId("entity_reference");
        return $def;
      }
      return FieldTargetDefinition::createFromFieldDefinition($field_definition);
    }
  }
}