You are here

public function HtmlList::viewElements in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/HtmlList.php \Drupal\double_field\Plugin\Field\FieldFormatter\HtmlList::viewElements()

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/HtmlList.php, line 69

Class

HtmlList
Plugin implementations for 'html_list' formatter.

Namespace

Drupal\double_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) : array {
  $field_settings = $this
    ->getFieldSettings();
  $settings = $this
    ->getSettings();
  $field_name = $items
    ->getName();
  if ($settings['list_type'] == 'dl') {
    $element[0] = [
      '#theme' => 'double_field_definition_list',
      '#items' => $items,
      '#settings' => $settings,
      '#field_settings' => $field_settings,
      '#field_name' => $field_name,
    ];
  }
  else {
    $list_items = [];
    foreach ($items as $delta => $item) {
      $list_items[$delta] = [
        '#theme' => 'double_field_item',
        '#item' => $item,
        '#settings' => $settings,
        '#field_settings' => $field_settings,
        '#field_name' => $field_name,
      ];
      if ($settings['inline']) {
        $list_items[$delta]['#wrapper_attributes'] = [];
        if (!isset($item->_attributes)) {
          $item->_attributes = [];
        }
        $list_items[$delta]['#wrapper_attributes'] += $item->_attributes;
        $list_items[$delta]['#wrapper_attributes']['class'][] = 'container-inline';
      }
    }
    $element[0] = [
      '#theme' => 'item_list',
      '#list_type' => $settings['list_type'],
      '#items' => $list_items,
      '#context' => [
        'double_field' => [
          'field_name' => $field_name,
        ],
      ],
    ];
  }
  $element[0]['#attributes']['class'][] = 'double-field-list';
  return $element;
}