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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\yaml_content\Plugin\YamlContentProcessBase implements YamlContentProcessInterface
- class \Drupal\yaml_content\Plugin\yaml_content\process\Reference implements ContainerFactoryPluginInterface, YamlContentProcessInterface
- class \Drupal\yaml_content\Plugin\YamlContentProcessBase implements YamlContentProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\processView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
Reference:: |
protected | property | The entity manager. | |
Reference:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Reference:: |
public | function |
Processes field data. Overrides YamlContentProcessInterface:: |
1 |
Reference:: |
public | function |
Constructs a new EntityView. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
YamlContentProcessBase:: |
protected | function | Prepare an error message and throw error. |