Chris@0: [ Chris@0: 'contexts' => [ Chris@0: 'user.permissions', Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: Chris@0: if (!\Drupal::currentUser()->hasPermission('access contextual links')) { Chris@0: return $items; Chris@0: } Chris@0: Chris@0: $items['contextual'] += [ Chris@0: '#type' => 'toolbar_item', Chris@0: 'tab' => [ Chris@0: '#type' => 'html_tag', Chris@0: '#tag' => 'button', Chris@0: '#value' => t('Edit'), Chris@0: '#attributes' => [ Chris@0: 'class' => ['toolbar-icon', 'toolbar-icon-edit'], Chris@0: 'aria-pressed' => 'false', Chris@0: 'type' => 'button', Chris@0: ], Chris@0: ], Chris@0: '#wrapper_attributes' => [ Chris@0: 'class' => ['hidden', 'contextual-toolbar-tab'], Chris@0: ], Chris@0: '#attached' => [ Chris@0: 'library' => [ Chris@0: 'contextual/drupal.contextual-toolbar', Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: Chris@0: return $items; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_page_attachments(). Chris@0: * Chris@0: * Adds the drupal.contextual-links library to the page for any user who has the Chris@0: * 'access contextual links' permission. Chris@0: * Chris@0: * @see contextual_preprocess() Chris@0: */ Chris@0: function contextual_page_attachments(array &$page) { Chris@0: if (!\Drupal::currentUser()->hasPermission('access contextual links')) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $page['#attached']['library'][] = 'contextual/drupal.contextual-links'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_help(). Chris@0: */ Chris@0: function contextual_help($route_name, RouteMatchInterface $route_match) { Chris@0: switch ($route_name) { Chris@0: case 'help.page.contextual': Chris@0: $output = ''; Chris@0: $output .= '

' . t('About') . '

'; Chris@0: $output .= '

' . t('The Contextual links module gives users with the Use contextual links permission quick access to tasks associated with certain areas of pages on your site. For example, a menu displayed as a block has links to edit the menu and configure the block. For more information, see the online documentation for the Contextual Links module.', [':contextual' => 'https://www.drupal.org/documentation/modules/contextual']) . '

'; Chris@0: $output .= '

' . t('Uses') . '

'; Chris@0: $output .= '
'; Chris@0: $output .= '
' . t('Displaying contextual links') . '
'; Chris@0: $output .= '
'; Chris@0: $output .= t('Contextual links for an area on a page are displayed using a contextual links button. There are two ways to make the contextual links button visible:'); Chris@0: $output .= '
    '; Chris@0: $sample_picture = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => 'core/misc/icons/bebebe/pencil.svg', Chris@17: '#alt' => t('contextual links button'), Chris@0: ]; Chris@0: $sample_picture = \Drupal::service('renderer')->render($sample_picture); Chris@0: $output .= '
  1. ' . t('Hovering over the area of interest will temporarily make the contextual links button visible (which looks like a pencil in most themes, and is normally displayed in the upper right corner of the area). The icon typically looks like this: @picture', ['@picture' => $sample_picture]) . '
  2. '; Chris@18: $output .= '
  3. ' . t('If you have the Toolbar module enabled, clicking the contextual links button in the toolbar (which looks like a pencil) will make all contextual links buttons on the page visible. Clicking this button again will toggle them to invisible.', [':toolbar' => (\Drupal::moduleHandler()->moduleExists('toolbar')) ? Url::fromRoute('help.page', ['name' => 'toolbar'])->toString() : '#']) . '
  4. '; Chris@0: $output .= '
'; Chris@0: $output .= t('Once the contextual links button for the area of interest is visible, click the button to display the links.'); Chris@0: $output .= '
'; Chris@0: $output .= '
'; Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess(). Chris@0: * Chris@0: * @see contextual_pre_render_placeholder() Chris@0: * @see contextual_page_attachments() Chris@0: * @see \Drupal\contextual\ContextualController::render() Chris@0: */ Chris@0: function contextual_preprocess(&$variables, $hook, $info) { Chris@0: $variables['#cache']['contexts'][] = 'user.permissions'; Chris@0: if (!\Drupal::currentUser()->hasPermission('access contextual links')) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Determine the primary theme function argument. Chris@0: if (!empty($info['variables'])) { Chris@0: $keys = array_keys($info['variables']); Chris@0: $key = $keys[0]; Chris@0: } Chris@0: elseif (!empty($info['render element'])) { Chris@0: $key = $info['render element']; Chris@0: } Chris@0: if (!empty($key) && isset($variables[$key])) { Chris@0: $element = $variables[$key]; Chris@0: } Chris@0: Chris@0: if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { Chris@0: // Mark this element as potentially having contextual links attached to it. Chris@0: $variables['attributes']['class'][] = 'contextual-region'; Chris@0: Chris@0: // Renders a contextual links placeholder unconditionally, thus not breaking Chris@0: // the render cache. Although the empty placeholder is rendered for all Chris@0: // users, contextual_page_attachments() only adds the asset library for Chris@0: // users with the 'access contextual links' permission, thus preventing Chris@0: // unnecessary HTTP requests for users without that permission. Chris@0: $variables['title_suffix']['contextual_links'] = [ Chris@0: '#type' => 'contextual_links_placeholder', Chris@0: '#id' => _contextual_links_to_id($element['#contextual_links']), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_contextual_links_view_alter(). Chris@0: * Chris@0: * @see \Drupal\contextual\Plugin\views\field\ContextualLinks::render() Chris@0: */ Chris@0: function contextual_contextual_links_view_alter(&$element, $items) { Chris@0: if (isset($element['#contextual_links']['contextual'])) { Chris@0: $encoded_links = $element['#contextual_links']['contextual']['metadata']['contextual-views-field-links']; Chris@0: $element['#links'] = Json::decode(rawurldecode($encoded_links)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Serializes #contextual_links property value array to a string. Chris@0: * Chris@0: * Examples: Chris@0: * - node:node=1:langcode=en Chris@0: * - views_ui_edit:view=frontpage:location=page&view_name=frontpage&view_display_id=page_1&langcode=en Chris@0: * - menu:menu=tools:langcode=en|block:block=bartik.tools:langcode=en Chris@0: * Chris@0: * So, expressed in a pattern: Chris@0: * :: Chris@0: * Chris@0: * The route parameters and options are encoded as query strings. Chris@0: * Chris@0: * @param array $contextual_links Chris@0: * The $element['#contextual_links'] value for some render element. Chris@0: * Chris@0: * @return string Chris@0: * A serialized representation of a #contextual_links property value array for Chris@0: * use in a data- attribute. Chris@0: */ Chris@0: function _contextual_links_to_id($contextual_links) { Chris@0: $ids = []; Chris@0: $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId(); Chris@0: foreach ($contextual_links as $group => $args) { Chris@0: $route_parameters = UrlHelper::buildQuery($args['route_parameters']); Chris@0: $args += ['metadata' => []]; Chris@0: // Add the current URL language to metadata so a different ID will be Chris@0: // computed when URLs vary by language. This allows to store different Chris@0: // language-aware contextual links on the client side. Chris@0: $args['metadata'] += ['langcode' => $langcode]; Chris@0: $metadata = UrlHelper::buildQuery($args['metadata']); Chris@0: $ids[] = "{$group}:{$route_parameters}:{$metadata}"; Chris@0: } Chris@0: return implode('|', $ids); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unserializes the result of _contextual_links_to_id(). Chris@0: * Chris@17: * Note that $id is user input. Before calling this method the ID should be Chris@17: * checked against the token stored in the 'data-contextual-token' attribute Chris@17: * which is passed via the 'tokens' request parameter to Chris@17: * \Drupal\contextual\ContextualController::render(). Chris@0: * Chris@0: * @param string $id Chris@0: * A serialized representation of a #contextual_links property value array. Chris@0: * Chris@0: * @return array Chris@0: * The value for a #contextual_links property. Chris@17: * Chris@17: * @see _contextual_links_to_id() Chris@17: * @see \Drupal\contextual\ContextualController::render() Chris@0: */ Chris@0: function _contextual_id_to_links($id) { Chris@0: $contextual_links = []; Chris@0: $contexts = explode('|', $id); Chris@0: foreach ($contexts as $context) { Chris@0: list($group, $route_parameters_raw, $metadata_raw) = explode(':', $context); Chris@0: parse_str($route_parameters_raw, $route_parameters); Chris@0: $metadata = []; Chris@0: parse_str($metadata_raw, $metadata); Chris@0: $contextual_links[$group] = [ Chris@0: 'route_parameters' => $route_parameters, Chris@0: 'metadata' => $metadata, Chris@0: ]; Chris@0: } Chris@0: return $contextual_links; Chris@0: }