You are here

function tablesorter_attach in Tablesorter 7

Helper function to configure tablesorter on a table's render array.

This function returns by reference.

Parameters

array $table: The table to add tablesorter to.

2 calls to tablesorter_attach()
tablesorter_preprocess_table in ./tablesorter.module
Implements hook_preprocess_HOOK().
template_preprocess_tablesorter_view in ./tablesorter.module
Preprocessor function for the tablesorter style.

File

./tablesorter.module, line 273
Tablesorter.

Code

function tablesorter_attach(array &$table) {

  // Make sure to only attach this once.
  if (!isset($table['#tablesorter_processed'])) {
    $table['#tablesorter_processed'] = TRUE;

    // Add a class and library to the table.
    $table['#attached']['libraries_load'][] = array(
      'tablesorter',
    );
    $table['#attributes']['class'][] = 'tablesorter';

    // Add this module's javascript.
    drupal_add_js(drupal_get_path('module', 'tablesorter') . '/tablesortervar.js');

    // Set the theme.
    $settings = array();
    $theme = variable_get('tablesorter_theme');
    if ($theme) {
      $settings['tablesorter']['theme'] = $theme;
    }

    // Add widgets.
    $widgets = variable_get('tablesorter_widgets');
    foreach ($widgets as $widget => $value) {
      if (!$value) {
        unset($widgets[$widget]);
      }
    }
    if ($widgets) {
      $settings['tablesorter']['widgets'] = array_keys($widgets);

      // Get stripe classes.
      if (in_array('zebra', $widgets)) {
        $settings['tablesorter']['zebra']['odd'] = variable_get('tablesorter_zebra_odd_class', 'odd');
        $settings['tablesorter']['zebra']['even'] = variable_get('tablesorter_zebra_even_class', 'even');
      }
    }

    // Add settings.
    drupal_add_js($settings, 'setting');

    // Allow other modules to make changes to sorted tables.
    drupal_alter(__FUNCTION__, $table);
  }
}