Reference.php in YAML Content 8
File
src/Plugin/yaml_content/process/Reference.php
View source
<?php
namespace Drupal\yaml_content\Plugin\yaml_content\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\yaml_content\Plugin\ProcessingContext;
use Drupal\yaml_content\Plugin\YamlContentProcessBase;
use Drupal\yaml_content\Plugin\YamlContentProcessInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Reference extends YamlContentProcessBase implements YamlContentProcessInterface, ContainerFactoryPluginInterface {
protected $entityTypeManager;
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;
}
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'));
}
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);
$query = $entity_storage
->getQuery('AND');
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)) {
$target = 'target_id';
if (!empty($this->configuration[2])) {
print_r($this->configuration[2]);
$target = $this->configuration[2];
}
$field_data[$target] = array_shift($entity_ids);
unset($field_data['#process']);
return $entity_ids;
}
$this
->throwParamError('Unable to find referenced content', $entity_type, $filter_params);
}
}
Classes
Name |
Description |
Reference |
Plugin for querying and loading a referenced entity. |