views_handler_sort_menu_hierarchy.inc

  1. 3.x handlers/views_handler_sort_menu_hierarchy.inc
  2. 2.x handlers/views_handler_sort_menu_hierarchy.inc

Definition of views_handler_sort_menu_hierarchy.

File

handlers/views_handler_sort_menu_hierarchy.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_sort_menu_hierarchy.
  5. */
  6. /**
  7. * Sort in menu hierarchy order.
  8. *
  9. * Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9;
  10. * and optionally injects multiple joins to {menu_links} to sort by weight
  11. * and title as well.
  12. *
  13. * This is only really useful for the {menu_links} table.
  14. *
  15. * @ingroup views_sort_handlers
  16. */
  17. class views_handler_sort_menu_hierarchy extends views_handler_sort {
  18. function option_definition() {
  19. $options = parent::option_definition();
  20. $options['sort_within_level'] = array('default' => FALSE);
  21. return $options;
  22. }
  23. function options_form(&$form, &$form_state) {
  24. parent::options_form($form, $form_state);
  25. $form['sort_within_level'] = array(
  26. '#type' => 'checkbox',
  27. '#title' => t('Sort within each hierarchy level'),
  28. '#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.'),
  29. '#default_value' => $this->options['sort_within_level'],
  30. );
  31. }
  32. function query() {
  33. $this->ensure_my_table();
  34. $max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
  35. for ($i = 1; $i <= $max_depth; ++$i) {
  36. if ($this->options['sort_within_level']) {
  37. $join = new views_join();
  38. $join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
  39. $menu_links = $this->query->add_table('menu_links', NULL, $join);
  40. $this->query->add_orderby($menu_links, 'weight', $this->options['order']);
  41. $this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
  42. }
  43. // We need this even when also sorting by weight and title, to make sure
  44. // that children of two parents with the same weight and title are
  45. // correctly separated.
  46. $this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
  47. }
  48. }
  49. }