Commit 68a6af0b authored by Dave Reid's avatar Dave Reid
Browse files

by Dave Reid: Ported the xmlsitemap_menu to work on Drupal 7.

parent 725c8cea
......@@ -14,6 +14,9 @@ function xmlsitemap_menu_xmlsitemap_link_info() {
'bundle' => 'menu_name',
),
'path callback' => 'xmlsitemap_menu_path',
'xmlsitemap' => array(
'process callback' => 'xmlsitemap_menu_xmlsitemap_process_menu_links',
),
);
foreach (menu_get_menus() as $menu => $name) {
$types['menu']['bundles'][$menu] = array(
......@@ -48,58 +51,31 @@ function xmlsitemap_menu_cron() {
* Implements hook_xmlsitemap_index_links().
*/
function xmlsitemap_menu_xmlsitemap_index_links($limit) {
if ($menus = xmlsitemap_menu_get_menus()) {
// Set the global user variable to the anonymous user.
xmlsitemap_switch_user(0);
if ($menus = xmlsitemap_get_link_type_enabled_bundles('menu')) {
$sql = "SELECT ml.mlid FROM {menu_links} ml LEFT JOIN {xmlsitemap} x ON x.type = 'menu' AND ml.mlid = x.id WHERE x.id IS NULL AND ml.menu_name IN (:menus) ORDER BY ml.mlid DESC";
$mlids = db_query_range($sql, 0, $limit, array(':menus' => $menus));
foreach ($mlids as $mlid) {
$menu_item = menu_link_load($mlid);
$link = xmlsitemap_menu_create_link($menu_item);
xmlsitemap_save_link($link);
}
// Set the global user variable back to the original user.
xmlsitemap_restore_user();
xmlsitemap_menu_xmlsitemap_process_menu_links($mlids);
}
}
/**
* Implements hook_xmlsitemap_links().
* Process menu sitemap links.
*
* @param $tids
* An array of menu link IDs.
*/
function xmlsitemap_menu_xmlsitemap_links($offset = 0, $limit = 0) {
$links = array();
if ($menus = xmlsitemap_menu_get_menus()) {
// Set the global user variable to the anonymous user.
xmlsitemap_switch_user(0);
$sql = "SELECT ml.mlid FROM {menu_links} ml WHERE ml.mlid > :mlid AND ml.menu_name IN (:menus) ORDER BY ml.mlid";
$args = array(':mlid' => $offset, ':menus' => $menus);
$mlids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
foreach ($mlids as $mlid) {
$menu_item = menu_link_load($mlid);
$links[] = xmlsitemap_menu_create_link($menu_item);
}
function xmlsitemap_menu_xmlsitemap_process_menu_links(array $mlids) {
// Set the global user variable to the anonymous user.
xmlsitemap_switch_user(0);
// Set the global user variable back to the original user.
xmlsitemap_restore_user();
foreach ($mlids as $mlid) {
$menu_item = menu_link_load($mlid);
$link = xmlsitemap_menu_create_link($menu_item);
xmlsitemap_save_link($link);
}
return $links;
}
/**
* Implements hook_xmlsitemap_links_batch_info().
*/
function xmlsitemap_menu_xmlsitemap_links_batch_info() {
$menus = xmlsitemap_menu_get_menus();
return array(
'max' => $menus ? db_query("SELECT COUNT(ml.mlid) FROM {menu_links} ml WHERE ml.menu_name IN (:menus)", array(':menus' => $menus))->fetchField() : 0,
);
// Set the global user variable back to the original user.
xmlsitemap_restore_user();
}
/**
......@@ -125,11 +101,6 @@ function xmlsitemap_menu_form_menu_edit_menu_alter(&$form, $form_state) {
// '#default_value' => variable_get('xmlsitemap_menu_calculate_priority_' . $menu, FALSE),
//);
$form['submit'] += array('#weight' => 50);
if (isset($form['delete'])) {
$form['delete'] += array('#weight' => 51);
}
$form['#submit'][] = 'xmlsitemap_menu_menu_edit_menu_submit';
}
......@@ -147,29 +118,6 @@ function xmlsitemap_menu_menu_edit_menu_submit($form, $form_state) {
if ($new_priority != variable_get('xmlsitemap_menu_priority_' . $menu, 0.5)) {
xmlsitemap_update_links(array('priority' => $new_priority), array('type' => 'menu', 'subtype' => $menu, 'priority_override' => 0));
}
variable_set('xmlsitemap_menu_priority_' . $menu, $new_priority);
variable_set('xmlsitemap_menu_status_' . $menu, $new_status);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see menu_delete_menu_confirm()
* @see xmlsitemap_menu_form_menu_delete_menu_confirm_submit()
*/
function xmlsitemap_menu_form_menu_delete_menu_confirm_alter(&$form, $form_state) {
$form['#submit'][] = 'xmlsitemap_menu_form_menu_delete_menu_confirm_submit';
}
/**
* Form submit handler; delete sitemap links when a menu is deleted.
*/
function xmlsitemap_menu_form_menu_delete_menu_confirm_submit($form, $form_state) {
$menu = $form['#menu']['menu_name'];
xmlsitemap_link_delete_multiple(array('type' => 'menu', 'subtype' => $menu));
variable_del('xmlsitemap_menu_status_' . $menu);
variable_del('xmlsitemap_menu_priority_ ' . $menu);
}
//function xmlsitemap_menu_form_menu_overview_form_alter(&$form, $form_state) {
......@@ -186,60 +134,49 @@ function xmlsitemap_menu_form_menu_delete_menu_confirm_submit($form, $form_state
//}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see menu_item_delete_form()
* Implements hook_menu_insert().
*/
function xmlsitemap_menu_form_menu_item_delete_form_alter(&$form, $form_state) {
$form['#submit'][] = 'xmlsitemap_menu_menu_item_delete_form_submit';
function xmlsitemap_menu_menu_insert(array $menu) {
variable_set('xmlsitemap_menu_priority_' . $menu['menu_name'], $menu['xmlsitemap_menu_priority']);
variable_set('xmlsitemap_menu_status_' . $menu['menu_name'], $menu['xmlsitemap_menu_status']);
}
/**
* Form submit callback; delete the sitemap link when a menu item is deleted.
* Implements hook_menu_update().
*/
function xmlsitemap_menu_menu_item_delete_form_submit($form, $form_state) {
xmlsitemap_menu_item_delete($form['#item']);
function xmlsitemap_menu_menu_update(array $menu) {
variable_set('xmlsitemap_menu_priority_' . $menu['menu_name'], $menu['xmlsitemap_menu_priority']);
variable_set('xmlsitemap_menu_status_' . $menu['menu_name'], $menu['xmlsitemap_menu_status']);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see menu_edit_item()
* Implements hook_menu_delete().
*/
function xmlsitemap_menu_form_menu_edit_item_alter(&$form, $form_state) {
$form['#submit'][] = 'xmlsitemap_menu_menu_edit_item_submit';
function xmlsitemap_menu_menu_delete(array $menu) {
xmlsitemap_link_delete_multiple(array('type' => 'menu', 'subtype' => $menu['menu_name']));
variable_del('xmlsitemap_menu_status_' . $menu['menu_name']);
variable_del('xmlsitemap_menu_priority_ ' . $menu['menu_name']);
}
/**
* Form submit callback; update the sitemap link when a menu item is updated.
* Implements hook_menu_link_insert().
*/
function xmlsitemap_menu_menu_edit_item_submit($form, $form_state) {
xmlsitemap_switch_user(0);
$menu_item = menu_link_load($form_state['values']['menu']['mlid']);
xmlsitemap_restore_user();
xmlsitemap_menu_item_update($menu_item);
function xmlsitemap_menu_menu_link_insert(array $link) {
xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']));
}
function xmlsitemap_menu_item_update(array $menu_item) {
$link = xmlsitemap_menu_create_link($menu_item);
xmlsitemap_save_link($link);
}
function xmlsitemap_menu_item_delete(array $menu_item) {
xmlsitemap_link_delete('menu', $menu_item['mlid']);
/**
* Implements hook_menu_link_update().
*/
function xmlsitemap_menu_menu_link_update(array $link) {
xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']));
}
/**
* Fetch an array of menus to be included in the sitemap.
* Implements hook_menu_link_delete().
*/
function xmlsitemap_menu_get_menus() {
$menus = array_keys(menu_get_menus());
foreach ($menus as $index => $menu) {
if (!variable_get('xmlsitemap_menu_status_' . $menu, 0)) {
unset($menus[$index]);
}
}
return $menus;
function xmlsitemap_menu_menu_link_delete(array $link) {
xmlsitemap_link_delete('menu', $link['mlid']);
}
/**
......@@ -302,22 +239,3 @@ function xmlsitemap_menu_variables() {
}
return $defaults;
}
/**
* Internal implementation of variable_get().
*/
function xmlsitemap_menu_var($name, $default = NULL) {
static $defaults = NULL;
if (!isset($defaults)) {
$defaults = xmlsitemap_menu_variables();
}
$name = 'xmlsitemap_menu_' . $name;
// @todo Remove when stable.
if (!isset($defaults[$name])) {
trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
}
return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment