You are here

function claro_preprocess_table in Drupal 8

Same name and namespace in other branches
  1. 9 core/themes/claro/claro.theme \claro_preprocess_table()
  2. 10 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 891
Functions to support theming in the Claro theme.

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) {

      // For 8.6.x and below.
      // @todo Remove this after 8.6.x is out of support.
      if ($header_cell['content'] instanceof GeneratedLink) {
        $dom_doc = Html::load($header_cell['content']
          ->getGeneratedLink());
        $anchors = $dom_doc
          ->getElementsByTagName('a');
        if (!empty($anchors)) {
          foreach ($anchors as $anchor) {
            $anchor_href = $anchor
              ->getAttribute('href');
            $parsed_url = UrlHelper::parse($anchor_href);
            $query = !empty($parsed_url['query']) ? $parsed_url['query'] : [];
            if (isset($query['order']) && isset($query['sort'])) {
              $header_cell['attributes']
                ->addClass('sortable-heading');
            }
          }
        }
      }

      // For 8.7.x and above.
      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');
        }
      }
    }
  }
}