You are here

public function YamlContentProcessManager::preprocessFieldData in YAML Content 8

Run any designated preprocessors on the provided field data.

Preprocessors are expected to be provided in the following format:

```yaml '#process': callback: '<callback string>' args:

  • <callback argument 1>
  • <callback argument 2>
  • <...>

```

The callback function receives the following arguments:

  • `$field`
  • `$field_data`
  • <callback argument 1>
  • <callback argument 2>
  • <...>

The `$field_data` array is passed by reference and may be modified directly by the callback implementation.

Parameters

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

array|string $field_data: The field data.

Throws

\Drupal\Core\TypedData\Exception\MissingDataException

File

src/Plugin/YamlContentProcessManager.php, line 65

Class

YamlContentProcessManager
Manages discovery and instantiation of YAML Content process plugins.

Namespace

Drupal\yaml_content\Plugin

Code

public function preprocessFieldData(ProcessingContext $context, &$field_data) {

  // Break here if the field data is not an array since there can be no
  // processing instructions included.
  if (!is_array($field_data)) {
    return;
  }

  // If there is no process element skip trying to process.
  if (!isset($field_data['#process'])) {
    return;
  }

  // If there is no process element skip trying to process.
  if (isset($field_data['#process'])) {
    $process_config = $field_data['#process'];
    if (isset($process_config['callback'])) {
      $plugin_id = $process_config['callback'];

      /** @var \Drupal\yaml_content\Plugin\YamlContentProcessInterface $plugin */
      $plugin = $this
        ->createInstance($plugin_id, $process_config['args']);
      $plugin
        ->process($context, $field_data);
    }
  }
}