You are here

protected function ParagraphsTableFormatter::getPreparedRenderedEntities in Paragraphs table 8

Prepare all of the given entities for rendering with applicable fields.

1 call to ParagraphsTableFormatter::getPreparedRenderedEntities()
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 668

Class

ParagraphsTableFormatter
Plugin implementation of the 'paragraphs_table_formatter' formatter.

Namespace

Drupal\paragraphs_table\Plugin\Field\FieldFormatter

Code

protected function getPreparedRenderedEntities($type, $bundle, $entities, $fields, &$notEmptyColumn, $view_mode = 'default', $show_operation = FALSE) {
  $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,
  ]));
  if (empty($renderer)) {
    $renderer = $storage
      ->load(implode('.', [
      $type,
      $bundle,
      'default',
    ]));
  }
  $setting = $this
    ->getSettings();
  $rows = [];
  foreach ($entities as $delta => $entity) {
    $entity_bundle = $entity
      ->bundle();
    if ($bundle != $entity_bundle) {
      continue;
    }
    $table_entity = $renderer
      ->build($entity);
    $paragraphs_id = $entity
      ->get('id')->value;
    foreach ($fields as $field_name => $field) {
      $table_entity[$field_name]['#label_display'] = 'hidden';
      $value = render($table_entity[$field_name]);
      if (!empty($value)) {
        $notEmptyColumn[$field_name] = TRUE;
      }
      elseif (!empty($setting["empty_cell_value"])) {
        $value = $setting["empty_cell_value"];
      }
      $rows[$delta]['data'][$field_name] = $value;
      $field_type = $field
        ->getType();

      // Add data order for sort.
      if ($setting['mode'] == 'datatables' && in_array($field_type, [
        'timestamp',
        'datetime',
        'daterange',
        'datestamp',
        'smartdate',
        'number_decimal',
        'decimal',
        'number_float',
        'list_float',
      ]) && !empty($table_entity[$field_name]["#items"])) {
        $list_value = $table_entity[$field_name]["#items"]
          ->getValue();
        $value_order = $list_value[0]['value'];
        if (!is_numeric($value_order)) {
          $value_order = strtotime($value_order);
        }
        $rows[$delta]['data'][$field_name] = [
          'data' => $value,
          'data-order' => $value_order,
        ];
      }
    }
    if ($show_operation) {
      $parent = $entity
        ->getParentEntity();
      $destination = implode('/', [
        $parent
          ->getEntityTypeId(),
        $parent
          ->id(),
      ]);
      $operation = $this
        ->paragraphsTableLinksAction($paragraphs_id, $destination);
      $rows[$delta]['data']['operation'] = $operation;
    }
    $rows[$delta]['data-quickedit-entity-id'] = "{$type}/{$paragraphs_id}";
    $rows[$delta]["data-id"] = $paragraphs_id;
    $rows[$delta]["data-type"] = $type;
  }
  return $rows;
}