You are here

public function Table::sanitizeColumns in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/style/Table.php \Drupal\views\Plugin\views\style\Table::sanitizeColumns()
  2. 10 core/modules/views/src/Plugin/views/style/Table.php \Drupal\views\Plugin\views\style\Table::sanitizeColumns()

Normalize a list of columns based upon the fields that are available. This compares the fields stored in the style handler to the list of fields actually in the view, removing fields that have been removed and adding new fields in their own column.

  • Each field must be in a column.
  • Each column must be based upon a field, and that field is somewhere in the column.
  • Any fields not currently represented must be added.
  • Columns must be re-ordered to match the fields.

Parameters

$columns: An array of all fields; the key is the id of the field and the value is the id of the column the field should be in.

$fields: The fields to use for the columns. If not provided, they will be requested from the current display. The running render should send the fields through, as they may be different than what the display has listed due to access control or other changes.

Return value

array An array of all the sanitized columns.

1 call to Table::sanitizeColumns()
Table::buildOptionsForm in core/modules/views/src/Plugin/views/style/Table.php
Render the given style.

File

core/modules/views/src/Plugin/views/style/Table.php, line 166

Class

Table
Style plugin to render each item as a row in a table.

Namespace

Drupal\views\Plugin\views\style

Code

public function sanitizeColumns($columns, $fields = NULL) {
  $sanitized = [];
  if ($fields === NULL) {
    $fields = $this->displayHandler
      ->getOption('fields');
  }

  // Preconfigure the sanitized array so that the order is retained.
  foreach ($fields as $field => $info) {

    // Set to itself so that if it isn't touched, it gets column
    // status automatically.
    $sanitized[$field] = $field;
  }
  foreach ($columns as $field => $column) {

    // first, make sure the field still exists.
    if (!isset($sanitized[$field])) {
      continue;
    }

    // If the field is the column, mark it so, or the column
    // it's set to is a column, that's ok
    if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
      $sanitized[$field] = $column;
    }

    // Since we set the field to itself initially, ignoring
    // the condition is ok; the field will get its column
    // status back.
  }
  return $sanitized;
}