You are here

xmlsitemap_menu.module in XML sitemap 5.2

Adds menu items to the sitemap.

File

xmlsitemap_menu/xmlsitemap_menu.module
View source
<?php

/**
 * @file
 * Adds menu items to the sitemap.
 */

/**
 * @addtogroup xmlsitemap
 * @{
 */

/*****************************************************************************
 * Drupal hooks.
 ****************************************************************************/

/**
 * Implementation of hook_form_alter().
 */
function xmlsitemap_menu_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'xmlsitemap_settings_sitemap':
      $form['xmlsitemap_menu'] = array(
        '#type' => 'fieldset',
        '#title' => t('Menu settings'),
        '#description' => t('The settings for the menus to include in the sitemap.'),
        '#collapsible' => TRUE,
      );
      $form['xmlsitemap_menu']['xmlsitemap_menu_menus'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Menus to include in sitemap'),
        '#default_value' => array_filter(variable_get('xmlsitemap_menu_menus', array())),
        '#options' => menu_get_root_menus(),
        '#checkall' => TRUE,
      );
      $form['buttons']['#weight'] = 1;
      break;
  }
}

/**
 * Implementation of hook_menu().
 */
function xmlsitemap_menu_menu($may_cache) {
  if ($may_cache) {
    global $user;
    if (!empty($user->uid)) {
      xmlsitemap_flag_sitemap();
    }
  }
}

/**
 * Implementation of hook_xmlsitemap_description().
 */
function xmlsitemap_menu_xmlsitemap_description() {
  return '<dt>' . t('XML sitemap menu') . '</dt>' . '<dd>' . t('It allows menu items to be added to the sitemap. You can choose the menus to include on the XML sitemap administration page and can add and remove menu items on the <a href="@menu">menu administration</a> page. The priority of a menu item is determined by its weight.', array(
    '@menu' => url('admin/build/menu'),
  )) . '</dd>';
}

/**
 * Implementation of hook_xmlsitmap_links().
 */
function xmlsitemap_menu_xmlsitemap_links() {
  unset($GLOBALS['_menu']);
  $menu = menu_get_menu();
  $root_menus = array();
  foreach ($menu['items'][0]['children'] as $mid) {
    $root_menus[$mid] = $menu['items'][$mid]['title'];
  }
  foreach (array_filter(variable_get('xmlsitemap_menu_menus', array())) as $mid) {
    if (isset($root_menus[$mid])) {
      _xmlsitemap_menu_process_link($mid, $menu);
    }
  }
}

/*****************************************************************************
 * Private functions.
 ****************************************************************************/

/**
 * Add links for a menu item and all its children to the sitemap.
 * @param $mid: The ID of the menu to process
 * @param $menu: The full menu structure for a user
 * @return None
 */
function _xmlsitemap_menu_process_link($mid, $menu) {
  $item = $menu['visible'][$mid];
  if (!empty($item['path']) && strpos($item['path'], '://') === FALSE && $item['path'] != variable_get('site_frontpage', 'node')) {
    $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $item['path']));
    $alias = $alias === FALSE ? NULL : $alias;
    $priority = 1.0 - min(max(round(($menu['items'][$mid]['weight'] + 10) / 20, 1), 0.0), 1.0);
    db_query("INSERT INTO {xmlsitemap} (loc, priority) VALUES ('%s', %f)", xmlsitemap_url($item['path'], $alias, NULL, NULL, TRUE), $priority);
  }
  if (isset($item['children'])) {
    foreach ($item['children'] as $mid) {
      _xmlsitemap_menu_process_link($mid, $menu);
    }
  }
}

/**
 * @} End of "addtogroup xmlsitemap".
 */

Functions

Namesort descending Description
xmlsitemap_menu_form_alter Implementation of hook_form_alter().
xmlsitemap_menu_menu Implementation of hook_menu().
xmlsitemap_menu_xmlsitemap_description Implementation of hook_xmlsitemap_description().
xmlsitemap_menu_xmlsitemap_links Implementation of hook_xmlsitmap_links().
_xmlsitemap_menu_process_link Add links for a menu item and all its children to the sitemap.