Project

General

Profile

Paste
Download (2.06 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_sort_menu_hierarchy.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_sort_menu_hierarchy.
6
 */
7

    
8
/**
9
 * Sort in menu hierarchy order.
10
 *
11
 * Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9;
12
 * and optionally injects multiple joins to {menu_links} to sort by weight
13
 * and title as well.
14
 *
15
 * This is only really useful for the {menu_links} table.
16
 *
17
 * @ingroup views_sort_handlers
18
 */
19
class views_handler_sort_menu_hierarchy extends views_handler_sort {
20

    
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function option_definition() {
25
    $options = parent::option_definition();
26
    $options['sort_within_level'] = array('default' => FALSE);
27
    return $options;
28
  }
29

    
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function options_form(&$form, &$form_state) {
34
    parent::options_form($form, $form_state);
35
    $form['sort_within_level'] = array(
36
      '#type' => 'checkbox',
37
      '#title' => t('Sort within each hierarchy level'),
38
      '#description' => t('Enable this to sort the items within each level of the hierarchy by weight and title.  Warning: this may produce a slow query.'),
39
      '#default_value' => $this->options['sort_within_level'],
40
    );
41
  }
42

    
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function query() {
47
    $this->ensure_my_table();
48
    $max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
49
    for ($i = 1; $i <= $max_depth; ++$i) {
50
      if ($this->options['sort_within_level']) {
51
        $join = new views_join();
52
        $join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
53
        $menu_links = $this->query->add_table('menu_links', NULL, $join);
54
        $this->query->add_orderby($menu_links, 'weight', $this->options['order']);
55
        $this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
56
      }
57

    
58
      // We need this even when also sorting by weight and title, to make sure
59
      // that children of two parents with the same weight and title are
60
      // correctly separated.
61
      $this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
62
    }
63
  }
64

    
65
}