You are here

class MediaTarget in Media Feeds 8

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

Plugin annotation


@FeedsTarget(
  id = "entity_reference",
  field_types = {
    "entity_reference",
  },
  arguments = {
    "@messenger",
    "@plugin.manager.feeds.target",
    "@entity_field.manager",
    "@entity_type.manager",
    "@entity.repository",
    "@entity.query",
  }
)

Hierarchy

Expanded class hierarchy of MediaTarget

File

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

Namespace

Drupal\media_feeds\Feeds\Target
View source
class MediaTarget extends EntityReference implements ConfigurableTargetInterface {

  /**
   * @var MessengerInterface
   */
  protected $messenger;

  /**
   * @var FeedsPluginManager
   */
  protected $plugin_manager;

  /**
   * @var EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The paragraph storage.
   *
   * @var EntityStorageInterface
   */
  protected $media_storage;

  /**
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var FieldConfigInterface
   */
  protected $field;

  /**
   * @var boolean
   */
  protected $isMedia;

  /**
   * @var FieldTargetBase
   */
  protected $targetInstance;
  public static $temp_type = "entity_reference";
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, MessengerInterface $messenger, FeedsPluginManager $plugin_manager, EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, QueryFactory $query_factory) {
    $this->messenger = $messenger;
    $this->plugin_manager = $plugin_manager;
    $this->entityFieldManager = $entityFieldManager;
    $this->entityTypeManager = $entity_type_manager;
    $this->media_storage = $entity_type_manager
      ->getStorage('media');
    $this->configuration = $configuration;
    $this->targetDefinition = $configuration['target_definition'];
    $this->field = $this->targetDefinition
      ->getFieldDefinition();
    if (self::isMedia($this->field)) {
      $this->isMedia = true;
      $this->targetInstance = $this
        ->createTargetInstance();
      FieldTargetBase::__construct($configuration, $plugin_id, $plugin_definition);
    }
    else {
      $this->isMedia = false;
      parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $query_factory, $entityFieldManager, $entity_repository);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function targets(array &$targets, FeedTypeInterface $feed_type, array $definition) {
    $processor = $feed_type
      ->getProcessor();
    $entity_type = $processor
      ->entityType();
    $bundle = $processor
      ->bundle();
    $fields = self::getFields($entity_type, $bundle);
    foreach ($fields as $field) {
      self::prepareTarget($field);
      if (isset($field->sub_fields)) {
        $sub_fields = $field->sub_fields;
        foreach ($sub_fields as $sub_field) {
          $subFInstance = $sub_field
            ->getFieldDefinition();
          $info = $subFInstance->media_feeds_info;
          $id = $info['host']['field'] . "_" . $subFInstance
            ->getName();
          $targets[$id] = $sub_field;
        }
      }
    }
    parent::targets($targets, $feed_type, $definition);
  }

  /**
   *
   * @return FieldConfigInterface[]
   */
  public static function getFields($entity_type, $bundle) {
    $fieldManager = \Drupal::service('entity_field.manager');
    $entityFields = $fieldManager
      ->getFieldDefinitions($entity_type, $bundle);
    $fields = array();
    foreach ($entityFields as $entityField) {
      if ($entityField instanceof FieldConfigInterface && $entityField
        ->getType() === 'entity_reference') {
        $fields[] = $entityField;
      }
    }
    return $fields;
  }

  /**
   * @param FieldDefinitionInterface $mediaField
   * @return array
   */
  public static function getSubFields(FieldDefinitionInterface $mediaField) {
    $settings = $mediaField
      ->getSetting('handler_settings');
    $bundles = $settings['target_bundles'];
    $fields = array();
    if (!is_array($bundles)) {
      return $fields;
    }
    $fieldManager = \Drupal::service('entity_field.manager');
    $addInfo = function ($field, $host, $bundle, $plugin) {
      $info = array(
        'plugin' => $plugin,
        'type' => $field
          ->getType(),
        'host' => array(
          'field' => $host,
          'bundle' => $bundle,
        ),
      );
      if ($field instanceof BaseFieldDefinition) {
        $field->media_feeds_info = $info;
      }
      else {
        $field
          ->set('media_feeds_info', $info);
      }
    };
    foreach ($bundles as $def_bundle) {
      $sub_fields = $fieldManager
        ->getFieldDefinitions('media', $def_bundle);
      foreach ($sub_fields as $fieldId => $sub_field) {
        $plugin = self::getPlugin($sub_field);
        if ($sub_field instanceof FieldConfigInterface) {
          if (isset($plugin)) {
            $type = $sub_field
              ->getType();
            $correctPlugin = $plugin;
            if (isset($sub_field->media_feeds_info) && $type === 'entity_reference') {
              $old_info = $sub_field->media_feeds_info;
              $correctPlugin = $old_info['plugin'];
              $sub_field = clone $sub_field;
            }
            $addInfo($sub_field, $mediaField
              ->getName(), $def_bundle, $correctPlugin);
            $id = $def_bundle . ':' . $sub_field
              ->getName();
            $fields[$id] = $sub_field;
          }
        }
      }
    }
    return $fields;
  }

  /**
   * @param FieldDefinitionInterface $field
   * @return bool
   */
  protected static function isMedia(FieldDefinitionInterface $field) {
    $info = null;
    $handler = $field
      ->getSetting('handler');
    if (!isset($media)) {
      $entityType = $field
        ->getTargetEntityTypeId();
      if ($entityType === 'media') {
        return true;
      }
    }
    return $handler === 'default:media';
  }

  /**
   * @param FieldDefinitionInterface $field
   * @param ProcessorInterface|null $processor
   * @return null
   */
  protected static function getPlugin(FieldDefinitionInterface $field, ProcessorInterface $processor = null) {
    $targetsManager = \Drupal::service('plugin.manager.feeds.target');
    $plugins = $targetsManager
      ->getDefinitions();
    $id = null;
    if ($field instanceof FieldConfigInterface) {
      $id = $field
        ->id();
    }
    else {
      if ($field instanceof BaseFieldDefinition) {
        $id = $field
          ->getUniqueIdentifier();
      }
    }
    if ($field
      ->isReadOnly() || isset($processor) && $id === $processor
      ->bundleKey()) {
      return null;
    }
    foreach ($plugins as $plugin) {
      if (in_array($field
        ->getType(), $plugin['field_types'])) {
        return $plugin;
      }
    }
    return null;
  }

  /**
   * @inheritdoc
   */
  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);
      }
    }
  }

  /**
   * @param FieldDefinitionInterface $field
   * @return null
   */
  protected static function callPrepareTarget(FieldDefinitionInterface $field) {
    $info = $field->media_feeds_info;
    $stop = null;
    $field_type = $info['type'];
    $plugin = $info['plugin'];
    if (!isset($field_type) || !isset($plugin)) {
      return null;
    }
    $class = $plugin['class'];
    if ($field instanceof FieldConfigInterface) {
      $field
        ->set('field_type', $info['type']);
    }
    $targetDef = $class::prepareTarget($field);
    $field_name = $field
      ->getName();
    $label = $field
      ->getLabel();
    $fracs = explode('(', $label);
    $old_label = $fracs[0];
    $target_bundle = $info['host']['bundle'];
    if (isset($info['host']['field'])) {
      $host_field = $info['host']['field'];
      $old_label .= ' (' . $host_field . ':' . $target_bundle . ':' . $field_name . ')';
    }
    else {
      $old_label .= ' (' . $target_bundle . ':' . $field_name . ')';
    }
    $field
      ->setLabel($old_label);
    if ($field instanceof FieldConfigInterface) {
      $field
        ->set('field_type', self::$temp_type);
    }
    return $targetDef;
  }

  /**
   * @return null|object
   */
  public function createTargetInstance() {
    $info = $this->field->media_feeds_info;
    $plugin = $info['plugin'];
    $class = $plugin['class'];
    $target = $class::prepareTarget($this->field);
    $target
      ->setPluginId($plugin['id']);
    $instance = null;
    try {
      $instance = $this->plugin_manager
        ->createInstance($plugin['id'], $this->configuration);
    } catch (PluginException $e) {
    }
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    if ($this->isMedia) {
      $config = $this->targetInstance
        ->defaultConfiguration();
      return $config;
    }
    else {
      return parent::defaultConfiguration();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    if ($this->isMedia) {
      $config = $this->targetInstance
        ->buildConfigurationForm($form, $form_state);
      return $config;
    }
    else {
      return parent::buildConfigurationForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if ($this->isMedia) {
      $delta = $form_state
        ->getTriggeringElement()['#delta'];
      $configuration = $form_state
        ->getValue([
        'mappings',
        $delta,
        'settings',
      ]);
      $this->targetInstance
        ->submitConfigurationForm($form, $form_state);
      $this
        ->setConfiguration($configuration);
    }
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    if ($this->isMedia) {
      $sum = null;
      if ($this->targetInstance instanceof ConfigurableTargetInterface) {
        $sum = $this->targetInstance
          ->getSummary();
      }
      return $sum;
    }
    else {
      return parent::getSummary();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setTarget(FeedInterface $feed, EntityInterface $entity, $field_name, array $values) {
    if ($this
      ->isEmpty($values)) {
      return null;
    }
    $target = $this->targetDefinition;
    $target = $target
      ->getFieldDefinition();
    $field_name = $target
      ->getName();
    if (!$this->isMedia) {
      parent::setTarget($feed, $entity, $field_name, $values);
    }
    else {
      $info = $target->media_feeds_info;
      $mediaEntity = $this
        ->createMediaEntity($entity, $info['host']['bundle'], $info['host']['field']);
      $mediaEntity->{$field_name} = NULL;
      if ($mediaEntity
        ->hasField($field_name)) {

        //
      }
      else {
        $stop = null;
      }
      $this->targetInstance
        ->setTarget($feed, $mediaEntity, $target
        ->getName(), $values);
    }
  }
  public function isEmpty($values) {
    $properties = $this->targetDefinition
      ->getProperties();
    $emptyValues = 0;
    foreach ($values as $value) {
      $currentProperties = array_keys($value);
      $emptyProps = [];
      foreach ($properties as $property) {
        foreach ($currentProperties as $currentProperty) {
          if ($currentProperty === $property) {
            if (!strlen($value[$currentProperty])) {
              $emptyProps[] = $currentProperty;
            }
          }
        }
      }
      if (count($emptyProps) === count($properties)) {
        $emptyValues++;
      }
    }
    return $emptyValues === count($values);
  }

  /**
   * @param $host_entity
   * @param $bundle
   * @param $host_field
   * @return Media
   */
  public function createMediaEntity($host_entity, $bundle, $host_field) {
    $value = $host_entity
      ->get($host_field)
      ->getValue();
    if (count($value)) {
      $value = end($value);
      if (isset($value['entity'])) {
        return $value['entity'];
      }
    }
    $media = $this->media_storage
      ->create([
      "bundle" => $bundle,
      'name' => 'Default name',
    ]);
    $host_entity
      ->get($host_field)
      ->setValue($media);
    return $media;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurablePluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
EntityReference::$entityFinder protected property The Feeds entity finder service.
EntityReference::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
EntityReference::createEntity protected function Creates a new entity with the given label and saves it. 1
EntityReference::filterFieldTypes protected function Callback for the potential field filter. 1
EntityReference::findEntities protected function Tries to lookup an existing entity.
EntityReference::findEntity protected function Searches for an entity by entity key.
EntityReference::getBundleKey protected function Returns the entity type's bundle key.
EntityReference::getBundles protected function Returns a list of bundles that may be referenced. 1
EntityReference::getEntityType protected function Returns the entity type to reference. 1
EntityReference::getFeedsItemOptions public function Returns options for feeds_item configuration.
EntityReference::getLabelKey protected function Returns the entity type's label key.
EntityReference::getLangcodeKey protected function Returns the entity type's langcode key, if it has one.
EntityReference::getPotentialFields protected function Returns a list of fields that may be used to reference by.
EntityReference::prepareValue protected function Prepares a single value. Overrides FieldTargetBase::prepareValue 1
FieldTargetBase::$fieldSettings protected property The field settings.
FieldTargetBase::$languageManager protected property The language manager.
FieldTargetBase::addMessage protected function Adds a message.
FieldTargetBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides PluginBase::calculateDependencies
FieldTargetBase::getEntityTarget public function Get entity, or entity translation to set the map.
FieldTargetBase::getLangcode public function Gets the configured language. Overrides TranslatableTargetInterface::getLangcode
FieldTargetBase::getLanguageManager protected function Gets the language manager.
FieldTargetBase::getMessenger protected function Returns the messenger to use.
FieldTargetBase::getUniqueQuery protected function Constructs a base query which is used to find an existing entity.
FieldTargetBase::getUniqueValue public function Looks for an existing entity and returns an entity ID if found.
FieldTargetBase::isMutable public function Returns if the target is mutable. Overrides TargetInterface::isMutable 1
FieldTargetBase::isTargetFieldTranslatable protected function Checks if the targeted field is translatable.
FieldTargetBase::isTargetTranslatable public function Checks if the target is translatable. Overrides TranslatableTargetInterface::isTargetTranslatable
FieldTargetBase::languageExists public function Checks if the language selected on the target exists. Overrides TranslatableTargetInterface::languageExists
FieldTargetBase::onDependencyRemoval public function Allows a plugin to define whether it should be removed. Overrides TargetBase::onDependencyRemoval
FieldTargetBase::prepareValues protected function Prepares the the values that will be mapped to an entity.
FieldTargetBase::setLanguageManager public function Sets the language manager.
MediaTarget::$entityFieldManager protected property Overrides EntityReference::$entityFieldManager
MediaTarget::$entityTypeManager protected property Overrides EntityReference::$entityTypeManager
MediaTarget::$field protected property
MediaTarget::$isMedia protected property
MediaTarget::$media_storage protected property The paragraph storage.
MediaTarget::$messenger protected property Overrides MessengerTrait::$messenger
MediaTarget::$plugin_manager protected property
MediaTarget::$targetInstance protected property
MediaTarget::$temp_type public static property
MediaTarget::buildConfigurationForm public function Form constructor. Overrides EntityReference::buildConfigurationForm
MediaTarget::callPrepareTarget protected static function
MediaTarget::createMediaEntity public function
MediaTarget::createTargetInstance public function
MediaTarget::defaultConfiguration public function Gets default configuration for this plugin. Overrides EntityReference::defaultConfiguration
MediaTarget::getFields public static function
MediaTarget::getPlugin protected static function
MediaTarget::getSubFields public static function
MediaTarget::getSummary public function Returns the summary for a target. Overrides EntityReference::getSummary
MediaTarget::isEmpty public function Returns if the value for the target is empty. Overrides FieldTargetBase::isEmpty
MediaTarget::isMedia protected static function
MediaTarget::prepareTarget protected static function @inheritdoc Overrides EntityReference::prepareTarget
MediaTarget::setTarget public function Sets the values on an object. Overrides EntityReference::setTarget
MediaTarget::submitConfigurationForm public function Form submission handler. Overrides TargetBase::submitConfigurationForm
MediaTarget::targets public static function Returns the targets defined by this plugin. Overrides FieldTargetBase::targets
MediaTarget::__construct public function Constructs a new EntityReference object. Overrides EntityReference::__construct
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$feedType protected property The importer this plugin is working for.
PluginBase::$linkGenerator protected property The link generator.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$urlGenerator protected property The url generator.
PluginBase::container private function Returns the service container.
PluginBase::defaultFeedConfiguration public function Returns default feed configuration. Overrides FeedsPluginInterface::defaultFeedConfiguration 3
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::l protected function Renders a link to a route given a route name and its parameters.
PluginBase::linkGenerator protected function Returns the link generator service.
PluginBase::onFeedDeleteMultiple public function A feed is being deleted. 3
PluginBase::onFeedSave public function A feed is being saved.
PluginBase::onFeedTypeDelete public function The feed type is being deleted. 1
PluginBase::onFeedTypeSave public function The feed type is being saved. 1
PluginBase::pluginType public function Returns the type of plugin. Overrides FeedsPluginInterface::pluginType
PluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration 1
PluginBase::url protected function Generates a URL or path for a specific route based on the given parameters.
PluginBase::urlGenerator protected function Returns the URL generator service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TargetBase::$targetDefinition protected property The target definition.
TargetBase::getTargetDefinition public function Returns the target's definition. Overrides TargetInterface::getTargetDefinition