You are here

class Reference in YAML Content 8

Plugin for querying and loading a referenced entity.

Plugin annotation


@YamlContentProcess(
  id = "reference",
  title = @Translation("Entity Reference Processor"),
  description = @Translation("Attach an entity reference.")
)

Hierarchy

Expanded class hierarchy of Reference

1 file declares its use of Reference
ReferenceTest.php in tests/src/Unit/Plugin/yaml_content/process/ReferenceTest.php

File

src/Plugin/yaml_content/process/Reference.php, line 21

Namespace

Drupal\yaml_content\Plugin\yaml_content\process
View source
class Reference extends YamlContentProcessBase implements YamlContentProcessInterface, ContainerFactoryPluginInterface {

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

  /**
   * Constructs a new EntityView.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function process(ProcessingContext $context, array &$field_data) {
    $entity_type = $this->configuration[0];
    $filter_params = $this->configuration[1];
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);

    // Use query factory to create a query object for the node of entity_type.
    $query = $entity_storage
      ->getQuery('AND');

    // Apply filter parameters.
    foreach ($filter_params as $property => $value) {
      $query
        ->condition($property, $value);
    }
    $entity_ids = $query
      ->execute();
    if (empty($entity_ids)) {
      $entity = $entity_storage
        ->create($filter_params);
      $entity_ids = [
        $entity
          ->id(),
      ];
    }
    if (!empty($entity_ids)) {

      // By default reference fields use "target_id" as the destination value
      // in the field structure to store the referenced ID. Some field types
      // use different strings, e.g. og_membership entities use "value". Allow
      // the target value to be changed by passing a third item to the reference
      // configuration.
      $target = 'target_id';
      if (!empty($this->configuration[2])) {
        print_r($this->configuration[2]);
        $target = $this->configuration[2];
      }

      // Use the first match for our value.
      $field_data[$target] = array_shift($entity_ids);

      // Remove process data to avoid issues when setting the value.
      unset($field_data['#process']);
      return $entity_ids;
    }
    $this
      ->throwParamError('Unable to find referenced content', $entity_type, $filter_params);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
MessengerTrait::$messenger protected property The messenger. 29
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::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
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::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.
Reference::$entityTypeManager protected property The entity manager.
Reference::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Reference::process public function Processes field data. Overrides YamlContentProcessInterface::process 1
Reference::__construct public function Constructs a new EntityView. Overrides PluginBase::__construct
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.
YamlContentProcessBase::throwParamError protected function Prepare an error message and throw error.