You are here

public function EntityToTableRenderer::getTable in Reference Table Formatter 8

Same name and namespace in other branches
  1. 2.0.x src/EntityToTableRenderer.php \Drupal\reference_table_formatter\EntityToTableRenderer::getTable()

Render the entities to a table.

Parameters

string $entity_type: The entity type of the given entities.

string $bundle: The bundle that the entities are composed of.

array $entities: An array of entities to put into the table.

array $settings: The settings array from the field formatter base containing keys:

  • view_mode: The target view mode to render the field settings from.
  • show_entity_label: If we should display the entity label.
  • empty_cell_value: The value to show in empty cells.

Return value

array A table renderable array.

Overrides EntityToTableRendererInterface::getTable

File

src/EntityToTableRenderer.php, line 48

Class

EntityToTableRenderer
A service for turning entities into tables.

Namespace

Drupal\reference_table_formatter

Code

public function getTable($type, $bundle, array $entities, array $settings) {
  $table_rows = $this
    ->getPreparedRenderedEntities($type, $bundle, $entities, $settings);
  $table_columns = $this
    ->getTableColumns($table_rows);
  $table = [
    '#theme' => 'table',
  ];
  if (!$settings['hide_header']) {
    $table['#header'] = $table_columns;
  }
  foreach ($table_rows as $table_row) {
    $row =& $table['#rows'][];
    foreach ($table_columns as $field_name => $field_title) {

      // If the field we need to render exists on the entity, render it
      // without a title, otherwise fill the cell anyway with a default.
      if (isset($table_row[$field_name])) {
        $table_row[$field_name]['#label_display'] = 'hidden';
        $row[] = $this->renderer
          ->render($table_row[$field_name]);
      }
      else {
        $row[] = $settings['empty_cell_value'];
      }
    }
  }
  $cache_metadata = new CacheableMetadata();
  foreach ($entities as $entity) {
    $cache_metadata
      ->addCacheableDependency($entity);
    $cache_metadata
      ->addCacheableDependency($entity
      ->access('view', NULL, TRUE));
  }
  $cache_metadata
    ->applyTo($table);
  return $table;
}