You are here

public function Table::viewElements in Double Field 8.3

Same name and namespace in other branches
  1. 4.x src/Plugin/Field/FieldFormatter/Table.php \Drupal\double_field\Plugin\Field\FieldFormatter\Table::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/Table.php, line 87

Class

Table
Plugin implementations for 'table' formatter.

Namespace

Drupal\double_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $settings = $this
    ->getSettings();
  $table = [
    '#type' => 'table',
  ];
  $table['#attributes']['class'][] = 'double-field-table';
  if ($settings['first_column_label'] || $settings['second_column_label']) {
    if ($settings['number_column']) {
      $header[] = $settings['number_column_label'];
    }
    $header[] = $settings['first_column_label'];
    $header[] = $settings['second_column_label'];
    $table['#header'] = $header;
  }
  foreach ($items as $delta => $item) {
    $row = [];
    if ($settings['number_column']) {
      $row[]['#markup'] = $delta + 1;
    }
    foreach ([
      'first',
      'second',
    ] as $subfield) {
      if ($settings[$subfield]['hidden']) {
        $row[]['#markup'] = '';
      }
      else {
        $row[] = [
          '#theme' => 'double_field_subfield',
          '#settings' => $settings,
          '#subfield' => $item->{$subfield},
          '#index' => $subfield,
        ];
      }
    }
    $table[$delta] = $row;
  }
  $element[0] = $table;
  return $element;
}