You are here

public function HMSNaturalLanguageFormatter::viewElements in HMS Field 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/HMSNaturalLanguageFormatter.php, line 133
Contains \Drupal\hms_field\Plugin\Field\FieldFormatter\HMSNaturalLanguageFormatter.

Class

HMSNaturalLanguageFormatter
Plugin implementation of the 'hms_natural_language_formatter' formatter.

Namespace

Drupal\hms_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = array();
  foreach ($items as $delta => $item) {
    $element[$delta]['#theme'] = 'hms_natural_language';
    $element[$delta]['#value'] = $item->value;
    $element[$delta]['#format'] = '';
    foreach ($this
      ->getSetting('display_formats') as $fragment) {
      if ($fragment) {
        $element[$delta]['#format'] .= ':' . $fragment;
      }
    }
    if (!strlen($element[$delta]['#format'])) {
      $element[$delta]['#format'] = implode(':', array_keys($this->hms_service
        ->factor_map(TRUE)));
    }
    else {
      $element[$delta]['#format'] = substr($element[$delta]['#format'], 1);
    }
    $element[$delta]['#separator'] = $this
      ->getSetting('separator');
    $element[$delta]['#last_separator'] = $this
      ->getSetting('last_separator');
  }
  return $element;
}