contextual.module

You are here

  1. 7 modules/contextual/contextual.module
  2. 8 core/modules/contextual/contextual.module

Adds contextual links to perform actions related to elements on a page.

Functions

Namesort descending Description
contextual_element_info Implements hook_element_info().
contextual_help Implements hook_help().
contextual_library Implements hook_library().
contextual_permission Implements hook_permission().
contextual_preprocess Implements hook_preprocess().
contextual_pre_render_links Build a renderable array for contextual links.

File

modules/contextual/contextual.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Adds contextual links to perform actions related to elements on a page.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function contextual_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#contextual':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Contextual links module displays links related to regions of pages on your site to users with <em>access contextual links</em> permission. For more information, see the online handbook entry for <a href="@contextual">Contextual links module</a>.', array('@contextual' => 'http://drupal.org/documentation/modules/contextual')) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Displaying contextual links') . '</dt>';
  18. $output .= '<dd>' . t('Contextual links are supplied by modules, to give you quick access to tasks associated with regions of pages on your site. For instance, if you have a custom menu block displayed in a sidebar of your site, the Blocks and Menus modules will supply links to configure the block and edit the menu. The Contextual links module collects these links into a list for display by your theme, and also adds JavaScript code to the page to hide the links initially, and display them when your mouse hovers over the block.') . '</dd>';
  19. $output .= '</dl>';
  20. return $output;
  21. }
  22. }
  23. /**
  24. * Implements hook_permission().
  25. */
  26. function contextual_permission() {
  27. return array(
  28. 'access contextual links' => array(
  29. 'title' => t('Use contextual links'),
  30. 'description' => t('Use contextual links to perform actions related to elements on a page.'),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_library().
  36. */
  37. function contextual_library() {
  38. $path = drupal_get_path('module', 'contextual');
  39. $libraries['contextual-links'] = array(
  40. 'title' => 'Contextual links',
  41. 'website' => 'http://drupal.org/node/473268',
  42. 'version' => '1.0',
  43. 'js' => array(
  44. $path . '/contextual.js' => array(),
  45. ),
  46. 'css' => array(
  47. $path . '/contextual.css' => array(),
  48. ),
  49. );
  50. return $libraries;
  51. }
  52. /**
  53. * Implements hook_element_info().
  54. */
  55. function contextual_element_info() {
  56. $types['contextual_links'] = array(
  57. '#pre_render' => array('contextual_pre_render_links'),
  58. '#theme' => 'links__contextual',
  59. '#links' => array(),
  60. '#prefix' => '<div class="contextual-links-wrapper">',
  61. '#suffix' => '</div>',
  62. '#attributes' => array(
  63. 'class' => array('contextual-links'),
  64. ),
  65. '#attached' => array(
  66. 'library' => array(
  67. array('contextual', 'contextual-links'),
  68. ),
  69. ),
  70. );
  71. return $types;
  72. }
  73. /**
  74. * Implements hook_preprocess().
  75. *
  76. * @see contextual_pre_render_links()
  77. */
  78. function contextual_preprocess(&$variables, $hook) {
  79. // Nothing to do here if the user is not permitted to access contextual links.
  80. if (!user_access('access contextual links')) {
  81. return;
  82. }
  83. $hooks = theme_get_registry(FALSE);
  84. // Determine the primary theme function argument.
  85. if (!empty($hooks[$hook]['variables'])) {
  86. $keys = array_keys($hooks[$hook]['variables']);
  87. $key = $keys[0];
  88. }
  89. elseif (!empty($hooks[$hook]['render element'])) {
  90. $key = $hooks[$hook]['render element'];
  91. }
  92. if (!empty($key) && isset($variables[$key])) {
  93. $element = $variables[$key];
  94. }
  95. if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
  96. // Initialize the template variable as a renderable array.
  97. $variables['title_suffix']['contextual_links'] = array(
  98. '#type' => 'contextual_links',
  99. '#contextual_links' => $element['#contextual_links'],
  100. '#element' => $element,
  101. );
  102. // Mark this element as potentially having contextual links attached to it.
  103. $variables['classes_array'][] = 'contextual-links-region';
  104. }
  105. }
  106. /**
  107. * Build a renderable array for contextual links.
  108. *
  109. * @param $element
  110. * A renderable array containing a #contextual_links property, which is a
  111. * keyed array. Each key is the name of the implementing module, and each
  112. * value is an array that forms the function arguments for
  113. * menu_contextual_links(). For example:
  114. * @code
  115. * array('#contextual_links' => array(
  116. * 'block' => array('admin/structure/block/manage', array('system', 'navigation')),
  117. * 'menu' => array('admin/structure/menu/manage', array('navigation')),
  118. * ))
  119. * @endcode
  120. *
  121. * @return
  122. * A renderable array representing contextual links.
  123. *
  124. * @see menu_contextual_links()
  125. * @see contextual_element_info()
  126. */
  127. function contextual_pre_render_links($element) {
  128. // Retrieve contextual menu links.
  129. $items = array();
  130. foreach ($element['#contextual_links'] as $module => $args) {
  131. $items += menu_contextual_links($module, $args[0], $args[1]);
  132. }
  133. // Transform contextual links into parameters suitable for theme_link().
  134. $links = array();
  135. foreach ($items as $class => $item) {
  136. $class = drupal_html_class($class);
  137. $links[$class] = array(
  138. 'title' => $item['title'],
  139. 'href' => $item['href'],
  140. );
  141. // @todo theme_links() should *really* use the same parameters as l().
  142. $item['localized_options'] += array('query' => array());
  143. $item['localized_options']['query'] += drupal_get_destination();
  144. $links[$class] += $item['localized_options'];
  145. }
  146. $element['#links'] = $links;
  147. // Allow modules to alter the renderable contextual links element.
  148. drupal_alter('contextual_links_view', $element, $items);
  149. // If there are no links, tell drupal_render() to abort rendering.
  150. if (empty($element['#links'])) {
  151. $element['#printed'] = TRUE;
  152. }
  153. return $element;
  154. }