xmlsitemap_menu.module 9.15 KB
Newer Older
1 2
<?php

3
/**
4 5 6
 * Implements hook_entity_info_alter().
 *
 * Adds support for the menu link entity if it doesn't already exist.
7
 */
8 9 10 11 12 13 14 15 16 17 18 19 20
function xmlsitemap_menu_entity_info_alter(&$info) {
  if (!isset($info['menu_link'])) {
    $info['menu_link'] = array(
      'label' => t('Menu link'),
      'controller class' => 'DrupalDefaultEntityController',
      'base table' => 'menu_links',
      'uri callback' => 'xmlsitemap_menu_menu_link_uri',
      'fieldable' => FALSE,
      'static cache' => TRUE,
      'field cache' => TRUE,
      'entity keys' => array(
        'id' => 'mlid',
        'bundle' => 'menu_name',
21
        'label' => 'link_title',
22 23 24 25 26 27 28 29 30 31
        'revision' => '',
      ),
      'load hook' => NULL,
      'view modes' => array(),
      'translation' => array(),
      'schema_fields_sql' => array(
        'base table' => drupal_schema_fields_sql('menu_links'),
      ),
      'xmlsitemap' => array(
        'process callback' => 'xmlsitemap_menu_xmlsitemap_process_menu_links',
32
      ),
33
      'bundle label' => t('Menu'),
34
      'token type' => 'menu_link',
35
    );
36 37 38

    foreach (menu_get_menus() as $type => $name) {
      $info['menu_link']['bundles'][$type] = array(
39
        'label' => $name,
40 41 42 43 44 45 46 47
        'admin' => array(
          'path' => 'admin/structure/menu/manage/%menu/edit',
          'bundle argument' => 4,
          'real path' => 'admin/structure/menu/manage/' . $type . '/edit',
          'access arguments' => array('administer menus'),
        ),
      );
    }
48
  }
49 50 51 52 53 54 55 56 57
  else {
    // If the entity type already exists ensure the xmlsitemap is added.
    $info['menu_link'] += array(
      'uri callback' => 'xmlsitemap_menu_menu_link_uri',
      'xmlsitemap' => array(
        'process callback' => 'xmlsitemap_menu_xmlsitemap_process_menu_links',
      ),
    );
  }
58 59
}

60 61 62
/**
 * Entity URI callback.
 */
63 64
function xmlsitemap_menu_menu_link_uri($menu_item) {
  return is_array($menu_item) ? $menu_item['href'] : $menu_item->href;
65 66
}

67 68 69 70 71 72
/**
 * Implements hook_cron().
 *
 * Process old menu links not found in the {xmlsitemap} table.
 */
function xmlsitemap_menu_cron() {
73 74 75 76 77 78 79
  xmlsitemap_menu_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}

/**
 * Implements hook_xmlsitemap_index_links().
 */
function xmlsitemap_menu_xmlsitemap_index_links($limit) {
80
  if ($menus = xmlsitemap_get_link_type_enabled_bundles('menu_link')) {
81
    $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";
82
    $mlids = db_query_range($sql, 0, $limit, array(':menus' => $menus))->fetchCol();
83
    xmlsitemap_menu_xmlsitemap_process_menu_links($mlids);
84 85 86 87
  }
}

/**
88 89
 * Process menu sitemap links.
 *
90
 * @param $mlids
91
 *   An array of menu link IDs.
92
 */
93
function xmlsitemap_menu_xmlsitemap_process_menu_links(array $mlids, array $xmlsitemap = array()) {
94 95
  // Set the global user variable to the anonymous user.
  xmlsitemap_switch_user(0);
96

97 98
  foreach ($mlids as $mlid) {
    $menu_item = menu_link_load($mlid);
99 100 101
    if (empty($menu_item)) {
      continue;
    }
102 103 104
    if (!empty($xmlsitemap)) {
      $menu_item['xmlsitemap'] = $xmlsitemap;
    }
105
    $link = xmlsitemap_menu_create_link($menu_item);
106
    xmlsitemap_link_save($link, array($link['type'] => $menu_item));
107 108
  }

109 110
  // Set the global user variable back to the original user.
  xmlsitemap_restore_user();
111 112 113 114 115 116
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_edit_menu()
117
 * @see xmlsitemap_add_link_bundle_settings()
118 119
 */
function xmlsitemap_menu_form_menu_edit_menu_alter(&$form, $form_state) {
120
  $menu = isset($form['menu_name']['#default_value']) ? $form['menu_name']['#default_value'] : '';
121
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
122
  xmlsitemap_add_link_bundle_settings($form, $form_state, 'menu_link', $menu);
123 124 125 126 127 128 129
}

//function xmlsitemap_menu_form_menu_overview_form_alter(&$form, $form_state) {
//  $form['#submit'][] = 'xmlsitemap_menu_menu_overview_form_submit';
//}
//
//function xmlsitemap_menu_menu_overview_form_submit($form, $form_state) {
130
//  $mlids = array();
131 132
//  foreach (element_children($form) as $mlid) {
//    if (isset($form[$mlid]['#item'])) {
133
//      $mlids[] = $form[$mlid]['#item']['mlid'];
134 135
//    }
//  }
136
//  xmlsitemap_menu_xmlsitemap_process_menu_links($mlids);
137 138
//}

139 140 141 142 143
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_edit_item()
 */
144 145 146 147 148 149
function xmlsitemap_menu_form_menu_edit_item_alter(&$form, $form_state) {
  $menu_name = $form['parent']['#default_value'];
  $menu_name = substr($menu_name, 0, strpos($menu_name, ':'));

  // Add the link options.
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
150
  xmlsitemap_add_form_link_options($form, 'menu_link', $menu_name, $form['mlid']['#value']);
151 152 153
  $form['xmlsitemap']['#weight'] = 30;
}

154
/**
155
 * Implements hook_menu_insert().
156
 */
157
function xmlsitemap_menu_menu_insert(array $menu) {
158
  if (isset($menu['xmlsitemap'])) {
159
    xmlsitemap_link_bundle_settings_save('menu_link', $menu['menu_name'], $menu['xmlsitemap']);
160
  }
161 162 163 164

  // When menus change, the bundles we defined in
  // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  entity_info_cache_clear();
165 166 167
}

/**
168
 * Implements hook_menu_update().
169
 */
170
function xmlsitemap_menu_menu_update(array $menu) {
171
  if (isset($menu['xmlsitemap'])) {
172
    xmlsitemap_link_bundle_settings_save('menu_link', $menu['menu_name'], $menu['xmlsitemap']);
173
  }
174 175 176 177

  // When menus change, the bundles we defined in
  // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  entity_info_cache_clear();
178 179 180
}

/**
181
 * Implements hook_menu_delete().
182
 */
183
function xmlsitemap_menu_menu_delete(array $menu) {
184
  xmlsitemap_link_bundle_delete('menu_link', $menu['menu_name']);
185 186 187 188

  // When menus change, the bundles we defined in
  // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  entity_info_cache_clear();
189 190 191
}

/**
192
 * Implements hook_menu_link_insert().
193
 */
194
function xmlsitemap_menu_menu_link_insert(array $link) {
195 196
  $link += array('xmlsitemap' => array());
  xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
197 198
}

199 200
/**
 * Implements hook_menu_link_update().
201 202
 *
 * @see hook_menu_link_alter()
203 204
 */
function xmlsitemap_menu_menu_link_update(array $link) {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  //$link += array('xmlsitemap' => array());
  //xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
}

/**
 * Implements hook_menu_link_alter().
 *
 * We have to use this hook rather than hook_menu_link_update() because this
 * hook is not always called if the user does not edit the core menu item
 * fields.
 *
 * @see http://drupal.org/node/1013856
 */
function xmlsitemap_menu_menu_link_alter(array &$link) {
  if (!empty($link['mlid'])) {
    $link += array('xmlsitemap' => array());
    xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
  }
223 224 225
}

/**
226
 * Implements hook_menu_link_delete().
227
 */
228
function xmlsitemap_menu_menu_link_delete(array $link) {
229
  xmlsitemap_link_delete('menu_link', $link['mlid']);
230 231 232 233 234 235 236 237 238 239 240
}

/**
 * Create a sitemap link from a menu item.
 *
 * @param $menu_item
 *   A loaded menu item.
 */
function xmlsitemap_menu_create_link(array $menu_item) {
  if (!isset($menu_item['xmlsitemap'])) {
    $menu_item['xmlsitemap'] = array();
241
    if ($menu_item['mlid'] && $link = xmlsitemap_link_load('menu_link', $menu_item['mlid'])) {
242 243
      $menu_item['xmlsitemap'] = $link;
    }
244 245
  }

246
  $settings = xmlsitemap_link_bundle_load('menu_link', $menu_item['menu_name']);
247

248
  $menu_item['xmlsitemap'] += array(
249
    'type' => 'menu_link',
250
    'id' => $menu_item['mlid'],
251 252
    'status' => $settings['status'],
    'status_default' => $settings['status'],
253
    'status_override' => 0,
254 255
    'priority' => $settings['priority'],
    'priority_default' => $settings['priority'],
256 257 258 259
    'priority_override' => 0,
  );

  // The following values must always be checked because they are volatile.
260
  $menu_item['xmlsitemap']['loc'] = $menu_item['href'];
261 262
  $menu_item['xmlsitemap']['subtype'] = $menu_item['menu_name'];
  $menu_item['xmlsitemap']['access'] = $menu_item['access'] && !$menu_item['external'] && !$menu_item['hidden'];
263
  $menu_item['xmlsitemap']['language'] = isset($menu_item['options']['langcode']) ? $menu_item['options']['langcode'] : LANGUAGE_NONE;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

  return $menu_item['xmlsitemap'];
}

/**
 * Calculate the priority of a menu link based on depth and weight.
 */
function xmlsitemap_menu_calculate_priority(array $menu_item) {
  $priority = (MENU_MAX_DEPTH - $menu_item['depth'] + 1) / MENU_MAX_DEPTH;
  $priority -= (50 + $menu_item['weight']) / (100 * (MENU_MAX_DEPTH + 1));
  return $priority;
}

/**
 * Internal default variables for template_var().
 */
function xmlsitemap_menu_variables() {
281
  $defaults = array();
282 283
  $menus = array_keys(menu_get_menus());
  foreach ($menus as $menu) {
284
    $defaults['xmlsitemap_settings_menu_' . $menu] = array(
285 286
      'status' => XMLSITEMAP_STATUS_DEFAULT,
      'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
287
    );
288 289 290
  }
  return $defaults;
}
291 292 293 294 295 296 297 298 299 300 301 302 303

/**
 * Implements hook_features_pipe_COMPONENT_alter().
 *
 * Add variables to exported menus.
 */
function xmlsitemap_menu_features_pipe_menu_custom_alter(&$pipe, $data, $export) {
  if (!empty($data)) {
    foreach ($data as $menu_name) {
      $pipe['variable'][] = "xmlsitemap_settings_menu_link_{$menu_name}";
    }
  }
}