Issue #1011614 by catch: Fixed Theme registry can grow too large for MySQL max_allowe...
[project/drupal.git] / modules / contextual / contextual.module
CommitLineData
66f740c5 1<?php
66f740c5
DB
2
3/**
4 * @file
5 * Adds contextual links to perform actions related to elements on a page.
6 */
7
169656a7
DB
8/**
9 * Implements hook_help().
10 */
11function contextual_help($path, $arg) {
12 switch ($path) {
13 case 'admin/help#contextual':
14 $output = '';
15 $output .= '<h3>' . t('About') . '</h3>';
16 $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/handbook/modules/contextual')) . '</p>';
17 $output .= '<h3>' . t('Uses') . '</h3>';
18 $output .= '<dl>';
19 $output .= '<dt>' . t('Displaying contextual links') . '</dt>';
20 $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>';
21 $output .= '</dl>';
22 return $output;
23 }
24}
25
66f740c5
DB
26/**
27 * Implements hook_permission().
28 */
29function contextual_permission() {
30 return array(
31 'access contextual links' => array(
32 'title' => t('Use contextual links'),
33 'description' => t('Use contextual links to perform actions related to elements on a page.'),
34 ),
35 );
36}
37
38/**
6fd99dba 39 * Implements hook_library_info().
66f740c5 40 */
6fd99dba 41function contextual_library_info() {
66f740c5
DB
42 $path = drupal_get_path('module', 'contextual');
43 $libraries['contextual-links'] = array(
44 'title' => 'Contextual links',
45 'website' => 'http://drupal.org/node/473268',
46 'version' => '1.0',
47 'js' => array(
48 $path . '/contextual.js' => array(),
49 ),
50 'css' => array(
51 $path . '/contextual.css' => array(),
52 ),
53 );
54 return $libraries;
55}
56
57/**
84e5d10b
DB
58 * Implements hook_element_info().
59 */
60function contextual_element_info() {
61 $types['contextual_links'] = array(
62 '#pre_render' => array('contextual_pre_render_links'),
63 '#theme' => 'links__contextual',
64 '#links' => array(),
65 '#prefix' => '<div class="contextual-links-wrapper">',
66 '#suffix' => '</div>',
67 '#attributes' => array(
68 'class' => array('contextual-links'),
69 ),
70 '#attached' => array(
71 'library' => array(
72 array('contextual', 'contextual-links'),
73 ),
74 ),
75 );
76 return $types;
77}
78
79/**
66f740c5
DB
80 * Template variable preprocessor for contextual links.
81 *
84e5d10b 82 * @see contextual_pre_render_links()
66f740c5
DB
83 */
84function contextual_preprocess(&$variables, $hook) {
66f740c5
DB
85 // Nothing to do here if the user is not permitted to access contextual links.
86 if (!user_access('access contextual links')) {
87 return;
88 }
89
256d8504 90 $hooks = theme_get_registry(FALSE);
66f740c5
DB
91
92 // Determine the primary theme function argument.
ad8eacac 93 if (!empty($hooks[$hook]['variables'])) {
66f740c5
DB
94 $keys = array_keys($hooks[$hook]['variables']);
95 $key = $keys[0];
96 }
dca92900 97 elseif (!empty($hooks[$hook]['render element'])) {
66f740c5
DB
98 $key = $hooks[$hook]['render element'];
99 }
83e9590a 100 if (!empty($key) && isset($variables[$key])) {
66f740c5
DB
101 $element = $variables[$key];
102 }
103
104 if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
84e5d10b
DB
105 // Initialize the template variable as a renderable array.
106 $variables['title_suffix']['contextual_links'] = array(
107 '#type' => 'contextual_links',
108 '#contextual_links' => $element['#contextual_links'],
109 '#element' => $element,
110 );
111 // Mark this element as potentially having contextual links attached to it.
112 $variables['classes_array'][] = 'contextual-links-region';
66f740c5
DB
113 }
114}
115
116/**
117 * Build a renderable array for contextual links.
118 *
119 * @param $element
120 * A renderable array containing a #contextual_links property, which is a
121 * keyed array. Each key is the name of the implementing module, and each
122 * value is an array that forms the function arguments for
123 * menu_contextual_links(). For example:
124 * @code
125 * array('#contextual_links' => array(
126 * 'block' => array('admin/structure/block/manage', array('system', 'navigation')),
127 * 'menu' => array('admin/structure/menu/manage', array('navigation')),
128 * ))
129 * @endcode
130 *
131 * @return
132 * A renderable array representing contextual links.
133 *
134 * @see menu_contextual_links()
135 */
84e5d10b 136function contextual_pre_render_links($element) {
66f740c5
DB
137 // Retrieve contextual menu links.
138 $items = array();
139 foreach ($element['#contextual_links'] as $module => $args) {
140 $items += menu_contextual_links($module, $args[0], $args[1]);
141 }
142
143 // Transform contextual links into parameters suitable for theme_link().
66f740c5
DB
144 $links = array();
145 foreach ($items as $class => $item) {
146 $class = drupal_html_class($class);
147 $links[$class] = array(
148 'title' => $item['title'],
149 'href' => $item['href'],
150 );
84e5d10b
DB
151 // @todo theme_links() should *really* use the same parameters as l().
152 $item['localized_options'] += array('query' => array());
153 $item['localized_options']['query'] += drupal_get_destination();
66f740c5
DB
154 $links[$class] += $item['localized_options'];
155 }
84e5d10b
DB
156 $element['#links'] = $links;
157
158 // Allow modules to alter the renderable contextual links element.
159 drupal_alter('contextual_links_view', $element, $items);
160
161 // If there are no links, tell drupal_render() to abort rendering.
162 if (empty($element['#links'])) {
163 $element['#printed'] = TRUE;
66f740c5 164 }
84e5d10b
DB
165
166 return $element;
66f740c5
DB
167}
168