summaryrefslogtreecommitdiff
path: root/modules/contextual
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-09-11 14:35:13 +0000
committerDries Buytaert <dries@buytaert.net>2010-09-11 14:35:13 +0000
commit84e5d10b66a19211fea8792816bfafc65a446478 (patch)
treeee514d05f0a5dbb7ca452fd581ae90d546cc45a4 /modules/contextual
parent452e31f62924d35b71576aa8291812014a80561e (diff)
downloadbrdo-84e5d10b66a19211fea8792816bfafc65a446478.tar.gz
brdo-84e5d10b66a19211fea8792816bfafc65a446478.tar.bz2
- Patch #627288 by sun, David_Rothstein: contextual links are not alterable.
Diffstat (limited to 'modules/contextual')
-rw-r--r--modules/contextual/contextual.api.php41
-rw-r--r--modules/contextual/contextual.module76
2 files changed, 86 insertions, 31 deletions
diff --git a/modules/contextual/contextual.api.php b/modules/contextual/contextual.api.php
new file mode 100644
index 000000000..e445bbcac
--- /dev/null
+++ b/modules/contextual/contextual.api.php
@@ -0,0 +1,41 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Hooks provided by Contextual module.
+ */
+
+/**
+ * @addtogroup hooks
+ * @{
+ */
+
+/**
+ * Alter a contextual links element before it is rendered.
+ *
+ * This hook is invoked by contextual_pre_render_links(). The renderable array
+ * of #type 'contextual_links', containing the entire contextual links data that
+ * is passed in by reference. Further links may be added or existing links can
+ * be altered.
+ *
+ * @param $element
+ * A renderable array representing the contextual links.
+ * @param $items
+ * An associative array containing the original contextual link items, as
+ * generated by menu_contextual_links(), which were used to build
+ * $element['#links'].
+ *
+ * @see hook_menu_contextual_links_alter()
+ * @see contextual_pre_render_links()
+ * @see contextual_element_info()
+ */
+function hook_contextual_links_view_alter(&$element, $items) {
+ // Add another class to all contextual link lists to facilitate custom
+ // styling.
+ $element['#attributes']['class'][] = 'custom-class';
+}
+
+/**
+ * @} End of "addtogroup hooks".
+ */
diff --git a/modules/contextual/contextual.module b/modules/contextual/contextual.module
index c5b76ceba..f0e6e032c 100644
--- a/modules/contextual/contextual.module
+++ b/modules/contextual/contextual.module
@@ -6,7 +6,6 @@
* Adds contextual links to perform actions related to elements on a page.
*/
-
/**
* Implements hook_help().
*/
@@ -57,9 +56,31 @@ function contextual_library() {
}
/**
+ * Implements hook_element_info().
+ */
+function contextual_element_info() {
+ $types['contextual_links'] = array(
+ '#pre_render' => array('contextual_pre_render_links'),
+ '#theme' => 'links__contextual',
+ '#links' => array(),
+ '#prefix' => '<div class="contextual-links-wrapper">',
+ '#suffix' => '</div>',
+ '#attributes' => array(
+ 'class' => array('contextual-links'),
+ ),
+ '#attached' => array(
+ 'library' => array(
+ array('contextual', 'contextual-links'),
+ ),
+ ),
+ );
+ return $types;
+}
+
+/**
* Template variable preprocessor for contextual links.
*
- * @see contextual_links_view()
+ * @see contextual_pre_render_links()
*/
function contextual_preprocess(&$variables, $hook) {
static $hooks;
@@ -86,10 +107,14 @@ function contextual_preprocess(&$variables, $hook) {
}
if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
- $variables['title_suffix']['contextual_links'] = contextual_links_view($element);
- if (!empty($variables['title_suffix']['contextual_links'])) {
- $variables['classes_array'][] = 'contextual-links-region';
- }
+ // Initialize the template variable as a renderable array.
+ $variables['title_suffix']['contextual_links'] = array(
+ '#type' => 'contextual_links',
+ '#contextual_links' => $element['#contextual_links'],
+ '#element' => $element,
+ );
+ // Mark this element as potentially having contextual links attached to it.
+ $variables['classes_array'][] = 'contextual-links-region';
}
}
@@ -113,9 +138,7 @@ function contextual_preprocess(&$variables, $hook) {
*
* @see menu_contextual_links()
*/
-function contextual_links_view($element) {
- static $destination;
-
+function contextual_pre_render_links($element) {
// Retrieve contextual menu links.
$items = array();
foreach ($element['#contextual_links'] as $module => $args) {
@@ -123,10 +146,6 @@ function contextual_links_view($element) {
}
// Transform contextual links into parameters suitable for theme_link().
- if (!isset($destination)) {
- $destination = drupal_get_destination();
- }
-
$links = array();
foreach ($items as $class => $item) {
$class = drupal_html_class($class);
@@ -134,26 +153,21 @@ function contextual_links_view($element) {
'title' => $item['title'],
'href' => $item['href'],
);
- // @todo theme_links() should *really* use the same parameters as l()...
- if (!isset($item['localized_options']['query'])) {
- $item['localized_options']['query'] = array();
- }
- $item['localized_options']['query'] += $destination;
+ // @todo theme_links() should *really* use the same parameters as l().
+ $item['localized_options'] += array('query' => array());
+ $item['localized_options']['query'] += drupal_get_destination();
$links[$class] += $item['localized_options'];
}
- $build = array();
- if ($links) {
- $build = array(
- '#prefix' => '<div class="contextual-links-wrapper">',
- '#suffix' => '</div>',
- '#theme' => 'links__contextual',
- '#links' => $links,
- '#attributes' => array('class' => array('contextual-links')),
- '#attached' => array(
- 'library' => array(array('contextual', 'contextual-links')),
- ),
- );
+ $element['#links'] = $links;
+
+ // Allow modules to alter the renderable contextual links element.
+ drupal_alter('contextual_links_view', $element, $items);
+
+ // If there are no links, tell drupal_render() to abort rendering.
+ if (empty($element['#links'])) {
+ $element['#printed'] = TRUE;
}
- return $build;
+
+ return $element;
}