function claro_preprocess_table

Same name and namespace in other branches
  1. 8.9.x core/themes/claro/claro.theme \claro_preprocess_table()
  2. 10 core/themes/claro/claro.theme \claro_preprocess_table()
  3. 11.x core/themes/claro/claro.theme \claro_preprocess_table()

Implements template_preprocess_HOOK() for table.

1 call to claro_preprocess_table()
claro_preprocess_field_ui_table in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for field_ui_table.

File

core/themes/claro/claro.theme, line 1009

Code

function claro_preprocess_table(&$variables) {
    // Adding table sort indicator CSS class for inactive sort link.
    // @todo Revisit after https://www.drupal.org/node/3025726 or
    // https://www.drupal.org/node/1973418 is in.
    if (!empty($variables['header'])) {
        foreach ($variables['header'] as &$header_cell) {
            if ($header_cell['content'] instanceof Link) {
                $query = $header_cell['content']->getUrl()
                    ->getOption('query') ?: [];
                if (isset($query['order']) && isset($query['sort'])) {
                    $header_cell['attributes']->addClass('sortable-heading');
                }
            }
        }
    }
    // Mark the whole table and the first cells if rows are draggable.
    if (!empty($variables['rows'])) {
        $draggable_row_found = FALSE;
        foreach ($variables['rows'] as &$row) {
            
            /** @var \Drupal\Core\Template\Attribute $row['attributes'] */
            if (!empty($row['attributes']) && $row['attributes']->hasClass('draggable')) {
                if (!$draggable_row_found) {
                    $variables['attributes']['class'][] = 'draggable-table';
                    $draggable_row_found = TRUE;
                }
                reset($row['cells']);
                $first_cell_key = key($row['cells']);
                // The 'attributes' key is always here and it is an
                // \Drupal\Core\Template\Attribute.
                // @see template_preprocess_table();
                $row['cells'][$first_cell_key]['attributes']->addClass('tabledrag-cell');
                // Check that the first cell is empty or not.
                if (empty($row['cells'][$first_cell_key]) || empty($row['cells'][$first_cell_key]['content'])) {
                    $row['cells'][$first_cell_key]['attributes']->addClass('tabledrag-cell--only-drag');
                }
            }
        }
    }
}

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