menu_token.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /**
  3. * @file
  4. * Main module file for the Menu Token module.
  5. */
  6. /**
  7. * Implements hook_theme().
  8. */
  9. function menu_token_theme() {
  10. return array(
  11. 'menu_token_uses_tokens' => array(),
  12. );
  13. }
  14. /**
  15. * Appends the "uses tokens" label to links on the admin menu links overview
  16. * form.
  17. */
  18. function theme_menu_token_uses_tokens() {
  19. drupal_add_css(drupal_get_path('module', 'menu_token') . '/menu_token.css');
  20. return ' <span class="uses-tokens">' . t('uses tokens') . '</span>';
  21. }
  22. /**
  23. * Implements hook_ctools_plugin_type().
  24. */
  25. function menu_token_ctools_plugin_type() {
  26. return array(
  27. 'plugins' => array(
  28. 'cache' => TRUE,
  29. 'use hooks' => TRUE,
  30. 'classes' => array('handler'),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_menu_token_plugins().
  36. */
  37. function menu_token_menu_token_plugins() {
  38. $plugins = array();
  39. $entity_info = entity_get_info();
  40. $entities = variable_get('menu_token_entities', drupal_map_assoc(array('node', 'user')));
  41. foreach ($entities as $entity => $enabled) {
  42. if ($enabled) {
  43. $plugins["{$entity}_context"] = array(
  44. 'type' => $entity,
  45. 'label' => t('@entity_label from context', array('@entity_label' => $entity_info[$entity]['label'])),
  46. 'description' => t('Picks a @entity_label from the current context.', array('@entity_label' => $entity_info[$entity]['label'])),
  47. 'handler' => array(
  48. 'path' => drupal_get_path('module', 'menu_token') . '/plugins',
  49. 'file' => 'menu_token_entity_context.inc',
  50. 'class' => 'menu_token_entity_context',
  51. ),
  52. );
  53. $plugins["{$entity}_random"] = array(
  54. 'type' => $entity,
  55. 'label' => t('Random @entity_label', array('@entity_label' => $entity_info[$entity]['label'])),
  56. 'description' => t('Picks a random @entity_label from the database.', array('@entity_label' => $entity_info[$entity]['label'])),
  57. 'handler' => array(
  58. 'path' => drupal_get_path('module', 'menu_token') . '/plugins',
  59. 'file' => 'menu_token_entity_random.inc',
  60. 'class' => 'menu_token_entity_random',
  61. ),
  62. );
  63. $plugins["{$entity}_user_defined"] = array(
  64. 'type' => $entity,
  65. 'label' => t('User-defined @entity_label', array('@entity_label' => $entity_info[$entity]['label'])),
  66. 'description' => t('Uses a user-defined @entity_label.', array('@entity_label' => $entity_info[$entity]['label'])),
  67. 'handler' => array(
  68. 'path' => drupal_get_path('module', 'menu_token') . '/plugins',
  69. 'file' => 'menu_token_entity_user_defined.inc',
  70. 'class' => 'menu_token_entity_user_defined',
  71. ),
  72. );
  73. }
  74. }
  75. return $plugins;
  76. }
  77. /**
  78. * Implements hook_translated_menu_link_alter().
  79. */
  80. function menu_token_translated_menu_link_alter(&$item, $map) {
  81. global $menu_admin;
  82. if (!_menu_token_is_called_from_features()) {
  83. return;
  84. }
  85. // Check whether we should replace the path.
  86. if (empty($menu_admin) && isset($item['options']['menu_token_link_path'])) {
  87. $info = token_get_info();
  88. $data = array();
  89. // Load data objects used when replacing link.
  90. if (isset($item['options']['menu_token_data'])) {
  91. foreach ($item['options']['menu_token_data'] as $type => $values) {
  92. if (!empty($info['types'][$type]) && $handler = menu_token_get_handler($values['plugin'])) {
  93. $values['options']['_type'] = $type;
  94. if ($object = $handler->object_load($values['options'])) {
  95. $data[$type] = $object;
  96. }
  97. }
  98. }
  99. }
  100. $options['clear'] = !empty($item['options']['menu_token_options']['clear']) ? TRUE : FALSE;
  101. // If item is generated by admin menu module, tokens should not be replaced
  102. // and indicator that tokens are used should be shown.
  103. $item['title'] = token_replace($item['title'], $data, $options);
  104. $url = token_replace($item['options']['menu_token_link_path'], $data, $options);
  105. // Make sure aliases are proccessed correctly
  106. $url = trim($url, '/');
  107. $url = drupal_get_normal_path($url);
  108. // Override active trail if showing front page but translated link is not to
  109. // front page.
  110. // NOTE: This relies on any parent of a tokenised menu item having "option"
  111. // flag "alter" set, which is most easily achieved by setting it to use
  112. // token translation but not specifying a token. Otherwise parent does not
  113. // get processed through this function and because its untranslated child
  114. // has an href of <front>, the menu system thinks it is part of the active
  115. // trail to the front page.
  116. if (drupal_is_front_page() && $item['href'] != drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
  117. $item['in_active_trail'] = FALSE;
  118. }
  119. // Check whether path is external.
  120. if (url_is_external($url)) {
  121. $item['href'] = $item['link_path'] = $url;
  122. return;
  123. }
  124. // Split url into parts and save in proper format.
  125. $url_parts = parse_url($url);
  126. $url = $url_parts['path'];
  127. $item['href'] = $item['link_path'] = $item['router_path'] = $url;
  128. if (isset($url_parts['query'])) {
  129. $query = drupal_get_query_array($url_parts['query']);
  130. $item['localized_options']['query'] = $item['options']['query'] = $query;
  131. }
  132. if (isset($url_parts['fragment'])) {
  133. $item['localized_options']['fragment'] = $item['options']['fragment'] = $url_parts['fragment'];
  134. }
  135. if ($url == '<front>') {
  136. $url = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  137. }
  138. // Load menu item and check access.
  139. if ($menu_item = menu_get_item($url)) {
  140. $item['access'] = $menu_item['access'];
  141. return;
  142. }
  143. $item['access'] = FALSE;
  144. }
  145. }
  146. /**
  147. * Implements hook_menu_link_alter().
  148. */
  149. function menu_token_menu_link_alter(&$item) {
  150. if (isset($item['options']['menu_token_link_path'])) {
  151. // Set 'alter' option to use hook_translated_menu_link_alter()
  152. // Only alter if not called from within menu_links_features_export_render
  153. $item['options']['alter'] = _menu_token_is_called_from_features();
  154. }
  155. }
  156. /**
  157. * Returns TRUE if 'menu_links_features_export_render' is in the callstack.
  158. */
  159. function _menu_token_is_called_from_features() {
  160. $called = &drupal_static(__FUNCTION__);
  161. if (!isset($called)) {
  162. $callstack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  163. foreach($callstack as $function) {
  164. $called = ($function['function'] == 'menu_links_features_export_render');
  165. if ($called) {
  166. break;
  167. }
  168. }
  169. }
  170. return !$called;
  171. }
  172. /**
  173. * Retrieves the handler of a menu token plugin.
  174. *
  175. * @param $name
  176. * The name of a plugin.
  177. *
  178. * @return
  179. * A menu_token_handler object that represents the handler of the plugin
  180. * defined by $name or FALSE if no plugin named $name exists.
  181. */
  182. function menu_token_get_handler($name) {
  183. $handlers = &drupal_static(__FUNCTION__);
  184. if (!isset($handlers[$name])) {
  185. if ($plugin = menu_token_get_plugin($name)) {
  186. $handlers[$name] = new $plugin['class']($plugin);
  187. }
  188. }
  189. if (isset($handlers[$name])) {
  190. return $handlers[$name];
  191. }
  192. return FALSE;
  193. }
  194. /**
  195. * Retrieves a menu token plugin.
  196. *
  197. * @param $name
  198. * The name of a plugin.
  199. *
  200. * @return
  201. * An array containing information about the plugin as returned by the ctools
  202. * plugin API.
  203. */
  204. function menu_token_get_plugin($name) {
  205. $plugins = _menu_token_plugin_info()->plugins;
  206. if (isset($plugins[$name])) {
  207. return $plugins[$name];
  208. }
  209. return FALSE;
  210. }
  211. /**
  212. * Retrieves a list of all available menu token plugins.
  213. *
  214. * @return
  215. * An array containing all available plugins.
  216. */
  217. function menu_token_get_plugins() {
  218. return _menu_token_plugin_info()->plugins;
  219. }
  220. /**
  221. * Retrieves a list of all token types that are covered by the available menu
  222. * token plugins.
  223. *
  224. * @return
  225. * An array containing all token types covered by menu token plugins.
  226. */
  227. function menu_token_get_plugin_types() {
  228. return _menu_token_plugin_info()->types;
  229. }
  230. /**
  231. * Builds and returns information about the menu token plugins and their types.
  232. */
  233. function _menu_token_plugin_info() {
  234. $cache = &drupal_static(__FUNCTION__);
  235. if (!isset($cache)) {
  236. ctools_include('plugins');
  237. $cache = (object) array(
  238. 'plugins' => array(),
  239. 'types' => array(),
  240. );
  241. $info = token_get_info();
  242. foreach (ctools_get_plugins('menu_token', 'plugins') as $plugin) {
  243. if (isset($info['types'][$plugin['type']]) && $class = ctools_plugin_get_class($plugin, 'handler')) {
  244. $cache->plugins[$plugin['name']] = $plugin;
  245. $cache->plugins[$plugin['name']]['class'] = $class;
  246. $cache->types[$plugin['type']][$plugin['name']] = $plugin['label'];
  247. }
  248. }
  249. }
  250. return $cache;
  251. }
  252. /**
  253. * Implementation of hook_form_FORM_ID_alter().
  254. */
  255. function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
  256. if ($form['module']['#value'] == 'menu') {
  257. $types = menu_token_get_plugin_types();
  258. $options = $form['options']['#value'];
  259. // Replace fake path (/menutoken/ouruid) with user inputed one.
  260. if (!empty($options['menu_token_link_path'])) {
  261. $form['menu_token_uuid'] = array(
  262. '#type' => 'hidden',
  263. '#value' => $form['link_path']['#default_value'],
  264. );
  265. $form['link_path']['#default_value'] = $options['menu_token_link_path'];
  266. }
  267. $form['link_title']['#weight'] = -5;
  268. $form['link_path']['#weight'] = -4;
  269. $form['menu_token_enabled'] = array(
  270. '#type' => 'checkbox',
  271. '#title' => t('<strong>Use tokens</strong> in title and in path.'),
  272. '#description' => t('Active this option in order to use Menu token.'),
  273. '#default_value' => isset($options['menu_token_link_path']),
  274. '#weight' => -3,
  275. );
  276. $form['menu_token_options'] = array(
  277. '#type' => 'fieldset',
  278. '#title' => t('Menu Token options'),
  279. '#collapsible' => TRUE,
  280. '#weight' => -2,
  281. '#states' => array(
  282. 'visible' => array(
  283. ':input[name="menu_token_enabled"]' => array('checked' => TRUE),
  284. ),
  285. ),
  286. );
  287. foreach ($types as $type => $items) {
  288. $info = token_get_info($type);
  289. $default = NULL;
  290. if (isset($form_state['values']['menu_token_type_' . $type])) {
  291. $default = $form_state['values']['menu_token_type_' . $type];
  292. }
  293. elseif (!empty($options['menu_token_data'][$type])) {
  294. $default = $options['menu_token_data'][$type]['plugin'];
  295. }
  296. $form['menu_token_options'][$type] = array(
  297. '#type' => 'container',
  298. );
  299. $form['menu_token_options'][$type]['menu_token_type_' . $type] = array(
  300. '#type' => 'select',
  301. '#title' => t('Method for') . ' ' . $info['name'],
  302. '#description' => $info['description'],
  303. '#options' => array('_none' => t('Disabled')),
  304. '#default_value' => isset($default) && in_array($default, array_keys($items)) ? $default : array('_none'),
  305. '#ajax' => array(
  306. 'callback' => 'menu_token_method_callback',
  307. 'wrapper' => 'menu-token-method-options-' . $type,
  308. 'method' => 'replace',
  309. 'effect' => 'fade',
  310. ),
  311. );
  312. foreach ($items as $name => $label) {
  313. $form['menu_token_options'][$type]['menu_token_type_' . $type]['#options'][$name] = $label;
  314. }
  315. $form['menu_token_options'][$type]['menu_token_method_options_wrapper'] = array(
  316. '#type' => 'container',
  317. '#prefix' => '<div id="menu-token-method-options-' . $type . '">',
  318. '#suffix' => '</div>',
  319. );
  320. if (isset($default) && $handler = menu_token_get_handler($default)) {
  321. if ($append = $handler->form_options($options['menu_token_data'][$type]['options'])) {
  322. $form['menu_token_options'][$type]['menu_token_method_options_wrapper']['menu_token_method_options'] = array(
  323. '#type' => 'fieldset',
  324. '#title' => t('Method options'),
  325. '#collapsible' => TRUE,
  326. ) + $append;
  327. }
  328. }
  329. }
  330. $form['menu_token_options']['menu_token_clear'] = array(
  331. '#type' => 'checkbox',
  332. '#title' => t('Remove token if replacement is not present'),
  333. '#description' => t('If the replacement token is not available on the page being viewed, the token will be removed if checked.'),
  334. '#default_value' => isset($options['menu_token_options']['clear']) ? $options['menu_token_options']['clear'] : '',
  335. );
  336. // Create new fieldset.
  337. $form['menu_token_replacement_patterns'] = array(
  338. '#type' => 'fieldset',
  339. '#title' => t('Replacement patterns'),
  340. '#collapsible' => FALSE,
  341. '#weight' => -1,
  342. '#states' => array(
  343. 'visible' => array(
  344. ':input[name="menu_token_enabled"]' => array('checked' => TRUE),
  345. ),
  346. ),
  347. );
  348. $form['menu_token_replacement_patterns']['patterns'] = array(
  349. '#theme' => 'token_tree',
  350. '#token_types' => array_keys($types),
  351. '#dialog' => TRUE,
  352. );
  353. // Add custom validation and submit functions.
  354. array_unshift($form['#validate'], 'menu_token_form_menu_edit_item_validate');
  355. array_unshift($form['#submit'], 'menu_token_form_menu_edit_item_submit');
  356. foreach (array_keys(menu_token_get_plugins()) as $plugin) {
  357. if ($handler = menu_token_get_handler($plugin)) {
  358. $handler->form_alter($form, $form_state);
  359. }
  360. }
  361. }
  362. }
  363. /**
  364. * Custom validation for form menu_edit_item.
  365. */
  366. function menu_token_form_menu_edit_item_validate($form, &$form_state) {
  367. $values = $form_state['values'];
  368. // If token replacing is enabled and this is a custom menu item.
  369. if ($values['module'] == 'menu' && !empty($values['menu_token_enabled'])) {
  370. // Substitute link_path with our own unique menu path. This will make sure features will export our menu items.
  371. form_set_value(array('#parents' => array('options', 'menu_token_link_path')), $values['link_path'], $form_state);
  372. form_set_value(array('#parents' => array('link_path')), '<front>', $form_state);
  373. if (!empty($values['menu_token_uuid'])) {
  374. // If a uuid already exists, dont change it
  375. form_set_value(array('#parents' => array('link_path')), $values['menu_token_uuid'], $form_state);
  376. }
  377. else {
  378. form_set_value(array('#parents' => array('link_path')), 'menutoken/' . uniqid(), $form_state);
  379. }
  380. foreach (array_keys(menu_token_get_plugin_types()) as $type) {
  381. if (!empty($values['menu_token_type_' . $type]) && $values['menu_token_type_' . $type] != '_none') {
  382. $plugin = $values['menu_token_type_' . $type];
  383. if ($handler = menu_token_get_handler($plugin)) {
  384. // Validate the form via the handler.
  385. $form_state['_menu_token_entity_type'] = $type;
  386. $handler->form_validate($form, $form_state);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * Custom submit for form menu_edit_item.
  394. */
  395. function menu_token_form_menu_edit_item_submit($form, &$form_state) {
  396. $values = &$form_state['values'];
  397. // If token replacing is enabled and this is a custom menu item
  398. if ($values['module'] == 'menu' && !empty($values['menu_token_enabled'])) {
  399. // Store the actual path in the options array.
  400. form_set_value(array('#parents' => array('options', 'menu_token_data')), array(), $form_state);
  401. form_set_value(array('#parents' => array('options', 'menu_token_options', 'clear')), $values['menu_token_clear'], $form_state);
  402. foreach (array_keys(menu_token_get_plugin_types()) as $type) {
  403. if (!empty($values['menu_token_type_' . $type]) && $values['menu_token_type_' . $type] != '_none') {
  404. $plugin = $values['menu_token_type_' . $type];
  405. if ($handler = menu_token_get_handler($plugin)) {
  406. form_set_value(array('#parents' => array('options', 'menu_token_data', $type)), array('type' => $type, 'plugin' => $plugin, 'options' => array()), $form_state);
  407. // Validate the form via the handler.
  408. if ($output = $handler->form_submit($form, $form_state)) {
  409. $output = $values['options']['menu_token_data'][$type]['options'] + $output;
  410. form_set_value(array('#parents' => array('options', 'menu_token_data', $type, 'options')), $output, $form_state);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. else {
  417. foreach (array('menu_token_link_path', 'menu_token_data', 'menu_token_options') as $key) {
  418. unset($values['options'][$key]);
  419. }
  420. }
  421. }
  422. /**
  423. * Implementation hook_form_FORM_ID_alter().
  424. */
  425. function menu_token_form_menu_overview_form_alter(&$form, $form_state) {
  426. foreach ($form as &$item) {
  427. if (isset($item['mlid'], $item['#item']['options']) && isset($item['#item']['options']['menu_token_link_path'])) {
  428. $item['title']['#markup'] .= theme('menu_token_uses_tokens');
  429. }
  430. }
  431. }
  432. /**
  433. * Ajax callback for the method select dropdown.
  434. */
  435. function menu_token_method_callback($form, $form_state) {
  436. $parents = $form_state['triggering_element']['#array_parents'];
  437. array_pop($parents);
  438. array_push($parents, 'menu_token_method_options_wrapper');
  439. return drupal_array_get_nested_value($form, $parents);
  440. }
  441. /**
  442. * The menu token handler interface should be implemented by all menu token
  443. * plugins.
  444. */
  445. interface menu_token_handler {
  446. /**
  447. * You can provide options for your menu token handler via this function.
  448. * The return value will be appended to the form as soon as the administrator
  449. * chooses your plugin.
  450. */
  451. function form_options($options);
  452. /**
  453. * This function allows your plugin to act upon form submission. The return
  454. * value will be added to the $options array and thus should be an array
  455. * itself.
  456. *
  457. * Note: Only invoked for selected plugins.
  458. */
  459. function form_submit($form, &$form_state);
  460. /**
  461. * This function allows your plugin to act upon form validation. The return
  462. * value will be added to the $options array and thus should be an array
  463. * itself.
  464. *
  465. * Note: Only invoked for selected plugins.
  466. */
  467. function form_validate($form, &$form_state);
  468. /**
  469. * You can alter the menu item administration form with this function.
  470. */
  471. function form_alter(&$form, &$form_state);
  472. /**
  473. * This function is used to load the relevant token replacement object.
  474. */
  475. function object_load($options);
  476. }
  477. /**
  478. * Implements hook_permission().
  479. */
  480. function menu_token_permission() {
  481. return array(
  482. 'administer menu_token' => array(
  483. 'title' => t('Administer Menu Token'),
  484. ),
  485. );
  486. }
  487. /**
  488. * Implements hook_menu().
  489. */
  490. function menu_token_menu() {
  491. $items = array();
  492. $items['menutoken/%'] = array(
  493. 'title' => "Dummy Menu Token item",
  494. 'access callback' => TRUE,
  495. 'page callback' => 'theme_menu_token_uses_tokens',
  496. );
  497. $items['admin/config/menu_token'] = array(
  498. 'title' => 'Menu Token',
  499. 'description' => 'Configure the Menu Token module.',
  500. 'page callback' => 'drupal_get_form',
  501. 'page arguments' => array('menu_token_settings_form'),
  502. 'access arguments' => array('administer menu_token'),
  503. 'file' => 'menu_token.admin.inc',
  504. );
  505. return $items;
  506. }