features.menu.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function menu_features_api() {
  6. return array(
  7. 'menu_custom' => array(
  8. 'name' => t('Menus'),
  9. 'default_hook' => 'menu_default_menu_custom',
  10. 'feature_source' => TRUE,
  11. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  12. ),
  13. 'menu_links' => array(
  14. 'name' => t('Menu links'),
  15. 'default_hook' => 'menu_default_menu_links',
  16. 'feature_source' => TRUE,
  17. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  18. ),
  19. // DEPRECATED
  20. 'menu' => array(
  21. 'name' => t('Menu items'),
  22. 'default_hook' => 'menu_default_items',
  23. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  24. 'feature_source' => FALSE,
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_features_export().
  30. * DEPRECATED: This implementation simply migrates deprecated `menu` items
  31. * to the `menu_links` type.
  32. */
  33. function menu_features_export($data, &$export, $module_name = '') {
  34. $pipe = array();
  35. foreach ($data as $path) {
  36. $pipe['menu_links'][] = "features:{$path}";
  37. }
  38. return $pipe;
  39. }
  40. /**
  41. * Implements hook_features_export_options().
  42. */
  43. function menu_custom_features_export_options() {
  44. $options = array();
  45. $result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
  46. foreach ($result as $menu) {
  47. $options[$menu['menu_name']] = $menu['title'];
  48. }
  49. return $options;
  50. }
  51. /**
  52. * Implements hook_features_export().
  53. */
  54. function menu_custom_features_export($data, &$export, $module_name = '') {
  55. // Default hooks are provided by the feature module so we need to add
  56. // it as a dependency.
  57. $export['dependencies']['features'] = 'features';
  58. $export['dependencies']['menu'] = 'menu';
  59. // Collect a menu to module map
  60. $pipe = array();
  61. $map = features_get_default_map('menu_custom', 'menu_name');
  62. foreach ($data as $menu_name) {
  63. // If this menu is provided by a different module, add it as a dependency.
  64. if (isset($map[$menu_name]) && $map[$menu_name] != $module_name) {
  65. $export['dependencies'][$map[$menu_name]] = $map[$menu_name];
  66. }
  67. else {
  68. $export['features']['menu_custom'][$menu_name] = $menu_name;
  69. }
  70. }
  71. return $pipe;
  72. }
  73. /**
  74. * Implements hook_features_export_render()
  75. */
  76. function menu_custom_features_export_render($module, $data) {
  77. $code = array();
  78. $code[] = ' $menus = array();';
  79. $code[] = '';
  80. $translatables = array();
  81. foreach ($data as $menu_name) {
  82. $row = db_select('menu_custom')
  83. ->fields('menu_custom')
  84. ->condition('menu_name', $menu_name)
  85. ->execute()
  86. ->fetchAssoc();
  87. if ($row) {
  88. $export = features_var_export($row, ' ');
  89. $code[] = " // Exported menu: {$menu_name}.";
  90. $code[] = " \$menus['{$menu_name}'] = {$export};";
  91. $translatables[] = $row['title'];
  92. $translatables[] = $row['description'];
  93. }
  94. }
  95. if (!empty($translatables)) {
  96. $code[] = features_translatables_export($translatables, ' ');
  97. }
  98. $code[] = ' return $menus;';
  99. $code = implode("\n", $code);
  100. return array('menu_default_menu_custom' => $code);
  101. }
  102. /**
  103. * Implements hook_features_revert().
  104. */
  105. function menu_custom_features_revert($module) {
  106. menu_custom_features_rebuild($module);
  107. }
  108. /**
  109. * Implements hook_features_rebuild().
  110. */
  111. function menu_custom_features_rebuild($module) {
  112. if ($defaults = features_get_default('menu_custom', $module)) {
  113. foreach ($defaults as $menu) {
  114. menu_save($menu);
  115. }
  116. }
  117. }
  118. /**
  119. * Implements hook_features_export_options().
  120. */
  121. function menu_links_features_export_options() {
  122. global $menu_admin;
  123. // Need to set this to TRUE in order to get menu links that the
  124. // current user may not have access to (i.e. user/login)
  125. $menu_admin = TRUE;
  126. $use_menus = array_intersect_key(menu_get_menus(), array_flip(array_filter(variable_get('features_admin_menu_links_menus', array_keys(menu_get_menus())))));
  127. $menu_links = menu_parent_options($use_menus, array('mlid' => 0));
  128. $options = array();
  129. foreach ($menu_links as $key => $name) {
  130. list($menu_name, $mlid) = explode(':', $key, 2);
  131. if ($mlid != 0) {
  132. $link = menu_link_load($mlid);
  133. $identifier = menu_links_features_identifier($link, TRUE);
  134. $options[$identifier] = "{$menu_name}: {$name}";
  135. }
  136. }
  137. $menu_admin = FALSE;
  138. return $options;
  139. }
  140. /**
  141. * Callback for generating the menu link exportable identifier.
  142. */
  143. function menu_links_features_identifier($link, $old = FALSE) {
  144. // Add some uniqueness to these identifiers, allowing multiple links with the same path, but different titles.
  145. $clean_title = features_clean_title(isset($link['title']) ? $link['title'] : $link['link_title']);
  146. // The old identifier is requested.
  147. if ($old) {
  148. // if identifier already exists
  149. if (isset($link['options']['identifier'])) {
  150. return $link['options']['identifier'];
  151. }
  152. // providing backward compatibility and allowing/enabling multiple links with same paths
  153. else {
  154. $identifier = isset($link['menu_name'], $link['link_path']) ? "{$link['menu_name']}:{$link['link_path']}" : FALSE;
  155. // Checking if there are multiples of this identifier
  156. if (features_menu_link_load($identifier) !== FALSE) {
  157. // this is where we return the upgrade posibility for links.
  158. return $identifier;
  159. }
  160. }
  161. }
  162. return isset($link['menu_name'], $link['link_path']) ? "{$link['menu_name']}_{$clean_title}:{$link['link_path']}" : FALSE;
  163. }
  164. /**
  165. * Implements hook_features_export().
  166. */
  167. function menu_links_features_export($data, &$export, $module_name = '') {
  168. // Default hooks are provided by the feature module so we need to add
  169. // it as a dependency.
  170. $export['dependencies']['features'] = 'features';
  171. $export['dependencies']['menu'] = 'menu';
  172. // Collect a link to module map
  173. $pipe = array();
  174. $map = features_get_default_map('menu_links', 'menu_links_features_identifier');
  175. foreach ($data as $key => $identifier) {
  176. if ($link = features_menu_link_load($identifier)) {
  177. // If this link is provided by a different module, add it as a dependency.
  178. $new_identifier = menu_links_features_identifier($link, empty($export));
  179. if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
  180. $export['dependencies'][$map[$identifier]] = $map[$identifier];
  181. }
  182. else {
  183. $export['features']['menu_links'][$new_identifier] = $new_identifier;
  184. }
  185. // For now, exclude a variety of common menus from automatic export.
  186. // They may still be explicitly included in a Feature if the builder
  187. // chooses to do so.
  188. if (!in_array($link['menu_name'], array('features', 'primary-links', 'secondary-links', 'navigation', 'admin', 'devel'))) {
  189. $pipe['menu_custom'][] = $link['menu_name'];
  190. }
  191. }
  192. }
  193. return $pipe;
  194. }
  195. /**
  196. * Implements hook_features_export_render()
  197. */
  198. function menu_links_features_export_render($module, $data, $export = NULL) {
  199. $code = array();
  200. $code[] = ' $menu_links = array();';
  201. $code[] = '';
  202. $translatables = array();
  203. foreach ($data as $identifier) {
  204. if ($link = features_menu_link_load($identifier)) {
  205. $new_identifier = menu_links_features_identifier($link, empty($export));
  206. // Replace plid with a parent path.
  207. if (!empty($link['plid']) && $parent = menu_link_load($link['plid'])) {
  208. // If the new identifier is different than the old, maintain
  209. // 'parent_path' for backwards compatibility.
  210. if ($new_identifier != menu_links_features_identifier($link)) {
  211. $link['parent_path'] = $parent['link_path'];
  212. }
  213. else {
  214. $clean_title = features_clean_title($parent['title']);
  215. $link['parent_identifier'] = "{$parent['menu_name']}_{$clean_title}:{$parent['link_path']}";
  216. }
  217. }
  218. if (isset($export)) {
  219. // Don't show new identifier unless we are actually exporting.
  220. $link['options']['identifier'] = $new_identifier;
  221. // identifiers are renewed, => that means we need to update them in the db
  222. $temp = $link;
  223. menu_link_save($temp);
  224. }
  225. unset($link['plid']);
  226. unset($link['mlid']);
  227. $code[] = " // Exported menu link: {$new_identifier}.";
  228. $code[] = " \$menu_links['{$new_identifier}'] = ". features_var_export($link, ' ') .";";
  229. $translatables[] = $link['link_title'];
  230. }
  231. }
  232. $code[] = '';
  233. if (!empty($translatables)) {
  234. $code[] = features_translatables_export($translatables, ' ');
  235. }
  236. $code[] = ' return $menu_links;';
  237. $code = implode("\n", $code);
  238. return array('menu_default_menu_links' => $code);
  239. }
  240. /**
  241. * Implements hook_features_revert().
  242. */
  243. function menu_links_features_revert($module) {
  244. menu_links_features_rebuild($module);
  245. }
  246. /**
  247. * Implements hook_features_rebuild().
  248. */
  249. function menu_links_features_rebuild($module) {
  250. if ($menu_links = features_get_default('menu_links', $module)) {
  251. menu_links_features_rebuild_ordered($menu_links);
  252. }
  253. }
  254. /**
  255. * Generate a depth tree of all menu links.
  256. */
  257. function menu_links_features_rebuild_ordered($menu_links, $reset = FALSE) {
  258. static $ordered;
  259. static $all_links;
  260. if (!isset($ordered) || $reset) {
  261. $ordered = array();
  262. $unordered = features_get_default('menu_links');
  263. // Order all links by depth.
  264. if ($unordered) {
  265. do {
  266. $current = count($unordered);
  267. foreach ($unordered as $key => $link) {
  268. $identifier = menu_links_features_identifier($link);
  269. $parent = isset($link['parent_identifier']) ? $link['parent_identifier'] : '';
  270. $weight = 0;
  271. // Parent has been seen, so weigh this above parent.
  272. if (isset($ordered[$parent])) {
  273. $weight = $ordered[$parent] + 1;
  274. }
  275. // Next loop will try to find parent weight instead.
  276. elseif ($parent) {
  277. continue;
  278. }
  279. $ordered[$identifier] = $weight;
  280. $all_links[$identifier] = $link;
  281. unset($unordered[$key]);
  282. }
  283. // Exit out when the above does no changes this loop.
  284. } while (count($unordered) < $current);
  285. }
  286. // Add all remaining unordered items to the ordered list.
  287. foreach ($unordered as $link) {
  288. $identifier = menu_links_features_identifier($link);
  289. $ordered[$identifier] = 0;
  290. $all_links[$identifier] = $link;
  291. }
  292. asort($ordered);
  293. }
  294. // Ensure any default menu items that do not exist are created.
  295. foreach (array_keys($ordered) as $identifier) {
  296. $link = $all_links[$identifier];
  297. $existing = features_menu_link_load($identifier);
  298. if (!$existing || in_array($link, $menu_links)) {
  299. // Retrieve the mlid if this is an existing item.
  300. if ($existing) {
  301. $link['mlid'] = $existing['mlid'];
  302. }
  303. // Retrieve the plid for a parent link.
  304. if (!empty($link['parent_identifier']) && $parent = features_menu_link_load($link['parent_identifier'])) {
  305. $link['plid'] = $parent['mlid'];
  306. }
  307. // This if for backwards compatibility.
  308. elseif (!empty($link['parent_path']) && $parent = features_menu_link_load("{$link['menu_name']}:{$link['parent_path']}")) {
  309. $link['plid'] = $parent['mlid'];
  310. }
  311. else {
  312. $link['plid'] = 0;
  313. }
  314. menu_link_save($link);
  315. }
  316. }
  317. }
  318. /**
  319. * Load a menu link by its menu_name_cleantitle:link_path identifier.
  320. * Also matches links with unique menu_name:link_path
  321. */
  322. function features_menu_link_load($identifier) {
  323. $menu_name = '';
  324. $link_path = '';
  325. // This gets variables for menu_name_cleantitle:link_path format.
  326. if (strstr($identifier, "_")) {
  327. $link_path = substr($identifier, strpos($identifier, ":") + 1);
  328. list($menu_name) = explode('_', $identifier, 2);
  329. $clean_title = substr($identifier, strpos($identifier, "_") + 1, strpos($identifier, ":") - strpos($identifier, "_") - 1);
  330. }
  331. // This gets variables for traditional identifier format.
  332. else {
  333. $clean_title = '';
  334. list($menu_name, $link_path) = explode(':', $identifier, 2);
  335. }
  336. $links = db_select('menu_links')
  337. ->fields('menu_links', array('menu_name', 'mlid', 'plid', 'link_path', 'router_path', 'link_title', 'options', 'module', 'hidden', 'external', 'has_children', 'expanded', 'weight', 'customized'))
  338. ->condition('menu_name', $menu_name)
  339. ->condition('link_path', $link_path)
  340. ->addTag('features_menu_link')
  341. ->execute()
  342. ->fetchAllAssoc('mlid');
  343. foreach($links as $link) {
  344. $link->options = unserialize($link->options);
  345. // Title or previous identifier matches.
  346. if ((isset($link->options['identifier']) && strcmp($link->options['identifier'], $identifier) == 0)
  347. || (isset($clean_title) && strcmp(features_clean_title($link->link_title), $clean_title) == 0)) {
  348. return (array)$link;
  349. }
  350. }
  351. // Only one link with the requested menu_name and link_path does exists,
  352. // -- providing an upgrade possibility for links saved in a feature before the
  353. // new identifier-pattern was added.
  354. if (count($links) == 1 && empty($clean_title)) {
  355. $link = reset($links); // get the first item
  356. return (array)$link;
  357. }
  358. // If link_path was changed on an existing link, we need to find it by
  359. // searching for link_title.
  360. else if (isset($clean_title)) {
  361. $links = db_select('menu_links')
  362. ->fields('menu_links', array('menu_name', 'mlid', 'plid', 'link_path', 'router_path', 'link_title', 'options', 'module', 'hidden', 'external', 'has_children', 'expanded', 'weight'))
  363. ->condition('menu_name', $menu_name)
  364. ->execute()
  365. ->fetchAllAssoc('mlid');
  366. foreach($links as $link) {
  367. $link->options = unserialize($link->options);
  368. // Links with a stored identifier must only be matched on that identifier,
  369. // to prevent cross over assumptions.
  370. if (isset($link->options['identifier'])) {
  371. if (strcmp($link->options['identifier'], $identifier) == 0) {
  372. return (array)$link;
  373. }
  374. }
  375. elseif ((strcmp(features_clean_title($link->link_title), $clean_title) == 0)) {
  376. return (array)$link;
  377. }
  378. }
  379. }
  380. return FALSE;
  381. }
  382. /**
  383. * Returns a lowercase clean string with only letters, numbers and dashes
  384. */
  385. function features_clean_title($str) {
  386. return strtolower(preg_replace_callback('/(\s)|([^a-zA-Z\-0-9])/i', '_features_clean_title', $str));
  387. }
  388. /**
  389. * Callback function for preg_replace_callback() to clean a string.
  390. */
  391. function _features_clean_title($matches) {
  392. return $matches[1] ? '-' : '';
  393. }