You are here

function theme_gc_tableselect in GatherContent 8

Returns HTML for a table with radio buttons or checkboxes.

See Drupal core's theme_tableselect() as reference.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing the properties and children of the tableselect element. Properties used: #header, #options, #empty, and #js_select. The #options property is an array of selection options; each array element of #options is an array of properties. These properties can include #attributes, which is added to the table row's HTML attributes; see theme_table().

Return value

string The rendered markup.

1 theme call to theme_gc_tableselect()
gc_element_info in ./gc.theme.inc
Implements hook_element_info().

File

./gc.theme.inc, line 81
Form elements and theme functions for GatherContent module.

Code

function theme_gc_tableselect(array $variables) {
  $output = '';
  $element = $variables['element'];
  $gc_module_path = drupal_get_path('module', 'gc');

  // Libraries module and tablesorter plugin are optional.
  // If tablesorter available, add it to the scope.
  if (\Drupal::moduleHandler()
    ->moduleExists('libraries')) {
    $library = libraries_detect('tablesorter-mottie');
    if ($library['installed'] && $element['#tablesorter']) {
      $element['#attributes']['class'][] = 'tablesorter-enabled';

      // @FIXME
      // The Assets API has totally changed. CSS, JavaScript, and libraries are now
      // attached directly to render arrays using the #attached property.
      //
      //
      // @see https://www.drupal.org/node/2169605
      // @see https://www.drupal.org/node/2408597
      // drupal_add_js($gc_module_path . '/js/gc-tablesorter.js');
      libraries_load('tablesorter-mottie');
    }
  }
  $rows = array();
  $header = $element['#header'];
  if (!empty($element['#options'])) {

    // Generate a table row for each selectable item in #options.
    foreach (\Drupal\Core\Render\Element::children($element) as $key) {
      $row = array();
      $row['data'] = array();
      if (isset($element['#options'][$key]['#attributes'])) {
        $row += $element['#options'][$key]['#attributes'];
      }

      // Render the checkbox / radio element.
      $row['data'][] = \Drupal::service("renderer")
        ->render($element[$key]);

      // As theme_table only maps header and row columns by order, create the
      // correct order by iterating over the header fields.
      foreach ($element['#header'] as $fieldname => $title) {
        $row['data'][] = $element['#options'][$key][$fieldname];
      }
      $rows[] = $row;
    }
  }

  // Add an empty header or a "Select all" checkbox to provide room for the
  // checkboxes/radios in the first table column.
  if ($element['#js_select']) {

    // Add a "Select all" checkbox.
    // @FIXME
    // The Assets API has totally changed. CSS, JavaScript, and libraries are now
    // attached directly to render arrays using the #attached property.
    //
    //
    // @see https://www.drupal.org/node/2169605
    // @see https://www.drupal.org/node/2408597
    // drupal_add_js($gc_module_path . '/js/gc-tableselect.js');
    array_unshift($header, array(
      'class' => array(
        'select-all',
      ),
      'data-sorter' => 'false',
    ));
  }
  else {

    // Add an empty header when radio buttons are displayed or a "Select all"
    // checkbox is not desired.
    array_unshift($header, '');
  }
  if (!empty($element['#filterwrapper']) && is_array($element['#filterwrapper'])) {
    $filterwrapper = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'gc-table--header',
        ),
      ),
    );
    foreach ($element['#filterwrapper'] as $key => $classes) {
      $filterwrapper[$key] = array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => is_array($classes) ? $classes : array(
            $classes,
          ),
        ),
      );
    }
    $output .= \Drupal::service("renderer")
      ->render($filterwrapper);
  }
  if (!empty($element['#filterdescription'])) {
    $filterdescription = array(
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $element['#filterdescription'],
      '#attributes' => array(
        'class' => array(
          'description',
        ),
      ),
    );
    $output .= \Drupal::service("renderer")
      ->render($filterdescription);
  }

  // @FIXME
  // theme() has been renamed to _theme() and should NEVER be called directly.
  // Calling _theme() directly can alter the expected output and potentially
  // introduce security issues (see https://www.drupal.org/node/2195739). You
  // should use renderable arrays instead.
  //
  //
  // @see https://www.drupal.org/node/2195739
  // $output .= theme('table', array(
  //     'header' => $header,
  //     'rows' => $rows,
  //     'empty' => $element['#empty'],
  //     'attributes' => $element['#attributes'],
  //   ));
  return $output;
}