You are here

public function PatternFormatter::viewElements in UI Patterns Field Formatters 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/PatternFormatter.php, line 198

Class

PatternFormatter
Plugin implementation of the 'pattern' formatter.

Namespace

Drupal\ui_patterns_field_formatters\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $pattern = $this
      ->getSetting('pattern');

    // Set pattern fields.
    $fields = [];
    $mapping = $this
      ->getSetting('pattern_mapping');
    $mapping = $mapping[$pattern]['settings'];
    foreach ($mapping as $source => $field) {
      if ($field['destination'] === '_hidden') {
        continue;
      }

      // Get rid of the source tag.
      $source = explode(":", $source)[1];
      if ($item
        ->get($source) instanceof TextProcessed) {
        $value = $item
          ->get($source)
          ->getValue();
      }
      else {
        $value = (string) $item
          ->get($source)
          ->getValue();
      }

      // Preprocess Uri datatype instead of pushing the raw value.
      if ($item
        ->get($source) instanceof Uri) {
        $options = [];

        // Most of the time, Uri datatype is met in a "link" field type.
        if ($item
          ->getPluginId() === "field_item:link") {
          $options = $item
            ->get('options')
            ->toArray();
        }
        $value = Url::fromUri($value, $options)
          ->toString();
      }
      $fields[$field['destination']][] = $value;
    }

    // Set pattern render array.
    $elements[$delta] = [
      '#type' => 'pattern',
      '#id' => $this
        ->getSetting('pattern'),
      '#fields' => $fields,
      '#multiple_sources' => TRUE,
    ];

    // Set the variant.
    $pattern_variant = $this
      ->getCurrentVariant($pattern);
    if (isset($pattern_variant)) {
      $elements[$delta]['#variant'] = $pattern_variant;
    }

    // Set the settings.
    $settings = $this
      ->getSetting('pattern_settings');
    $pattern_settings = !empty($settings) && isset($settings[$pattern]) ? $settings[$pattern] : NULL;
    if (isset($pattern_settings)) {
      $elements[$delta]['#settings'] = $pattern_settings;
    }

    // Set the variant tokens.
    $variant_tokens = $this
      ->getSetting('variants_token');
    $variant_token = !empty($variant_tokens) && isset($variant_tokens[$pattern]) ? $variant_tokens[$pattern] : NULL;
    if (isset($variant_tokens)) {
      $elements[$delta]['#variant_token'] = $variant_token;
    }

    // Set pattern context.
    $entity = $items
      ->getEntity();
    $elements[$delta]['#context'] = [
      'type' => 'field_formatter',
      'entity' => $entity,
      'item' => $item,
    ];
  }
  return $elements;
}