You are here

function gin_lb_preprocess_table in Gin Layout Builder 1.0.x

Implements template_preprocess_HOOK() for table.

1 call to gin_lb_preprocess_table()
gin_lb_preprocess_field_ui_table in ./gin_lb.module
Implements template_preprocess_HOOK() for field_ui_table.

File

./gin_lb.module, line 590
Provides hooks for gin_lb module.

Code

function gin_lb_preprocess_table(&$variables) {
  if (isset($variables['attributes']['class']) && in_array('glb-table', $variables['attributes']['class'], TRUE)) {

    // 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');
          }
        }
      }
    }
    if ($draggable_row_found) {
      $variables['#attached']['library'][] = 'gin/gin_tabledrag';
    }
  }
}