shortcut.module
Same filename and directory in other branches
File
-
core/
modules/ shortcut/ shortcut.module
View source
<?php
/**
* @file
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\shortcut\ShortcutSetInterface;
/**
* Access callback for editing a shortcut set.
*
* @param Drupal\shortcut\ShortcutSetInterface $shortcut_set
* (optional) The shortcut set to be edited. If not set, the current user's
* shortcut set will be used.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
function shortcut_set_edit_access(?ShortcutSetInterface $shortcut_set = NULL) {
$account = \Drupal::currentUser();
// Shortcut administrators can edit any set.
if ($account->hasPermission('administer shortcuts')) {
return AccessResult::allowed()->cachePerPermissions();
}
// Sufficiently-privileged users can edit their currently displayed shortcut
// set, but not other sets. They must also be able to access shortcuts.
$may_edit_current_shortcut_set = $account->hasPermission('customize shortcut links') && $account->hasPermission('access shortcuts');
if ($may_edit_current_shortcut_set && isset($shortcut_set)) {
$displayed_shortcut_set = \Drupal::entityTypeManager()->getStorage('shortcut_set')
->getDisplayedToUser($account);
$may_edit_current_shortcut_set = $shortcut_set == $displayed_shortcut_set;
}
$result = AccessResult::allowedIf($may_edit_current_shortcut_set)->cachePerPermissions();
if (!$result->isAllowed()) {
$result->setReason("The shortcut set must be the currently displayed set for the user and the user must have 'access shortcuts' AND 'customize shortcut links' permissions.");
}
return $result;
}
/**
* Access callback for switching the shortcut set assigned to a user account.
*
* @param object $account
* (optional) The user account whose shortcuts will be switched. If not set,
* permissions will be checked for switching the logged-in user's own
* shortcut set.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
function shortcut_set_switch_access($account = NULL) {
$user = \Drupal::currentUser();
if ($user->hasPermission('administer shortcuts')) {
// Administrators can switch anyone's shortcut set.
return AccessResult::allowed()->cachePerPermissions();
}
if (!$user->hasPermission('access shortcuts')) {
// The user has no permission to use shortcuts.
return AccessResult::neutral()->cachePerPermissions();
}
if (!$user->hasPermission('switch shortcut sets')) {
// The user has no permission to switch anyone's shortcut set.
return AccessResult::neutral()->cachePerPermissions();
}
// Users with the 'switch shortcut sets' permission can switch their own
// shortcuts sets.
if (!isset($account)) {
return AccessResult::allowed()->cachePerPermissions();
}
elseif ($user->id() == $account->id()) {
return AccessResult::allowed()->cachePerPermissions()
->cachePerUser();
}
// No opinion.
return AccessResult::neutral()->cachePerPermissions();
}
/**
* Returns an array of shortcut links, suitable for rendering.
*
* @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set
* (optional) An object representing the set whose links will be displayed.
* If not provided, the user's current set will be displayed.
*
* @return \Drupal\shortcut\ShortcutInterface[]
* An array of shortcut links, in the format returned by the menu system.
*/
function shortcut_renderable_links($shortcut_set = NULL) : array {
$shortcut_links = [];
if (!isset($shortcut_set)) {
$account = \Drupal::currentUser();
$shortcut_set = \Drupal::entityTypeManager()->getStorage('shortcut_set')
->getDisplayedToUser($account);
}
$cache_tags = [];
foreach ($shortcut_set->getShortcuts() as $shortcut) {
$shortcut = \Drupal::service('entity.repository')->getTranslationFromContext($shortcut);
$url = $shortcut->getUrl();
if ($url->access()) {
$links[$shortcut->id()] = [
'type' => 'link',
'title' => $shortcut->label(),
'url' => $shortcut->getUrl(),
];
$cache_tags = Cache::mergeTags($cache_tags, $shortcut->getCacheTags());
}
}
if (!empty($links)) {
$shortcut_links = [
'#theme' => 'links__toolbar_shortcuts',
'#links' => $links,
'#attributes' => [
'class' => [
'toolbar-menu',
],
],
'#cache' => [
'tags' => $cache_tags,
],
];
}
return $shortcut_links;
}
Functions
Title | Deprecated | Summary |
---|---|---|
shortcut_renderable_links | Returns an array of shortcut links, suitable for rendering. | |
shortcut_set_edit_access | Access callback for editing a shortcut set. | |
shortcut_set_switch_access | Access callback for switching the shortcut set assigned to a user account. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.