2009-10-17 00:51:53 +00:00
<?php
/**
* @file
* Allows users to manage customizable lists of shortcut links.
*/
2014-09-17 14:39:29 +00:00
use Drupal\Core\Access\AccessResult;
2014-04-16 10:07:11 +00:00
use Drupal\Component\Utility\UrlHelper;
2014-09-25 07:30:31 +00:00
use Drupal\Core\Cache\Cache;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2013-12-15 09:51:10 +00:00
use Drupal\Core\Routing\UrlMatcher;
2014-02-09 01:29:41 +00:00
use Drupal\Core\Url;
2014-05-20 09:29:40 +00:00
use Drupal\shortcut\Entity\ShortcutSet;
2013-12-15 09:51:10 +00:00
use Drupal\shortcut\ShortcutSetInterface;
2009-10-17 00:51:53 +00:00
2009-12-04 16:02:42 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function shortcut_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.shortcut':
2009-12-04 16:02:42 +00:00
$output = '<h3>' . t('About') . '</h3>';
2013-10-15 17:07:43 +00:00
$output .= '<p>' . t('The Shortcut module allows users to create sets of <em>shortcut</em> links to commonly-visited pages of the site. Shortcuts are contained within <em>sets</em>. Each user with <em>Select any shortcut set</em> permission can select a shortcut set created by anyone at the site. For more information, see the online handbook entry for <a href="!shortcut">Shortcut module</a>.', array('!shortcut' => 'http://drupal.org/documentation/modules/shortcut')) . '</p>';
2009-12-04 16:02:42 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
2010-02-25 15:58:54 +00:00
$output .= '<dl><dt>' . t('Administering shortcuts') . '</dt>';
2013-10-15 17:07:43 +00:00
$output .= '<dd>' . t('Users with the <em>Administer shortcuts</em> permission can manage shortcut sets and edit the shortcuts within sets from the <a href="!shortcuts">Shortcuts administration page</a>.', array('!shortcuts' => \Drupal::url('shortcut.set_admin'))) . '</dd>';
2010-02-25 15:58:54 +00:00
$output .= '<dt>' . t('Choosing shortcut sets') . '</dt>';
$output .= '<dd>' . t('Users with permission to switch shortcut sets can choose a shortcut set to use from the Shortcuts tab of their user account page.') . '</dd>';
2009-12-04 16:02:42 +00:00
$output .= '<dt>' . t('Adding and removing shortcuts') . '</dt>';
2010-02-25 15:58:54 +00:00
$output .= '<dd>' . t('The Shortcut module creates an add/remove link for each page on your site; the link lets you add or remove the current page from the currently-enabled set of shortcuts (if your theme displays it and you have permission to edit your shortcut set). The core Seven administration theme displays this link next to the page title, as a small + or - sign. If you click on the + sign, you will add that page to your preferred set of shortcuts. If the page is already part of your shortcut set, the link will be a - sign, and will allow you to remove the current page from your shortcut set.') . '</dd>';
2009-12-04 16:02:42 +00:00
$output .= '<dt>' . t('Displaying shortcuts') . '</dt>';
2013-10-15 17:07:43 +00:00
$output .= '<dd>' . t('You can display your shortcuts by enabling the Shortcuts block on the <a href="!blocks">Blocks administration page</a>. Certain administrative modules also display your shortcuts; for example, the core <a href="!toolbar-help">Toolbar module</a> displays them near the top of the page, along with an <em>Edit shortcuts</em> link.', array('!blocks' => \Drupal::url('block.admin_display'), '!toolbar-help' => \Drupal::url('help.page', array('name' => 'toolbar')))) . '</dd>';
2009-12-04 16:02:42 +00:00
$output .= '</dl>';
return $output;
2010-02-25 15:58:54 +00:00
2014-05-07 02:04:53 +00:00
case 'shortcut.set_admin':
case 'shortcut.set_add':
2014-08-04 17:06:25 +00:00
case 'entity.shortcut_set.edit_form':
2014-05-07 02:04:53 +00:00
$user = \Drupal::currentUser();
2014-06-09 23:13:41 +00:00
if ($user->hasPermission('access shortcuts') && $user->hasPermission('switch shortcut sets')) {
2014-09-27 07:03:46 +00:00
$output = '<p>' . t('Define which shortcut set you are using on the <a href="@shortcut-link">Shortcuts tab</a> of your account page.', array('@shortcut-link' => \Drupal::url('shortcut.set_switch', array('user' => $user->id())))) . '</p>';
2010-02-25 15:58:54 +00:00
return $output;
}
2009-12-04 16:02:42 +00:00
}
}
2010-01-07 11:03:18 +00:00
2009-10-17 00:51:53 +00:00
/**
* Access callback for editing a shortcut set.
*
2013-12-15 09:51:10 +00:00
* @param Drupal\shortcut\ShortcutSetInterface $shortcut_set
2010-02-25 15:58:54 +00:00
* (optional) The shortcut set to be edited. If not set, the current user's
* shortcut set will be used.
*
2014-09-17 14:39:29 +00:00
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
2009-10-17 00:51:53 +00:00
*/
2013-12-15 09:51:10 +00:00
function shortcut_set_edit_access(ShortcutSetInterface $shortcut_set = NULL) {
2013-09-16 03:58:06 +00:00
$account = \Drupal::currentUser();
2014-06-09 23:13:41 +00:00
// Shortcut administrators can edit any set.
2013-09-04 21:18:30 +00:00
if ($account->hasPermission('administer shortcuts')) {
2014-09-17 14:39:29 +00:00
return AccessResult::allowed()->cachePerRole();
2014-06-09 23:13:41 +00:00
}
2014-09-17 14:39:29 +00:00
2014-06-09 23:13:41 +00:00
// Sufficiently-privileged users can edit their currently displayed shortcut
2014-09-17 14:39:29 +00:00
// set, but not other sets. They must also be able to access shortcuts.
$may_edit_current_shortcut_set = $account->hasPermission('customize shortcut links') && (!isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set()) && $account->hasPermission('access shortcuts');
return AccessResult::allowedIf($may_edit_current_shortcut_set)->cachePerRole();
2009-10-17 00:51:53 +00:00
}
/**
* Access callback for switching the shortcut set assigned to a user account.
*
2010-02-25 15:58:54 +00:00
* @param object $account
2009-10-17 00:51:53 +00:00
* (optional) The user account whose shortcuts will be switched. If not set,
2010-02-25 15:58:54 +00:00
* permissions will be checked for switching the logged-in user's own
* shortcut set.
*
2014-09-17 14:39:29 +00:00
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
2009-10-17 00:51:53 +00:00
*/
function shortcut_set_switch_access($account = NULL) {
2013-09-16 03:58:06 +00:00
$user = \Drupal::currentUser();
2010-02-25 15:58:54 +00:00
2013-09-04 21:18:30 +00:00
if ($user->hasPermission('administer shortcuts')) {
2010-02-25 15:58:54 +00:00
// Administrators can switch anyone's shortcut set.
2014-09-17 14:39:29 +00:00
return AccessResult::allowed()->cachePerRole();
2010-02-25 15:58:54 +00:00
}
2014-06-09 23:13:41 +00:00
if (!$user->hasPermission('access shortcuts')) {
// The user has no permission to use shortcuts.
2014-09-22 21:08:27 +00:00
return AccessResult::neutral()->cachePerRole();
2014-06-09 23:13:41 +00:00
}
2013-09-04 21:18:30 +00:00
if (!$user->hasPermission('switch shortcut sets')) {
2010-02-25 15:58:54 +00:00
// The user has no permission to switch anyone's shortcut set.
2014-09-22 21:08:27 +00:00
return AccessResult::neutral()->cachePerRole();
2010-02-25 15:58:54 +00:00
}
2014-09-17 14:39:29 +00:00
// Users with the 'switch shortcut sets' permission can switch their own
// shortcuts sets.
if (!isset($account)) {
return AccessResult::allowed()->cachePerRole();
}
else if ($user->id() == $account->id()) {
return AccessResult::allowed()->cachePerRole()->cachePerUser();
2010-02-25 15:58:54 +00:00
}
2014-09-17 14:39:29 +00:00
// No opinion.
2014-09-22 21:08:27 +00:00
return AccessResult::neutral()->cachePerRole();
2009-10-17 00:51:53 +00:00
}
/**
2010-02-25 15:58:54 +00:00
* Assigns a user to a particular shortcut set.
2009-10-17 00:51:53 +00:00
*
2013-08-18 21:16:19 +00:00
* @param $shortcut_set Drupal\shortcut\Entity\Shortcut
2009-10-17 00:51:53 +00:00
* An object representing the shortcut set.
* @param $account
* A user account that will be assigned to use the set.
*/
function shortcut_set_assign_user($shortcut_set, $account) {
2013-09-16 03:58:06 +00:00
\Drupal::entityManager()
2014-03-27 11:54:40 +00:00
->getStorage('shortcut_set')
2013-06-16 09:40:11 +00:00
->assignUser($shortcut_set, $account);
2009-10-17 00:51:53 +00:00
}
/**
2010-02-25 15:58:54 +00:00
* Unassigns a user from any shortcut set they may have been assigned to.
2009-10-17 00:51:53 +00:00
*
* The user will go back to using whatever default set applies.
*
* @param $account
* A user account that will be removed from the shortcut set assignment.
2010-02-25 15:58:54 +00:00
*
2009-10-17 00:51:53 +00:00
* @return
* TRUE if the user was previously assigned to a shortcut set and has been
* successfully removed from it. FALSE if the user was already not assigned
* to any set.
*/
function shortcut_set_unassign_user($account) {
2013-09-16 03:58:06 +00:00
return (bool) \Drupal::entityManager()
2014-03-27 11:54:40 +00:00
->getStorage('shortcut_set')
2013-06-16 09:40:11 +00:00
->unassignUser($account);
2009-10-17 00:51:53 +00:00
}
/**
* Returns the current displayed shortcut set for the provided user account.
*
* @param $account
* (optional) The user account whose shortcuts will be returned. Defaults to
2010-02-25 15:58:54 +00:00
* the currently logged-in user.
*
2009-10-17 00:51:53 +00:00
* @return
* An object representing the shortcut set that should be displayed to the
* current user. If the user does not have an explicit shortcut set defined,
* the default set is returned.
*/
function shortcut_current_displayed_set($account = NULL) {
$shortcut_sets = &drupal_static(__FUNCTION__, array());
2013-10-02 21:42:16 +00:00
$user = \Drupal::currentUser();
2009-10-17 00:51:53 +00:00
if (!isset($account)) {
$account = $user;
}
// Try to return a shortcut set from the static cache.
2013-07-11 17:29:02 +00:00
if (isset($shortcut_sets[$account->id()])) {
return $shortcut_sets[$account->id()];
2009-10-17 00:51:53 +00:00
}
// If none was found, try to find a shortcut set that is explicitly assigned
// to this user.
2013-09-16 03:58:06 +00:00
$shortcut_set_name = \Drupal::entityManager()
2014-03-27 11:54:40 +00:00
->getStorage('shortcut_set')
2013-06-16 09:40:11 +00:00
->getAssignedToUser($account);
2009-11-11 06:56:52 +00:00
if ($shortcut_set_name) {
2014-05-20 09:29:40 +00:00
$shortcut_set = ShortcutSet::load($shortcut_set_name);
2009-11-11 06:56:52 +00:00
}
2009-10-17 00:51:53 +00:00
// Otherwise, use the default set.
2010-01-30 07:59:26 +00:00
else {
2009-10-17 00:51:53 +00:00
$shortcut_set = shortcut_default_set($account);
}
2009-11-11 06:56:52 +00:00
2013-07-11 17:29:02 +00:00
$shortcut_sets[$account->id()] = $shortcut_set;
2009-10-17 00:51:53 +00:00
return $shortcut_set;
}
/**
* Returns the default shortcut set for a given user account.
*
2010-02-25 15:58:54 +00:00
* @param object $account
* (optional) The user account whose default shortcut set will be returned.
* If not provided, the function will return the currently logged-in user's
* default shortcut set.
*
2009-10-17 00:51:53 +00:00
* @return
* An object representing the default shortcut set.
*/
function shortcut_default_set($account = NULL) {
2013-10-02 21:42:16 +00:00
$user = \Drupal::currentUser();
2009-10-17 00:51:53 +00:00
if (!isset($account)) {
$account = $user;
}
2010-02-25 15:58:54 +00:00
2009-10-17 00:51:53 +00:00
// Allow modules to return a default shortcut set name. Since we can only
// have one, we allow the last module which returns a valid result to take
// precedence. If no module returns a valid set, fall back on the site-wide
2010-02-25 15:58:54 +00:00
// default, which is the lowest-numbered shortcut set.
2013-09-16 03:58:06 +00:00
$suggestions = array_reverse(\Drupal::moduleHandler()->invokeAll('shortcut_default_set', array($account)));
2013-01-19 05:26:12 +00:00
$suggestions[] = 'default';
2010-02-25 15:58:54 +00:00
foreach ($suggestions as $name) {
2014-05-20 09:29:40 +00:00
if ($shortcut_set = ShortcutSet::load($name)) {
2009-10-17 00:51:53 +00:00
break;
}
}
2010-02-25 15:58:54 +00:00
2009-10-17 00:51:53 +00:00
return $shortcut_set;
}
2010-06-30 15:12:59 +00:00
/**
* Check to see if a shortcut set with the given title already exists.
*
* @param $title
* Human-readable name of the shortcut set to check.
*
* @return
* TRUE if a shortcut set with that title exists; FALSE otherwise.
*/
function shortcut_set_title_exists($title) {
2014-08-18 14:23:37 +00:00
$sets = ShortcutSet::loadMultiple();
2013-09-05 02:59:33 +00:00
foreach ($sets as $set) {
2013-01-19 05:26:12 +00:00
if ($set->label == $title) {
return TRUE;
}
}
return FALSE;
2010-06-30 15:12:59 +00:00
}
2009-10-17 00:51:53 +00:00
/**
* Returns an array of shortcut links, suitable for rendering.
*
2013-12-15 09:51:10 +00:00
* @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set
2009-10-17 00:51:53 +00:00
* (optional) An object representing the set whose links will be displayed.
* If not provided, the user's current set will be displayed.
2013-12-15 09:51:10 +00:00
*
* @return \Drupal\shortcut\ShortcutInterface[]
2009-10-17 00:51:53 +00:00
* An array of shortcut links, in the format returned by the menu system.
*/
function shortcut_renderable_links($shortcut_set = NULL) {
2013-12-15 09:51:10 +00:00
$shortcut_links = array();
2009-10-17 00:51:53 +00:00
if (!isset($shortcut_set)) {
$shortcut_set = shortcut_current_displayed_set();
}
2013-12-15 09:51:10 +00:00
2014-05-12 09:58:30 +00:00
/** @var \Drupal\shortcut\ShortcutInterface[] $shortcuts */
2014-03-27 11:54:40 +00:00
$shortcuts = \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(array('shortcut_set' => $shortcut_set->id()));
2014-09-25 07:30:31 +00:00
$cache_tags = array();
2013-12-15 09:51:10 +00:00
foreach ($shortcuts as $shortcut) {
2014-09-03 15:34:18 +00:00
$shortcut = \Drupal::entityManager()->getTranslationFromContext($shortcut);
2013-12-15 09:51:10 +00:00
$links[] = array(
'title' => $shortcut->label(),
'href' => $shortcut->path->value,
);
2014-09-25 07:30:31 +00:00
$cache_tags = Cache::mergeTags($cache_tags, $shortcut->getCacheTag());
2013-12-15 09:51:10 +00:00
}
if (!empty($links)) {
$shortcut_links = array(
'#theme' => 'links__toolbar_shortcuts',
'#links' => $links,
'#attributes' => array(
'class' => array('menu'),
),
2014-05-12 09:58:30 +00:00
'#cache' => array(
2014-09-25 07:30:31 +00:00
'tags' => $cache_tags,
2014-05-12 09:58:30 +00:00
),
2013-12-15 09:51:10 +00:00
);
}
return $shortcut_links;
2009-10-17 00:51:53 +00:00
}
2012-01-10 03:22:24 +00:00
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK() for block templates.
2012-01-10 03:22:24 +00:00
*/
function shortcut_preprocess_block(&$variables) {
2014-04-01 21:14:13 +00:00
if ($variables['configuration']['provider'] == 'shortcut') {
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'navigation';
2012-01-10 03:22:24 +00:00
}
}
2009-10-17 00:51:53 +00:00
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK() for page templates.
2009-10-17 00:51:53 +00:00
*/
2010-01-04 03:57:19 +00:00
function shortcut_preprocess_page(&$variables) {
2011-09-28 03:06:39 +00:00
// Only display the shortcut link if the user has the ability to edit
// shortcuts and if the page's actual content is being shown (for example,
// we do not want to display it on "access denied" or "page not found"
// pages).
2014-09-17 14:39:29 +00:00
if (shortcut_set_edit_access()->isAllowed() && !\Drupal::request()->attributes->has('exception')) {
2012-04-29 15:16:27 +00:00
$link = current_path();
2014-09-15 09:22:01 +00:00
if (!($url = \Drupal::pathValidator()->getUrlIfValid($link))) {
2013-12-15 09:51:10 +00:00
// Bail out early if we couldn't find a matching route.
return;
2009-10-17 00:51:53 +00:00
}
2013-12-15 09:51:10 +00:00
2009-10-17 00:51:53 +00:00
$query = array(
2014-02-24 20:12:19 +00:00
'link' => $link,
'name' => $variables['title'],
2009-10-17 00:51:53 +00:00
);
$query += drupal_get_destination();
$shortcut_set = shortcut_current_displayed_set();
2009-11-11 06:56:52 +00:00
// Check if $link is already a shortcut and set $link_mode accordingly.
2014-03-27 11:54:40 +00:00
$shortcuts = \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(array('shortcut_set' => $shortcut_set->id()));
2013-12-15 09:51:10 +00:00
foreach ($shortcuts as $shortcut) {
2014-02-09 01:29:41 +00:00
if ($shortcut->getRouteName() == $url->getRouteName() && $shortcut->getRouteParams() == $url->getRouteParameters()) {
2013-12-15 09:51:10 +00:00
$shortcut_id = $shortcut->id();
2009-11-11 06:56:52 +00:00
break;
}
}
2013-12-15 09:51:10 +00:00
$link_mode = isset($shortcut_id) ? "remove" : "add";
2009-11-11 06:56:52 +00:00
if ($link_mode == "add") {
2014-09-17 14:39:29 +00:00
$link_text = shortcut_set_switch_access()->isAllowed() ? t('Add to %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->label())) : t('Add to shortcuts');
2013-11-11 11:38:41 +00:00
$route_name = 'shortcut.link_add_inline';
$route_parameters = array('shortcut_set' => $shortcut_set->id());
2009-11-11 06:56:52 +00:00
}
else {
2013-12-15 09:51:10 +00:00
$query['id'] = $shortcut_id;
2014-09-17 14:39:29 +00:00
$link_text = shortcut_set_switch_access()->isAllowed() ? t('Remove from %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->label())) : t('Remove from shortcuts');
2014-09-08 16:48:41 +00:00
$route_name = 'entity.shortcut.link_delete_inline';
2013-12-15 09:51:10 +00:00
$route_parameters = array('shortcut' => $shortcut_id);
2009-11-11 06:56:52 +00:00
}
2010-01-14 06:47:54 +00:00
if (theme_get_setting('shortcut_module_link')) {
$variables['title_suffix']['add_or_remove_shortcut'] = array(
2012-03-27 06:33:36 +00:00
'#attached' => array(
2013-08-28 08:49:00 +00:00
'library' => array(
2014-03-09 19:59:45 +00:00
'shortcut/drupal.shortcut',
2012-03-27 06:33:36 +00:00
),
),
2010-01-14 06:47:54 +00:00
'#prefix' => '<div class="add-or-remove-shortcuts ' . $link_mode . '-shortcut">',
'#type' => 'link',
2014-02-08 20:57:42 +00:00
'#title' => '<span class="icon"></span><span class="text">'. $link_text .'</span>',
2013-11-11 11:38:41 +00:00
'#route_name' => $route_name,
'#route_parameters' => $route_parameters,
2010-01-14 06:47:54 +00:00
'#options' => array('query' => $query, 'html' => TRUE),
'#suffix' => '</div>',
);
}
2009-10-17 00:51:53 +00:00
}
2009-11-19 03:57:15 +00:00
}
/**
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
* Implements hook_toolbar().
2009-11-19 03:57:15 +00:00
*/
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
function shortcut_toolbar() {
2014-06-09 23:13:41 +00:00
$user = \Drupal::currentUser();
2013-02-01 16:31:47 +00:00
$items = array();
2014-06-09 23:13:41 +00:00
if ($user->hasPermission('access shortcuts')) {
$links = shortcut_renderable_links();
$shortcut_set = shortcut_current_displayed_set();
$configure_link = NULL;
2014-09-17 14:39:29 +00:00
if (shortcut_set_edit_access($shortcut_set)->isAllowed()) {
2014-06-09 23:13:41 +00:00
$configure_link = array(
2013-02-01 16:31:47 +00:00
'#type' => 'link',
2014-06-09 23:13:41 +00:00
'#title' => t('Edit shortcuts'),
2014-08-04 17:06:25 +00:00
'#route_name' => 'entity.shortcut_set.customize_form',
2014-06-09 23:13:41 +00:00
'#route_parameters' => array('shortcut_set' => $shortcut_set->id()),
'#options' => array('attributes' => array('class' => array('edit-shortcuts'))),
);
}
if (!empty($links) || !empty($configure_link)) {
$items['shortcuts'] = array(
'#type' => 'toolbar_item',
'tab' => array(
'#type' => 'link',
'#title' => t('Shortcuts'),
'#href' => 'admin/config/user-interface/shortcut',
'#attributes' => array(
'title' => t('Shortcuts'),
'class' => array('toolbar-icon', 'toolbar-icon-shortcut'),
),
2013-02-01 16:31:47 +00:00
),
2014-06-09 23:13:41 +00:00
'tray' => array(
'#heading' => t('User-defined shortcuts'),
'shortcuts' => $links,
'configure' => $configure_link,
2013-02-01 16:31:47 +00:00
),
2014-06-09 23:13:41 +00:00
'#weight' => -10,
'#attached' => array(
'library' => array(
'shortcut/drupal.shortcut',
),
),
);
}
2013-02-01 16:31:47 +00:00
}
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
return $items;
2009-10-17 00:51:53 +00:00
}