class ContextualHooks
Same name and namespace in other branches
- 11.x core/modules/contextual/src/Hook/ContextualHooks.php \Drupal\contextual\Hook\ContextualHooks
Hook implementations for contextual.
Hierarchy
- class \Drupal\contextual\Hook\ContextualHooks uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of ContextualHooks
File
-
core/
modules/ contextual/ src/ Hook/ ContextualHooks.php, line 13
Namespace
Drupal\contextual\HookView source
class ContextualHooks {
use StringTranslationTrait;
/**
* Implements hook_toolbar().
*/
public function toolbar() : array {
$items = [];
$items['contextual'] = [
'#cache' => [
'contexts' => [
'user.permissions',
],
],
];
if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
return $items;
}
$items['contextual'] += [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => $this->t('Edit'),
'#attributes' => [
'class' => [
'toolbar-icon',
'toolbar-icon-edit',
],
'aria-pressed' => 'false',
'type' => 'button',
],
],
'#wrapper_attributes' => [
'class' => [
'hidden',
'contextual-toolbar-tab',
],
],
'#attached' => [
'library' => [
'contextual/drupal.contextual-toolbar',
],
],
];
return $items;
}
/**
* Implements hook_page_attachments().
*
* Adds the drupal.contextual-links library to the page for any user who has
* the 'access contextual links' permission.
*
* @see contextual_preprocess()
*/
public function pageAttachments(array &$page) : void {
if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
return;
}
$page['#attached']['library'][] = 'contextual/drupal.contextual-links';
}
/**
* Implements hook_help().
*/
public function help($route_name, RouteMatchInterface $route_match) : ?string {
switch ($route_name) {
case 'help.page.contextual':
$output = '';
$output .= '<h2>' . $this->t('About') . '</h2>';
$output .= '<p>' . $this->t('The Contextual links module gives users with the <em>Use contextual links</em> 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 <a href=":contextual">online documentation for the Contextual Links module</a>.', [
':contextual' => 'https://www.drupal.org/docs/8/core/modules/contextual',
]) . '</p>';
$output .= '<h2>' . $this->t('Uses') . '</h2>';
$output .= '<dl>';
$output .= '<dt>' . $this->t('Displaying contextual links') . '</dt>';
$sample_picture = [
'#theme' => 'image',
'#uri' => 'core/misc/icons/bebebe/pencil.svg',
'#alt' => $this->t('contextual links button'),
];
$sample_picture = \Drupal::service('renderer')->render($sample_picture);
$output .= '<dd>' . $this->t('Contextual links for an area on a page are displayed using a contextual links button. 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 Once the contextual links button for the area of interest is visible, click the button to display the links.', [
'@picture' => $sample_picture,
]) . '</dd>';
$output .= '</dl>';
return $output;
}
return NULL;
}
/**
* Implements hook_contextual_links_view_alter().
*
* @see \Drupal\contextual\Plugin\views\field\ContextualLinks::render()
*/
public function contextualLinksViewAlter(&$element, $items) : void {
if (isset($element['#contextual_links']['contextual'])) {
$encoded_links = $element['#contextual_links']['contextual']['metadata']['contextual-views-field-links'];
$element['#links'] = Json::decode(rawurldecode($encoded_links));
}
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| ContextualHooks::contextualLinksViewAlter | public | function | Implements hook_contextual_links_view_alter(). | |
| ContextualHooks::help | public | function | Implements hook_help(). | |
| ContextualHooks::pageAttachments | public | function | Implements hook_page_attachments(). | |
| ContextualHooks::toolbar | public | function | Implements hook_toolbar(). | |
| StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 |
| StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
| StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
| StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.