menu_attributes.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @file
  4. * Alters the menu item form to allow the administrator to specify additional
  5. * attributes for the menu link
  6. */
  7. /**
  8. * Implements hook_permission().
  9. */
  10. function menu_attributes_permission() {
  11. return array(
  12. 'administer menu attributes' => array(
  13. 'title' => t('Administer menu attributes'),
  14. 'description' => t('Administer menu attributes.'),
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_menu_link_alter().
  20. */
  21. function menu_attributes_menu_link_alter(&$item, $menu) {
  22. if (isset($item['options']['attributes']) && is_array($item['options']['attributes'])) {
  23. // Filter out blank attributes.
  24. foreach ($item['options']['attributes'] as $key => $value) {
  25. if ((is_array($value) && empty($value)) || is_string($value) && !drupal_strlen($value)) {
  26. unset($item['options']['attributes'][$key]);
  27. }
  28. }
  29. // Convert classes to an array.
  30. if (isset($item['options']['attributes']['class']) && is_string($item['options']['attributes']['class'])) {
  31. $item['options']['attributes']['class'] = explode(' ', $item['options']['attributes']['class']);
  32. }
  33. }
  34. }
  35. /**
  36. * Implements hook_menu_attriute_info().
  37. */
  38. function menu_attributes_menu_attribute_info() {
  39. $info['title'] = array(
  40. 'label' => t('Title'),
  41. 'description' => t('The description displayed when hovering over the link.'),
  42. 'form' => array(
  43. '#type' => 'textarea',
  44. '#rows' => 2,
  45. ),
  46. );
  47. $info['id'] = array(
  48. 'label' => t('ID'),
  49. 'description' => t('Specifies a unique ID for the link.'),
  50. );
  51. $info['name'] = array(
  52. 'label' => t('Name'),
  53. );
  54. $info['rel'] = array(
  55. 'label' => t('Relationship'),
  56. 'description' => t("Specifies the relationship between the current page and the link. Enter 'nofollow' here to nofollow this link."),
  57. );
  58. $info['class'] = array(
  59. 'label' => t('Classes'),
  60. 'description' => t('Enter additional classes to be added to the link.'),
  61. );
  62. $info['style'] = array(
  63. 'label' => t('Style'),
  64. 'description' => t('Enter additional styles to be applied to the link.'),
  65. );
  66. $info['target'] = array(
  67. 'label' => t('Target'),
  68. 'description' => t('Specifies where to open the link. Using this attribute breaks XHTML validation.'),
  69. 'form' => array(
  70. '#type' => 'select',
  71. '#options' => array(
  72. '' => 'None (i.e. same window)',
  73. '_blank' => 'New window (_blank)',
  74. '_top' => 'Top window (_top)',
  75. '_self' => 'Same window (_self)',
  76. '_parent' => 'Parent window (_parent)',
  77. ),
  78. ),
  79. );
  80. $info['accesskey'] = array(
  81. 'label' => t('Access Key'),
  82. 'description' => t('Specifies a <a href="@accesskey">keyboard shortcut</a> to access this link.', array('@accesskey' => url('http://en.wikipedia.org/wiki/Access_keys'))),
  83. 'form' => array(
  84. '#maxlength' => 1,
  85. '#size' => 1,
  86. ),
  87. );
  88. return $info;
  89. }
  90. /**
  91. * Fetch an array of menu attributes.
  92. */
  93. function menu_attributes_get_menu_attribute_info() {
  94. $attributes = module_invoke_all('menu_attribute_info');
  95. drupal_alter('menu_attribute_info', $attributes);
  96. // Merge in defaul values.
  97. foreach ($attributes as $attribute => &$info) {
  98. $info += array(
  99. 'form' => array(),
  100. 'enabled' => variable_get("menu_attributes_{$attribute}_enable", 1),
  101. 'default' => '',
  102. );
  103. $info['form'] += array(
  104. '#type' => 'textfield',
  105. '#title' => $info['label'],
  106. '#description' => isset($info['description']) ? $info['description'] : '',
  107. '#default_value' => variable_get("menu_attributes_{$attribute}_default", $info['default']),
  108. );
  109. }
  110. return $attributes;
  111. }
  112. /**
  113. * Implements hook_form_FORM_ID_alter().
  114. *
  115. * Adds menu attribute options to the edit menu item form.
  116. *
  117. * @see menu_edit_item()
  118. * @see _menu_attributes_form_alter()
  119. * @see menu_attributes_form_menu_edit_item_submit()
  120. */
  121. function menu_attributes_form_menu_edit_item_alter(&$form, $form_state) {
  122. $item = $form['original_item']['#value'];
  123. _menu_attributes_form_alter($form, $item, $form);
  124. }
  125. /**
  126. * Implements hook_form_FORM_ID_alter().
  127. *
  128. * Adds menu attribute options to the node's edit menu item form.
  129. *
  130. * @see _menu_attributes_form_alter()
  131. */
  132. function menu_attributes_form_node_form_alter(&$form, $form_state) {
  133. if (isset($form['menu']['link'])) {
  134. $item = $form['#node']->menu;
  135. _menu_attributes_form_alter($form['menu']['link'], $item, $form);
  136. $form['menu']['link']['options']['attributes']['#type'] = 'container';
  137. }
  138. }
  139. /**
  140. * Add the menu attributes to a menu item edit form.
  141. *
  142. * @param $form
  143. * The menu item edit form passed by reference.
  144. * @param $item
  145. * The optional existing menu item for context.
  146. */
  147. function _menu_attributes_form_alter(array &$form, array $item = array(), array &$complete_form) {
  148. // Restrict access to the new form elements.
  149. $form['options']['attributes']['#access'] = user_access('administer menu attributes');
  150. $form['options']['#tree'] = TRUE;
  151. $form['options']['#weight'] = 50;
  152. // Unset the previous value so that the new values get saved.
  153. unset($form['options']['#value']['attributes']);
  154. $form['options']['attributes'] = array(
  155. '#type' => 'fieldset',
  156. '#title' => t('Menu item attributes'),
  157. '#collapsible' => TRUE,
  158. '#collapsed' => FALSE,
  159. '#tree' => TRUE,
  160. );
  161. $attributes = menu_attributes_get_menu_attribute_info();
  162. foreach ($attributes as $attribute => $info) {
  163. // Merge in the proper default value.
  164. if (isset($item['options']['attributes'][$attribute])) {
  165. // If the menu link already has this attribute, use it.
  166. $info['form']['#default_value'] = $item['options']['attributes'][$attribute];
  167. // Convert the classes array to a string for the form.
  168. if ($attribute == 'class' && is_array($info['form']['#default_value'])) {
  169. $info['form']['#default_value'] = implode(' ', $info['form']['#default_value']);
  170. }
  171. }
  172. elseif ($item['mlid']) {
  173. // If this is an existing link, use the raw default (usually empty).
  174. $info['form']['#default_value'] = $info['default'];
  175. }
  176. $form['options']['attributes'][$attribute] = $info['form'] + array(
  177. '#access' => $info['enabled'],
  178. );
  179. }
  180. // Add form values for the reset of $item['options'] and
  181. // $item['options']['attributes'] so the values will carry over during save.
  182. foreach ($item['options'] as $key => $value) {
  183. if ($key !== 'attributes' && !isset($form['options'][$key])) {
  184. $form['options'][$key] = array(
  185. '#type' => 'value',
  186. '#value' => $value,
  187. );
  188. }
  189. }
  190. if (isset($item['options']['attributes'])) {
  191. foreach ($item['options']['attributes'] as $key => $value) {
  192. if (!isset($form['options']['attributes'][$key])) {
  193. $form['options']['attributes'][$key] = array(
  194. '#type' => 'value',
  195. '#value' => $value,
  196. );
  197. }
  198. }
  199. }
  200. // Hide the 'description' field since we will be using our own 'title' field.
  201. if (isset($form['description'])) {
  202. $form['description']['#access'] = FALSE;
  203. // Because this form uses a special $form['description'] field which is
  204. // really the 'title' attribute, we need to add special pre-submit handling
  205. // to ensure our field gets saved as the title attribute.
  206. array_unshift($complete_form['#submit'], '_menu_attributes_form_submit');
  207. }
  208. $form['options']['attributes']['#access'] = (bool) element_get_visible_children($form['options']['attributes']);
  209. }
  210. /**
  211. * Form submit handler for menu item links.
  212. *
  213. * Move the title attributes value into the 'description' value so that it
  214. * will get properly saved.
  215. */
  216. function _menu_attributes_form_submit($form, &$form_state) {
  217. if (isset($form_state['values']['menu']['options']['attributes']['title'])) {
  218. $form_state['values']['menu']['description'] = $form_state['values']['menu']['options']['attributes']['title'];
  219. }
  220. elseif (isset($form_state['values']['options']['attributes']['title'])) {
  221. $form_state['values']['description'] = $form_state['values']['options']['attributes']['title'];
  222. }
  223. }
  224. /**
  225. * Implements hook_form_FORM_ID_alter().
  226. *
  227. * Alters the menu settings form with our menu attribute settings.
  228. *
  229. * @see menu_configure_form()
  230. */
  231. function menu_attributes_form_menu_configure_alter(&$form, $form_state) {
  232. if (!user_access('administer menu attributes')) {
  233. return;
  234. }
  235. $form['attributes_title'] = array(
  236. '#type' => 'item',
  237. '#title' => t('Menu item attribute options'),
  238. );
  239. $form['attributes_vertical_tabs'] = array(
  240. '#type' => 'vertical_tabs',
  241. '#attached' => array(
  242. 'js' => array(drupal_get_path('module', 'menu_attributes') . '/menu_attributes.js'),
  243. ),
  244. );
  245. $attributes = menu_attributes_get_menu_attribute_info();
  246. foreach ($attributes as $attribute => $info) {
  247. $form['attributes'][$attribute] = array(
  248. '#type' => 'fieldset',
  249. '#title' => $info['label'],
  250. '#group' => 'attributes_vertical_tabs',
  251. '#description' => $info['form']['#description'],
  252. );
  253. $form['attributes'][$attribute]["menu_attributes_{$attribute}_enable"] = array(
  254. '#type' => 'checkbox',
  255. '#title' => t('Enable the @attribute attribute.', array('@attribute' => drupal_strtolower($info['label']))),
  256. '#default_value' => $info['enabled'],
  257. );
  258. $form['attributes'][$attribute]["menu_attributes_{$attribute}_default"] = array(
  259. '#title' => t('Default'),
  260. '#description' => '',
  261. '#states' => array(
  262. 'invisible' => array(
  263. 'input[name="menu_attributes_' . $attribute . '_enable"]' => array('checked' => FALSE),
  264. ),
  265. ),
  266. ) + $info['form'];
  267. }
  268. }