You are here

public function Reference::process in YAML Content 8

Processes field data.

Parameters

\Drupal\yaml_content\Plugin\ProcessingContext $context: The processing context.

array $field_data: The field data.

Return value

array|int The entity id.

Throws

\Drupal\Core\TypedData\Exception\MissingDataException Error for missing data.

Overrides YamlContentProcessInterface::process

See also

\Drupal\yaml_content\Plugin\YamlContentProcessManager::preprocessFieldData()

1 call to Reference::process()
ReferenceUri::process in src/Plugin/yaml_content/process/ReferenceUri.php
Processes field data.
1 method overrides Reference::process()
ReferenceUri::process in src/Plugin/yaml_content/process/ReferenceUri.php
Processes field data.

File

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

Class

Reference
Plugin for querying and loading a referenced entity.

Namespace

Drupal\yaml_content\Plugin\yaml_content\process

Code

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);
}