Commit 9f5d2d5f authored by mustanggb's avatar mustanggb Committed by Allan Chappell
Browse files

Issue #2187323 by MustangGB: Enable Natural Sorts for Clickable Table Headers

parent c4133883
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
<?php

class views_natural_sort_handler_field_node extends views_handler_field_node {
  /**
   * Called to determine what to tell the clicksorter.
   */
  public function click_sort($order) {
    // Determine if this field has natural sort enabled.
    $natural_sort = 0;
    $id = $this->options['id'];
    if (isset($this->view->style_options['info'][$id]['natural_sort'])) {
      $natural_sort = $this->view->style_options['info'][$id]['natural_sort'];
    }

    // If this field isn't being used as a Natural Sort Field, move along
    // nothing to see here.
    if (!$natural_sort) {
      parent::click_sort($order);
      return;
    }

    // Add the Views Natural Sort table for this field.
    $vns_alias = 'vns_' . $this->table_alias;
    if (empty($this->query->relationships[$vns_alias])) {
      $this->ensure_my_table();
      $vns_alias = $this->query->add_relationship('vns_' . $this->table_alias, $this->natural_sort_join(), $this->table, $this->relationship);
    }
    $this->query->add_orderby($vns_alias, 'content', $order);
  }

  /**
   * Helper function that creates a join to the views natural sort table.
   */
  public function natural_sort_join() {
    $join = new views_join();
    $table_data = views_fetch_data($this->table);
    $join->definition = array(
      'table' => 'views_natural_sort',
      'field' => 'eid',
      'left_field' => $table_data['table']['base']['field'],
      'left_table' => $this->table,
      'extra' => array(
        array(
          'field' => 'entity_type',
          'value' => $table_data['table']['entity type'],
        ),
        array(
          'field' => 'field',
          'value' => $this->real_field,
        ),
      ),
    );
    $join->construct();
    $join->adjusted = TRUE;
    return $join;
  }
}
+19 −3
Original line number Diff line number Diff line
<?php

class views_natural_sort_plugin_style_table extends views_plugin_style_table {
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $handlers = $this->display->handler->get_handlers('field');

    $form['#theme'] = 'views_natural_sort_style_plugin_table';
    $form['vns_test'] = array(
      '#markup' => "<h1>VNS TEST</h1>",

    $columns = $this->sanitize_columns($this->options['columns']);

    foreach ($columns as $field => $column) {
      $safe = str_replace(array('][', '_', ' '), '-', $field);
      // the $id of the column for dependency checking.
      $id = 'edit-style-options-columns-' . $safe;

      if ($handlers[$field]->click_sortable()) {
        $form['info'][$field]['natural_sort'] = array(
          '#type' => 'checkbox',
          '#default_value' => !empty($this->options['info'][$field]['natural_sort']),
          '#dependency' => array($id => array($field)),
        );
      }
    }
  }
}
+1 −0
Original line number Diff line number Diff line
@@ -8,5 +8,6 @@ core = 7.x

; Views handlers
files[] = handlers/views_natural_sort_handler_sort.inc
files[] = handlers/views_natural_sort_handler_field_node.inc
files[] = plugins/views_natural_sort_plugin_style_table.inc
files[] = views_natural_sort.test
+7 −4
Original line number Diff line number Diff line
@@ -74,7 +74,8 @@ function views_natural_sort_theme() {
}

/**
 * Theme the form for the table style plugin
 * Theme the form for the table style plugin, based on
 * theme_views_ui_style_plugin_table().
 */
function theme_views_natural_sort_style_plugin_table($variables) {
  $form = $variables['form'];
@@ -99,7 +100,7 @@ function theme_views_natural_sort_style_plugin_table($variables) {
      'align' => 'center',
    ),
    array(
      'data' => t('Sort Naturally'),
      'data' => t('Sort naturally'),
      'align' => 'center',
    ),
    array(
@@ -127,8 +128,10 @@ function theme_views_natural_sort_style_plugin_table($variables) {
        'data' => drupal_render($form['default'][$id]),
        'align' => 'center',
      );
      // TODO: ADD Natural Logic here.
      $row[] = '';
      $row[] = array(
        'data' => drupal_render($form['info'][$id]['natural_sort']),
        'align' => 'center',
      );
    }
    else {
      $row[] = '';
+7 −2
Original line number Diff line number Diff line
@@ -32,6 +32,13 @@ function views_natural_sort_views_data_alter(&$views_data) {
        // We've verified that this is a perfect candidate to add our sort
        // handler to.
        $view_field_data['sort']['handler'] = 'views_natural_sort_handler_sort';

        switch ($view_field_data['field']['handler']) {
          case 'views_handler_field_node':
            $view_field_data['field']['handler'] = 'views_natural_sort_handler_field_node';
            break;
          // @todo: Add support for other field handlers.
        }
      }
    }
  }
@@ -41,9 +48,7 @@ function views_natural_sort_views_data_alter(&$views_data) {
 * Implements hook_views_plugins_alter().
 */
function views_natural_sort_views_plugins_alter(&$plugins) {
  // Add apachesolr to the base of the node row plugin.
  $plugins['style']['table']['handler'] = 'views_natural_sort_plugin_style_table';
  $plugins['style']['table']['path'] = drupal_get_path('module', 'views_natural_sort') . '/plugins';
  $plugins['style']['table']['file'] = 'views_natural_sort_plugin_style_table.inc';
  dpm($plugins);
}