function template_preprocess_table

Same name and namespace in other branches
  1. 10 core/includes/theme.inc \template_preprocess_table()
  2. 9 core/includes/theme.inc \template_preprocess_table()
  3. 8.9.x core/includes/theme.inc \template_preprocess_table()

Prepares variables for table templates.

Default template: table.html.twig.

Parameters

array $variables: An associative array containing:

  • header: An array containing the table headers. Each element of the array can be either a localized string or an associative array with the following keys:

    • data: The localized title of the table column, as a string or render array.
    • field: The database field represented in the table column (required if user is to be able to sort on this column).
    • sort: A default sort order for this column ("asc" or "desc"). Only one column should be given a default sort order because table sorting only applies to one column at a time.
    • initial_click_sort: Set the initial sort of the column when clicked. Defaults to "asc".
    • class: An array of values for the 'class' attribute. In particular, the least important columns that can be hidden on narrow and medium width screens should have a 'priority-low' class, referenced with the RESPONSIVE_PRIORITY_LOW constant. Columns that should be shown on medium+ wide screens should be marked up with a class of 'priority-medium', referenced by with the RESPONSIVE_PRIORITY_MEDIUM constant. Themes may hide columns with one of these two classes on narrow viewports to save horizontal space.
    • Any HTML attributes, such as "colspan", to apply to the column header cell.
  • rows: An array of table rows. Every row is an array of cells, or an associative array with the following keys:

    • data: An array of cells.
    • Any HTML attributes, such as "class", to apply to the table row.
    • no_striping: A Boolean indicating that the row should receive no 'even / odd' styling. Defaults to FALSE.

    Each cell can be either a string or an associative array with the following keys:

    • data: The string or render array to display in the table cell.
    • header: Indicates this cell is a header.
    • Any HTML attributes, such as "colspan", to apply to the table cell.

    Here's an example for $rows:

$rows = [
  // Simple row
[
    'Cell 1',
    'Cell 2',
    'Cell 3',
  ],
  // Row with attributes on the row and some of its cells.
[
    'data' => [
      'Cell 1',
      [
        'data' => 'Cell 2',
        'colspan' => 2,
      ],
    ],
    'class' => [
      'funky',
    ],
  ],
];
  • footer: An array of table rows which will be printed within a <tfoot> tag, in the same format as the rows element (see above).
  • attributes: An array of HTML attributes to apply to the table tag.
  • caption: A localized string to use for the <caption> tag.
  • colgroups: An array of column groups. Each element of the array can be either:

    • An array of columns, each of which is an associative array of HTML attributes applied to the <col> element.
    • An array of attributes applied to the <colgroup> element, which must include a "data" attribute. To add attributes to <col> elements, set the "data" attribute with an array of columns, each of which is an associative array of HTML attributes.

    Here's an example for $colgroup:

$colgroup = [
  // <colgroup> with one <col> element.
[
    [
      'class' => [
        'funky',
      ],
    ],
  ],
  // <colgroup> with attributes and inner <col> elements.
[
    'data' => [
      [
        'class' => [
          'funky',
        ],
      ],
    ],
    'class' => [
      'jazzy',
    ],
  ],
];

These optional tags are used to group and set properties on columns within a table. For example, one may easily group three columns and apply same background style to all.

  • sticky: Use a "sticky" table header.
  • empty: The message to display in an extra row if table does not have any rows.

Deprecated

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

See also

https://www.drupal.org/node/3504125

File

core/includes/theme.inc, line 593

Code

function template_preprocess_table(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ThemePreprocess::class)->preprocessTable($variables);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.