You are here

public function ParagraphsTableFormatter::getData in Paragraphs table 8

1 call to ParagraphsTableFormatter::getData()
ParagraphsTableFormatter::viewElements in src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php, line 564

Class

ParagraphsTableFormatter
Plugin implementation of the 'paragraphs_table_formatter' formatter.

Namespace

Drupal\paragraphs_table\Plugin\Field\FieldFormatter

Code

public function getData($type, $bundle, $entities, $fields, &$notEmptyColumn, $view_mode = 'default') {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display');

  // When a display renderer doesn't exist, fall back to the default.
  $renderer = $storage
    ->load(implode('.', [
    $type,
    $bundle,
    $view_mode,
  ]));
  $setting = $this
    ->getSettings();
  $data = [];
  foreach ($entities as $delta => $entitie) {
    $table_entity = $renderer
      ->build($entitie);
    foreach ($fields as $field_name => $field) {
      $table_entity[$field_name]['#label_display'] = 'hidden';
      $value = trim(strip_tags(render($table_entity[$field_name])));
      if (in_array($field
        ->getType(), [
        'integer',
        'list_integer',
        'number_integer',
      ])) {
        $value = (int) $value;
      }
      if (in_array($field
        ->getType(), [
        'boolean',
      ])) {
        $list_value = $table_entity[$field_name]["#items"]
          ->getValue();
        $value = (int) $list_value[0]['value'];
      }
      if (in_array($field
        ->getType(), [
        'decimal',
        'list_decimal',
        'number_decimal',
        'float',
        'list_float',
        'number_float',
      ])) {
        $value = (double) $value;
      }
      if (!empty($value)) {
        $notEmptyColumn[$field_name] = TRUE;
      }
      elseif (!empty($setting["empty_cell_value"])) {
        $value = $setting["empty_cell_value"];
      }
      $data[$delta][] = $value;
    }
  }
  return $data;
}