hasPermission('access contextual links')) { return; } $tab['contextual'] = array( 'tab' => array( '#type' => 'html_tag', '#tag' => 'button', '#value' => t('Edit'), '#attributes' => array( 'class' => array('toolbar-icon', 'toolbar-icon-edit'), 'role' => 'button', 'aria-pressed' => 'false', ), ), '#theme_wrappers' => array('toolbar_tab_wrapper'), '#wrapper_attributes' => array( 'class' => array('hidden', 'contextual-toolbar-tab'), ), '#attached' => array( 'library' => array( array('contextual', 'drupal.contextual-toolbar'), ), ), ); return $tab; } /** * Implements hook_page_build(). * * Adds the drupal.contextual-links library to the page for any user who has the * 'access contextual links' permission. * * @see contextual_preprocess() */ function contextual_page_build(&$page) { if (!\Drupal::currentUser()->hasPermission('access contextual links')) { return; } $page['#attached']['library'][] = array('contextual', 'drupal.contextual-links'); } /** * Implements hook_help(). */ function contextual_help($path, $arg) { switch ($path) { case 'admin/help#contextual': $output = ''; $output .= '

' . t('About') . '

'; $output .= '

' . t('The Contextual Links module displays links related to regions of pages on your site to users with access contextual links permission. For more information, see the online handbook entry for Contextual Links module.', array('@contextual' => 'http://drupal.org/documentation/modules/contextual')) . '

'; $output .= '

' . t('Uses') . '

'; $output .= '
'; $output .= '
' . t('Displaying contextual links') . '
'; $output .= '
' . 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.') . '
'; $output .= '
'; return $output; } } /** * Implements hook_permission(). */ function contextual_permission() { return array( 'access contextual links' => array( 'title' => t('Use contextual links'), 'description' => t('Use contextual links to perform actions related to elements on a page.'), ), ); } /** * Implements hook_library_info(). */ function contextual_library_info() { $path = drupal_get_path('module', 'contextual'); // Add the JavaScript, with a group and weight such that it will run // before core/modules/contextual/js/contextual.toolbar.js. $options = array( 'group' => JS_LIBRARY, 'weight' => -2, ); $libraries['drupal.contextual-links'] = array( 'title' => 'Contextual Links', 'website' => 'http://drupal.org/node/473268', 'version' => \Drupal::VERSION, 'js' => array( // Core. $path . '/js/contextual.js' => $options, // Models. $path . '/js/models/StateModel.js' => $options, // Views. $path . '/js/views/AuralView.js' => $options, $path . '/js/views/KeyboardView.js' => $options, $path . '/js/views/RegionView.js' => $options, $path . '/js/views/VisualView.js' => $options, ), 'css' => array( $path . '/css/contextual.module.css' => array(), $path . '/css/contextual.theme.css' => array(), $path . '/css/contextual.icons.css' => array(), ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), array('system', 'drupalSettings'), array('system', 'backbone'), array('system', 'modernizr'), array('system', 'jquery.once'), ), ); $libraries['drupal.contextual-toolbar'] = array( 'title' => 'Contextual Links Toolbar Tab', 'version' => \Drupal::VERSION, 'js' => array( $path . '/js/contextual.toolbar.js' => array('group' => JS_LIBRARY), ), 'css' => array( $path . '/css/contextual.toolbar.css' => array(), ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), array('system', 'backbone'), array('system', 'jquery.once'), array('system', 'drupal.tabbingmanager'), array('system', 'drupal.announce'), ), ); return $libraries; } /** * Implements hook_element_info(). */ function contextual_element_info() { $types['contextual_links_placeholder'] = array( '#pre_render' => array('contextual_pre_render_placeholder'), '#id' => NULL, ); $types['contextual_links'] = array( '#pre_render' => array('contextual_pre_render_links'), '#theme' => 'links__contextual', '#links' => array(), '#attributes' => array('class' => array('contextual-links')), '#attached' => array( 'library' => array( array('contextual', 'drupal.contextual-links'), ), ), ); return $types; } /** * Implements hook_preprocess(). * * @see contextual_pre_render_placeholder() * @see contextual_page_build() * @see \Drupal\contextual\ContextualController::render() */ function contextual_preprocess(&$variables, $hook, $info) { // Determine the primary theme function argument. if (!empty($info['variables'])) { $keys = array_keys($info['variables']); $key = $keys[0]; } elseif (!empty($info['render element'])) { $key = $info['render element']; } if (!empty($key) && isset($variables[$key])) { $element = $variables[$key]; } if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { // Mark this element as potentially having contextual links attached to it. $variables['attributes']['class'][] = 'contextual-region'; // Renders a contextual links placeholder unconditionally, thus not breaking // the render cache. Although the empty placeholder is rendered for all // users, contextual_page_build() only adds the drupal.contextual-links // library for users with the 'access contextual links' permission, thus // preventing unnecessary HTTP requests for users without that permission. $variables['title_suffix']['contextual_links'] = array( '#type' => 'contextual_links_placeholder', '#id' => _contextual_links_to_id($element['#contextual_links']), ); } } /** * Pre-render callback: Renders a contextual links placeholder into #markup. * * Renders an empty (hence invisible) placeholder div with a data-attribute that * contains an identifier ("contextual id"), which allows the JavaScript of the * drupal.contextual-links library to dynamically render contextual links. * * @param $element * A structured array with #id containing a "contextual id". * * @return * The passed-in element with a contextual link placeholder in '#markup'. * * @see _contextual_links_to_id() * @see contextual_element_info() */ function contextual_pre_render_placeholder($element) { $element['#markup'] = ' $element['#id'])) . '>'; return $element; } /** * Pre-render callback: Builds a renderable array for contextual links. * * @param $element * A renderable array containing a #contextual_links property, which is a * keyed array. Each key is the name of the group of contextual links to * render (based on the 'group' key in the *.contextual_links.yml files for * all enabled modules). The value contains an associative array containing * the following keys: * - route_parameters: The route parameters passed to the url generator. * - metadata: Any additional data needed in order to alter the link. * @code * array('#contextual_links' => array( * 'block' => array( * 'route_parameters' => array('block' => 'system.menu-tools'), * ), * 'menu' => array( * 'route_parameters' => array('menu' => 'tools'), * ), * )) * @endcode * * @return * A renderable array representing contextual links. * * @see contextual_element_info() */ function contextual_pre_render_links($element) { // Retrieve contextual menu links. $items = array(); /** @var $contextual_links_manager \Drupal\Core\Menu\ContextualLinkManager */ $contextual_links_manager = \Drupal::service('plugin.manager.menu.contextual_link'); foreach ($element['#contextual_links'] as $group => $args) { $args += array( 'route_parameters' => array(), 'metadata' => array(), ); $items += $contextual_links_manager->getContextualLinksArrayByGroup($group, $args['route_parameters'], $args['metadata']); } // Transform contextual links into parameters suitable for theme_links(). $links = array(); foreach ($items as $class => $item) { $class = drupal_html_class($class); $links[$class] = array( 'title' => $item['title'], 'route_name' => isset($item['route_name']) ? $item['route_name'] : '', 'route_parameters' => isset($item['route_parameters']) ? $item['route_parameters'] : array(), ); $item['localized_options'] += array('query' => array()); $item['localized_options']['query'] += drupal_get_destination(); $links[$class] += $item['localized_options']; } $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 $element; } /** * Implements hook_contextual_links_view_alter(). * * @see \Drupal\contextual\Plugin\views\field\ContextualLinks::render() */ function contextual_contextual_links_view_alter(&$element, $items) { if (isset($element['#contextual_links']['contextual'])) { $encoded_links = $element['#contextual_links']['contextual']['metadata']['contextual-views-field-links']; $element['#links'] = drupal_json_decode(rawurldecode($encoded_links)); } } /** * Serializes #contextual_links property value array to a string. * * Examples: * - node:node=1: * - views_ui_edit:view=frontpage:location=page&view_name=frontpage&view_display_id=page_1 * - menu:menu=tools:|block:block=bartik.tools: * * So, expressed in a pattern: * :: * * The route parameters and options are encoded as query strings. * * @param array $contextual_links * The $element['#contextual_links'] value for some render element. * * @return string * A serialized representation of a #contextual_links property value array for * use in a data- attribute. */ function _contextual_links_to_id($contextual_links) { $ids = array(); foreach ($contextual_links as $group => $args) { $route_parameters = Url::buildQuery($args['route_parameters']); $metadata = Url::buildQuery((isset($args['metadata'])) ? $args['metadata'] : array()); $ids[] = "{$group}:{$route_parameters}:{$metadata}"; } return implode('|', $ids); } /** * Unserializes the result of _contextual_links_to_id(). * * @see _contextual_links_to_id * * @param string $id * A serialized representation of a #contextual_links property value array. * * @return array * The value for a #contextual_links property. */ function _contextual_id_to_links($id) { $contextual_links = array(); $contexts = explode('|', $id); foreach ($contexts as $context) { list($group, $route_parameters_raw, $metadata_raw) = explode(':', $context); parse_str($route_parameters_raw, $route_parameters); $metadata = array(); parse_str($metadata_raw, $metadata); $contextual_links[$group] = array( 'route_parameters' => $route_parameters, 'metadata' => $metadata, ); } return $contextual_links; }