You are here

public function ProcessedContentLoader::preprocessData in YAML Content 8.2

Evaluate the current import data array and run any preprocessing needed.

Any data keys starting with '#' indicate preprocessing instructions that should be executed on the data prior to import. The data array is altered directly and fully prepared for import.

Parameters

array $import_data: The current content data being evaluated for import. This array is altered directly and returned without the processing key.

array $context: Contextual data passed by reference to preprocessing plugins.

Throws

Exception

5 calls to ProcessedContentLoader::preprocessData()
ProcessedContentLoader::importEntity in src/ContentLoader/ProcessedContentLoader.php
Load an entity from a loaded import data outline.
ProcessedContentLoader::importEntityField in src/ContentLoader/ProcessedContentLoader.php
Process import data into an appropriate field value and assign it.
ProcessedContentLoader::importFieldItem in src/ContentLoader/ProcessedContentLoader.php
Process import data for an individual field list item value.
ProcessedContentLoader::loadContent in src/ContentLoader/ProcessedContentLoader.php
Load all demo content for a set of parsed data.
ProcessedContentLoader::recursivelyPreprocessData in src/ContentLoader/ProcessedContentLoader.php
Recursively navigate the content hierarchy to apply processors.

File

src/ContentLoader/ProcessedContentLoader.php, line 177

Class

ProcessedContentLoader
A ContentLoader supporting processing of content through plugins.

Namespace

Drupal\yaml_content\ContentLoader

Code

public function preprocessData(array &$import_data, array &$context) {

  // Abort if there are no preprocessing instructions.
  if (!isset($import_data['#preprocess'])) {
    return;
  }
  if (!is_array($import_data['#preprocess'])) {
    throw new Exception('Preprocessing instructions must be provided as an array.');
  }

  // Execute all processing actions.
  foreach ($import_data['#preprocess'] as $key => $data) {

    // Execute preprocess actions.
    if (isset($data['#plugin'])) {

      // Expose preprocess configuration into context for the plugin.
      $processor_context = array_merge($context, $data);

      // Load the plugin.
      $processor = $this
        ->loadProcessor($data['#plugin'], $processor_context);
      assert($processor instanceof ImportProcessorInterface, 'Preprocess plugin [' . $data['#plugin'] . '] failed to load a valid ImportProcessor plugin.');

      // Execute plugin on $import_data.
      $processor
        ->preprocess($import_data);
    }
    else {
      throw new Exception('Preprocessing instructions require a defined "#plugin" identifier.');
    }
  }

  // Remove executed preprocess data.
  unset($import_data['#preprocess']);
}