Sitemap Patch

Sitemap Patch

Description 

Patch to make sitemap sortable.

Patch

Patch found here: https://www.drupal.org/files/issues/site_map-allow_ordering_content-1529....

From a8a6d468b469ab6ed97656e342197b8b892c627b Mon Sep 17 00:00:00 2001
From: Ron Shimshock <ron@shimshockgroup.com>
Date: Mon, 22 Jun 2015 15:40:53 -0500
Subject: [PATCH] Issue #1529708 - allow ordering of site map content

---
 site-map.tpl.php   |  36 +-----------------
 site_map.admin.inc |  34 ++++++++++++++++-
 site_map.admin.js  |  29 +++++++++++++++
 site_map.module    |  93 +++++++++++++++++++++-------------------------
 site_map.theme.inc | 107 +++++++++++++++++++++++++++++++++++++++++++----------
 5 files changed, 192 insertions(+), 107 deletions(-)
 create mode 100644 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 @@
     </div>
   <?php endif; ?>
 
-  <?php if($front_page): ?>
-    <div class="site-map-front-page">
-      <?php print $front_page; ?>
-    </div>
-  <?php endif; ?>
-
-  <?php if($blogs): ?>
-    <div class="site-map-blogs">
-      <?php print $blogs; ?>
-    </div>
-  <?php endif; ?>
-
-  <?php if($books): ?>
-    <div class="site-map-books">
-      <?php print $books; ?>
-    </div>
-  <?php endif; ?>
-
-  <?php if($menus): ?>
-    <div class="site-map-menus">
-      <?php print $menus; ?>
-    </div>
-  <?php endif; ?>
-
-  <?php if($faq): ?>
-    <div class="site-map-faq">
-      <?php print $faq; ?>
-    </div>
-  <?php endif; ?>
-
-  <?php if($taxonomys): ?>
-    <div class="site-map-taxonomys">
-      <?php print $taxonomys; ?>
-    </div>
-  <?php endif; ?>
+  <?php print $site_map; ?>
 
   <?php if($additional): ?>
     <div class="site-map-additional">
diff --git a/site_map.admin.inc b/site_map.admin.inc
index 42648d5..ce0e104 100644
--- a/site_map.admin.inc
+++ b/site_map.admin.inc
@@ -37,12 +37,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'),
@@ -56,6 +58,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();
@@ -75,11 +78,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'),
@@ -101,11 +106,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(
@@ -115,6 +122,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..8af7965
--- /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 aac1f01..089d10f 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',
+    ),
   );
 }
 
@@ -463,52 +467,43 @@ 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();
 
-  // Get the list of menus we'll be displaying.
-  $mids = array_filter(variable_get('site_map_show_menus', array()));
-
-  // Allow other modules to alter it.
-  drupal_alter('site_map_menu_list', $mids);
-
-  // Iterate through each menu to render it.
-  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);
+  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);
+    }
+
+    // Add an alter hook so that 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);
 
-      // Add an alter hook so that 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,
-        ));
-      }
+      $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,
+      ));
     }
   }
 
@@ -549,21 +544,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);
+  if (module_exists('taxonomy') && !empty($voc)) {
+    if (module_exists('i18n_taxonomy')) {
+      $voc->name = i18n_taxonomy_vocabulary_name($voc, $GLOBALS['language']->language);
     }
-    _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE);
 
+    $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 17f1785..e475957 100644
--- a/site_map.theme.inc
+++ b/site_map.theme.inc
@@ -11,7 +11,7 @@
  * Returns HTML for a site map feed icon legend.
  */
 function theme_site_map_rss_legend() {
-  $output .= '<p><strong>' . t('Legend:') . '</strong><br />';
+  $output = '<p><strong>' . t('Legend:') . '</strong><br />';
   $output .= '<span class="rss">' . theme('site_map_feed_icon', array('type' => 'node')) . '</span> ' . t('Link to a content RSS feed');
   $output .= '<br /><span class="rss">' . theme('site_map_feed_icon', array('type' => 'comment')) . '</span> ' . t('Link to a comment RSS feed');
   $output .= '</p>';
@@ -185,34 +185,103 @@ 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();
-
-  // Compile the menu trees.
-  $variables['menus'] = _site_map_menus();
+  $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;
+    }
 
-  if (variable_get('site_map_show_faq', 0)) {
-    $variables['faq'] = _site_map_faq();
+    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':
+        // Get the list of menus we'll be displaying.
+        $menus = variable_get('site_map_show_menus', array());
+        // Allow other modules to alter it.
+        drupal_alter('site_map_menu_list', $menus);
+        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_machine_name_load($id);
+        $vocabularies = variable_get('site_map_show_vocabularies', array());
+        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.4.4