views_handler_sort_menu_hierarchy.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  19. * {@inheritdoc}
  20. */
  21. public function option_definition() {
  22. $options = parent::option_definition();
  23. $options['sort_within_level'] = array('default' => FALSE);
  24. return $options;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function options_form(&$form, &$form_state) {
  30. parent::options_form($form, $form_state);
  31. $form['sort_within_level'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Sort within each hierarchy level'),
  34. '#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.'),
  35. '#default_value' => $this->options['sort_within_level'],
  36. );
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function query() {
  42. $this->ensure_my_table();
  43. $max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
  44. for ($i = 1; $i <= $max_depth; ++$i) {
  45. if ($this->options['sort_within_level']) {
  46. $join = new views_join();
  47. $join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
  48. $menu_links = $this->query->add_table('menu_links', NULL, $join);
  49. $this->query->add_orderby($menu_links, 'weight', $this->options['order']);
  50. $this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
  51. }
  52. // We need this even when also sorting by weight and title, to make sure
  53. // that children of two parents with the same weight and title are
  54. // correctly separated.
  55. $this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
  56. }
  57. }
  58. }