Project

General

Profile

Paste
Download (5.55 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / contextual / contextual.module @ 175ce36f

1
<?php
2

    
3
/**
4
 * @file
5
 * Adds contextual links to perform actions related to elements on a page.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function 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/documentation/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

    
26
/**
27
 * Implements hook_permission().
28
 */
29
function 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
/**
39
 * Implements hook_library().
40
 */
41
function contextual_library() {
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
/**
58
 * Implements hook_element_info().
59
 */
60
function 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
/**
80
 * Implements hook_preprocess().
81
 *
82
 * @see contextual_pre_render_links()
83
 */
84
function contextual_preprocess(&$variables, $hook) {
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

    
90
  $hooks = theme_get_registry(FALSE);
91

    
92
  // Determine the primary theme function argument.
93
  if (!empty($hooks[$hook]['variables'])) {
94
    $keys = array_keys($hooks[$hook]['variables']);
95
    $key = $keys[0];
96
  }
97
  elseif (!empty($hooks[$hook]['render element'])) {
98
    $key = $hooks[$hook]['render element'];
99
  }
100
  if (!empty($key) && isset($variables[$key])) {
101
    $element = $variables[$key];
102
  }
103

    
104
  if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
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';
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
 * @see contextual_element_info()
136
 */
137
function contextual_pre_render_links($element) {
138
  // Retrieve contextual menu links.
139
  $items = array();
140
  foreach ($element['#contextual_links'] as $module => $args) {
141
    $items += menu_contextual_links($module, $args[0], $args[1]);
142
  }
143

    
144
  // Transform contextual links into parameters suitable for theme_link().
145
  $links = array();
146
  foreach ($items as $class => $item) {
147
    $class = drupal_html_class($class);
148
    $links[$class] = array(
149
      'title' => $item['title'],
150
      'href' => $item['href'],
151
    );
152
    // @todo theme_links() should *really* use the same parameters as l().
153
    $item['localized_options'] += array('query' => array());
154
    $item['localized_options']['query'] += drupal_get_destination();
155
    $links[$class] += $item['localized_options'];
156
  }
157
  $element['#links'] = $links;
158

    
159
  // Allow modules to alter the renderable contextual links element.
160
  drupal_alter('contextual_links_view', $element, $items);
161

    
162
  // If there are no links, tell drupal_render() to abort rendering.
163
  if (empty($element['#links'])) {
164
    $element['#printed'] = TRUE;
165
  }
166

    
167
  return $element;
168
}
169