From ee24189a67fc74a77664edc3a43126992abf9217 Mon Sep 17 00:00:00 2001 From: Jan Grzegorowski Date: Thu, 3 Jul 2014 15:45:33 +0200 Subject: [PATCH] add menu ordering in sitemap module --- site-map.tpl.php | 36 +------------------- site_map.admin.inc | 34 +++++++++++++++++-- site_map.admin.js | 29 ++++++++++++++++ site_map.module | 84 +++++++++++++++++++++------------------------ site_map.theme.inc | 99 ++++++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 182 insertions(+), 100 deletions(-) create mode 100644 sites/all/modules/site_map/site_map.admin.js diff --git a/site-map.tpl.php b/site-map.tpl.php index 20ed0e6..2f37ed6 100644 --- a/site-map.tpl.php +++ b/site-map.tpl.php @@ -32,41 +32,7 @@ - -
- -
- - - -
- -
- - - -
- -
- - - -
- -
- - - -
- -
- - - -
- -
- +
diff --git a/site_map.admin.inc b/site_map.admin.inc index 58053bc..f76373d 100644 --- a/site_map.admin.inc +++ b/site_map.admin.inc @@ -31,12 +31,14 @@ function site_map_admin_settings_form() { '#type' => 'fieldset', '#title' => t('Site map content'), ); + $site_map_ordering = array(); $form['site_map_content']['site_map_show_front'] = array( '#type' => 'checkbox', '#title' => t('Show front page'), '#default_value' => variable_get('site_map_show_front', 1), '#description' => t('When enabled, this option will include the front page in the site map.'), ); + $site_map_ordering['front'] = t('Front page'); $form['site_map_content']['site_map_show_titles'] = array( '#type' => 'checkbox', '#title' => t('Show titles'), @@ -50,6 +52,7 @@ function site_map_admin_settings_form() { '#default_value' => variable_get('site_map_show_blogs', 1), '#description' => t('When enabled, this option will show the 10 most active blog authors.'), ); + $site_map_ordering['blogs'] = t('Active blog authors'); } if (module_exists('book')) { $book_options = array(); @@ -69,11 +72,13 @@ function site_map_admin_settings_form() { '#default_value' => variable_get('site_map_books_expanded', 1), '#description' => t('When enabled, this option will show all children pages for each book.'), ); + $site_map_ordering['books'] = t('Books'); } - $menu_options = array(); $menu_options = menu_get_menus(); - + foreach ($menu_options as $menu_id => $menu_title) { + $site_map_ordering['menus_' . $menu_id] = $menu_title; + } $form['site_map_content']['site_map_show_menus'] = array( '#type' => 'checkboxes', '#title' => t('Menus to include in the site map'), @@ -95,11 +100,13 @@ function site_map_admin_settings_form() { '#default_value' => variable_get('site_map_show_faq', 0), '#description' => t('When enabled, this option will include the content from the FAQ module in the site map.'), ); + $site_map_ordering['faq'] = t('FAQ content'); } $vocab_options = array(); if (module_exists('taxonomy')) { foreach (taxonomy_get_vocabularies() as $vocabulary) { $vocab_options[$vocabulary->machine_name] = $vocabulary->name; + $site_map_ordering['vocabularies_' . $vocabulary->machine_name] = $vocabulary->name; } } $form['site_map_content']['site_map_show_vocabularies'] = array( @@ -109,6 +116,29 @@ function site_map_admin_settings_form() { '#options' => $vocab_options, '#multiple' => TRUE, ); + $form['site_map_content']['site_map_order'] = array( + '#type' => 'item', + '#title' => t('Site map order'), + '#theme' => 'site_map_order', + ); + $site_map_order_defaults = variable_get('site_map_order', array()); + foreach ($site_map_ordering as $content_id => $content_title) { + $form['site_map_content']['site_map_order'][$content_id] = array( + 'content' => array( + '#markup' => $content_title, + ), + 'weight' => array( + '#type' => 'weight', + '#title' => t('Weight for @title', array('@title' => $content_title)), + '#title_display' => 'invisible', + '#delta' => 50, + '#default_value' => isset($site_map_order_defaults[$content_id])? $site_map_order_defaults[$content_id] : -50, + '#parents' => array('site_map_order', $content_id), + ), + '#weight' => isset($site_map_order_defaults[$content_id])? $site_map_order_defaults[$content_id] : -50, + ); + } + $form['#attached']['js'][] = drupal_get_path('module', 'site_map') . '/site_map.admin.js'; $form['site_map_taxonomy_options'] = array( '#type' => 'fieldset', diff --git a/site_map.admin.js b/site_map.admin.js new file mode 100644 index 0000000..6594c95 --- /dev/null +++ b/site_map.admin.js @@ -0,0 +1,29 @@ +(function ($) { + +Drupal.behaviors.siteMapContent = { + attach: function (context, settings) { + $('#edit-site-map-content input.form-checkbox', context).once('site-map-content', function () { + var $checkbox = $(this); + // Retrieve the tabledrag row belonging to this content. + var $row = $('#' + $checkbox.attr('id').replace(/-show-/, '-order-'), context).closest('tr'); + + // Bind click handler to this checkbox to conditionally show and hide the + // filter's tableDrag row. + $checkbox.bind('click.siteMapUpdate', function () { + if ($checkbox.is(':checked')) { + $row.show(); + } + else { + $row.hide(); + } + // Restripe table after toggling visibility of table row. + Drupal.tableDrag['site-map-order'].restripeTable(); + }); + + // Trigger our bound click handler to update elements to initial state. + $checkbox.triggerHandler('click.siteMapUpdate'); + }); + } +}; + +})(jQuery); diff --git a/site_map.module b/site_map.module index 38bb070..b7ec93c 100644 --- a/site_map.module +++ b/site_map.module @@ -64,6 +64,10 @@ function site_map_theme() { 'variables' => array(), 'file' => 'site_map.theme.inc', ), + 'site_map_order' => array( + 'render element' => 'element', + 'file' => 'site_map.theme.inc', + ), ); } @@ -436,45 +440,37 @@ function _site_map_books() { * @return string * Returns HTML string of site map for menus. */ -function _site_map_menus() { +function _site_map_menus($mid) { $output = ''; + $class = array(); $options = array(); - $mids = array_filter(variable_get('site_map_show_menus', array())); - - if (!empty($mids)) { - foreach ($mids as $mid) { - $class = array(); - $menu = menu_load($mid); - // Use menu_tree_all_data to retrieve the expanded tree. - $tree = menu_tree_all_data($mid); - if (module_exists('i18n_menu')) { - $tree = i18n_menu_localize_tree($tree, $GLOBALS['language']->language); - } - // Add an alter hook so other modules can manipulate the menu tree prior to rendering - $alter_mid = preg_replace('/[^a-z0-9_]+/', '_', $mid); - drupal_alter(array('site_map_menu_tree', 'site_map_menu_tree_' . $alter_mid), $tree, $menu); - - $menu_display = _site_map_menu_tree_output($tree); - $menu_html = drupal_render($menu_display); - if (!empty($menu_html)) { - $title = t($menu['title']); - if (module_exists('i18n_string')) { - $m_array = array('menu', 'menu', $menu['menu_name'], 'title'); - $title = i18n_string_plain($m_array, $title); - } - _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE); - - $class[] = 'site-map-box-menu'; - $class[] = 'site-map-box-menu-' . $mid; - $attributes = array('class' => $class); - $output .= theme('site_map_box', array( - 'title' => $title, - 'content' => $menu_html, - 'attributes' => $attributes, - 'options' => $options, - )); + if (!empty($mid)) { + $menu = menu_load($mid); + // Use menu_tree_all_data to retrieve the expanded tree. + $tree = menu_tree_all_data($mid); + if (module_exists('i18n_menu')) { + $tree = i18n_menu_localize_tree($tree, $GLOBALS['language']->language); + } + $menu_display = _site_map_menu_tree_output($tree); + $menu_html = drupal_render($menu_display); + if (!empty($menu_html)) { + $title = t($menu['title']); + if (module_exists('i18n_string')) { + $m_array = array('menu', 'menu', $menu['menu_name'], 'title'); + $title = i18n_string_plain($m_array, $title); } + _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE); + + $class[] = 'site-map-box-menu'; + $class[] = 'site-map-box-menu-' . $mid; + $attributes = array('class' => $class); + $output .= theme('site_map_box', array( + 'title' => $title, + 'content' => $menu_html, + 'attributes' => $attributes, + 'options' => $options, + )); } } @@ -515,21 +511,17 @@ function _site_map_faq() { * @return string * Returns HTML string of site map for taxonomies. */ -function _site_map_taxonomys() { +function _site_map_taxonomys($voc) { $output = ''; $options = array(); - $vnames = array_filter(variable_get('site_map_show_vocabularies', array())); - if (module_exists('taxonomy') && !empty($vnames)) { - $result = db_query('SELECT vid, name, description FROM {taxonomy_vocabulary} WHERE machine_name IN (:vnames) ORDER BY weight ASC, name', array(':vnames' => $vnames)); - foreach ($result as $voc) { - if (module_exists('i18n_taxonomy')) { - $voc->name = i18n_taxonomy_vocabulary_name($voc, $GLOBALS['language']->language); - } - $output .= _site_map_taxonomy_tree($voc->vid, $voc->name, $voc->description); - } - _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE); + if (module_exists('taxonomy') && !empty($voc)) { + if (module_exists('i18n_taxonomy')) { + $voc->name = i18n_taxonomy_vocabulary_name($voc, $GLOBALS['language']->language); + } + $output .= _site_map_taxonomy_tree($voc->vid, $voc->name, $voc->description); + _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE); } return $output; diff --git a/site_map.theme.inc b/site_map.theme.inc index 1f5791b..877f10f 100644 --- a/site_map.theme.inc +++ b/site_map.theme.inc @@ -181,34 +181,99 @@ function template_preprocess_site_map(&$variables) { $variables['rss_legend'] = theme('site_map_rss_legend'); } - if (variable_get('site_map_show_front', 1)) { - $variables['front_page'] = _site_map_front_page(); - } - if (variable_get('site_map_show_titles', 1)) { $variables['show_titles'] = TRUE; } - if (variable_get('site_map_show_blogs', 1)) { - $variables['blogs'] = _site_map_blogs(); - } - - // Compile the books trees. - $variables['books'] = _site_map_books(); + $variables['site_map'] = ''; + $site_map_order = variable_get('site_map_order', array()); + asort($site_map_order); + + foreach ($site_map_order as $content => $weight) { + // Get type of content + $type = substr($content, 0, strpos($content, '_')); + $id = substr($content, strpos($content, '_') + 1); + if (empty($type)) { + $type = $content; + $id = NULL; + } - // Compile the menu trees. - $variables['menus'] = _site_map_menus(); + switch ($type) { + case 'front': + if (variable_get('site_map_show_front', 1)) { + $variables['site_map'] .= _site_map_front_page(); + } + break; + case 'blogs': + if (variable_get('site_map_show_blogs', 1)) { + $variables['site_map'] .= _site_map_blogs(); + } + break; + case 'books': + $books = variable_get('site_map_show_books', array()); + if (!empty($books)) { + $variables['site_map'] .= _site_map_books(); + } + break; + case 'menus': + $menus = variable_get('site_map_show_menus', array()); + if (!empty($menus[$id])) { + $variables['site_map'] .= _site_map_menus($id); + } + break; + case 'faq': + if (variable_get('site_map_show_faq', 0)) { + $variables['site_map'] .= _site_map_faq(); + } + break; + case 'vocabularies': + $vocabulary = taxonomy_vocabulary_load($id); + $vocabularies = variable_get('site_map_show_vocabularies', array()); - if (variable_get('site_map_show_faq', 0)) { - $variables['faq'] = _site_map_faq(); + if (!empty($vocabularies[$vocabulary->machine_name])) { + $variables['site_map'] .= _site_map_taxonomys($vocabulary); + } + break; + } } - // Compile the vocabulary trees. - $variables['taxonomys'] = _site_map_taxonomys(); - // Invoke all custom modules and integrate themed HTML into the site map. $additional = module_invoke_all('site_map'); foreach ($additional as $themed_site_map_code) { $variables['additional'] .= $themed_site_map_code; } } + +/** + * Returns HTML for the site map order form. + * + * Copied from the core theme_filter_admin_format_filter_order() function. + * + * @param $variables + * An associative array containing: + * - element: A render element representing the form. + * + * @ingroup themeable + */ +function theme_site_map_order($variables) { + $element = $variables['element']; + + // Site map order (tabledrag). + $rows = array(); + foreach (element_children($element, TRUE) as $name) { + $element[$name]['weight']['#attributes']['class'][] = 'site-map-order-weight'; + $rows[] = array( + 'data' => array( + drupal_render($element[$name]['content']), + drupal_render($element[$name]['weight']), + ), + 'class' => array('draggable'), + ); + } + $output = drupal_render_children($element); + $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'site-map-order'))); + drupal_add_tabledrag('site-map-order', 'order', 'sibling', 'site-map-order-weight', NULL, NULL, TRUE); + + return $output; +} + -- 2.0.0