You are here

public static function Pattern::processMultipleSources in UI Patterns 8

Process fields.

Parameters

array $element: Render array.

Return value

array Render array.

File

src/Element/Pattern.php, line 150

Class

Pattern
Renders a pattern element.

Namespace

Drupal\ui_patterns\Element

Code

public static function processMultipleSources(array $element) {

  // Make sure we don't render anything in case fields are empty.
  if (self::hasFields($element) && self::hasMultipleSources($element)) {
    foreach ($element['#fields'] as $name => $field) {

      // This guarantees backward compatibility: single sources be simple.
      $element['#fields'][$name] = reset($field);
      if (count($field) > 1) {

        /** @var \Drupal\ui_patterns\Element\PatternContext $context */
        $context = $element['#context'];
        $context
          ->setProperty('pattern', $element['#id']);
        $context
          ->setProperty('field', $name);

        // Render multiple sources with "patterns_destination" template.
        $element['#fields'][$name] = [
          '#sources' => $field,
          '#context' => $context,
          '#theme' => 'patterns_destination',
        ];
      }
    }
  }
  return $element;
}