Update to Drupal 8.0.0 beta 14. For more information, see https://drupal.org/node/2544542

This commit is contained in:
Pantheon Automation 2015-08-27 12:03:05 -07:00 committed by Greg Anderson
parent 3b2511d96d
commit 81ccda77eb
2155 changed files with 54307 additions and 46870 deletions

View file

@ -6,6 +6,7 @@
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
@ -13,7 +14,6 @@ use Drupal\Core\Template\Attribute;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\user\Entity\Role;
use Drupal\Core\Url;
/**
@ -158,10 +158,9 @@ function toolbar_toolbar() {
// toolbar_subtrees route. We provide the JavaScript requesting that JSONP
// script here with the hash parameter that is needed for that route.
// @see toolbar_subtrees_jsonp()
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
list($hash, $hash_cacheability) = _toolbar_get_subtrees_hash();
$subtrees_attached['drupalSettings']['toolbar'] = [
'subtreesHash' => _toolbar_get_subtrees_hash($langcode),
'langcode' => $langcode,
'subtreesHash' => $hash,
];
// The administration element has a link that is themed to correspond to
@ -198,6 +197,7 @@ function toolbar_toolbar() {
),
'#weight' => -15,
);
$hash_cacheability->applyTo($items['administration']);
return $items;
}
@ -215,10 +215,13 @@ function toolbar_toolbar() {
*/
function toolbar_prerender_toolbar_administration_tray(array $element) {
$menu_tree = \Drupal::service('toolbar.menu_tree');
// Render the top-level administration menu links.
// Load the administrative menu. The first level is the "Administration" link.
// In order to load the children of that link, start and end on the second
// level.
$parameters = new MenuTreeParameters();
$parameters->setRoot('system.admin')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
$tree = $menu_tree->load(NULL, $parameters);
$parameters->setMinDepth(2)->setMaxDepth(2)->onlyEnabledLinks();
// @todo Make the menu configurable in https://www.drupal.org/node/1869638.
$tree = $menu_tree->load('admin', $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
@ -248,7 +251,7 @@ function toolbar_menu_navigation_links(array $tree) {
// and behaviors to the menu links.
$link = $element->link;
$url = $link->getUrlObject();
if ($url->isExternal()) {
if (!$url->isRouted()) {
// This is an unusual case, so just get a distinct, safe string.
$id = substr(Crypt::hashBase64($url->getUri()), 0, 16);
}
@ -269,12 +272,38 @@ function toolbar_menu_navigation_links(array $tree) {
/**
* Returns the rendered subtree of each top-level toolbar link.
*
* @return array
* An array with the following key-value pairs:
* - 'subtrees': the rendered subtrees
* - 'cacheability: the associated cacheability.
*/
function toolbar_get_rendered_subtrees() {
$data = [
'#pre_render' => ['_toolbar_do_get_rendered_subtrees'],
'#cache' => [
'keys' => [
'toolbar_rendered_subtrees',
],
],
'#cache_properties' => ['#subtrees'],
];
\Drupal::service('renderer')->renderPlain($data);
return [$data['#subtrees'], CacheableMetadata::createFromRenderArray($data)];
}
/**
* #pre_render callback for toolbar_get_rendered_subtrees().
*/
function _toolbar_do_get_rendered_subtrees(array $data) {
$menu_tree = \Drupal::service('toolbar.menu_tree');
// Load the administration menu. The first level is the "Administration" link.
// In order to load the children of that link and the subsequent two levels,
// start at the second level and end at the fourth.
$parameters = new MenuTreeParameters();
$parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth(3)->onlyEnabledLinks();
$tree = $menu_tree->load(NULL, $parameters);
$parameters->setMinDepth(2)->setMaxDepth(4)->onlyEnabledLinks();
// @todo Make the menu configurable in https://www.drupal.org/node/1869638.
$tree = $menu_tree->load('admin', $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
@ -282,65 +311,42 @@ function toolbar_get_rendered_subtrees() {
);
$tree = $menu_tree->transform($tree, $manipulators);
$subtrees = array();
// Calculated the combined cacheability of all subtrees.
$cacheability = new CacheableMetadata();
foreach ($tree as $element) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $element->link;
if ($element->subtree) {
$subtree = $menu_tree->build($element->subtree);
$output = \Drupal::service('renderer')->renderPlain($subtree);
$cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($subtree));
}
else {
$output = '';
}
// Many routes have dots as route name, while some special ones like <front>
// have <> characters in them.
$id = str_replace(array('.', '<', '>'), array('-', '', '' ), $link->getUrlObject()->getRouteName());
$url = $link->getUrlObject();
$id = str_replace(array('.', '<', '>'), array('-', '', '' ), $url->isRouted() ? $url->getRouteName() : $url->getUri());
$subtrees[$id] = $output;
}
return $subtrees;
// Store the subtrees, along with the cacheability metadata.
$cacheability->applyTo($data);
$data['#subtrees'] = $subtrees;
return $data;
}
/**
* Returns the hash of the per-user rendered toolbar subtrees.
*
* @param string $langcode
* The langcode of the current request.
*
* @return string
* The hash of the admin_menu subtrees.
*/
function _toolbar_get_subtrees_hash($langcode) {
$uid = \Drupal::currentUser()->id();
$cid = _toolbar_get_user_cid($uid, $langcode);
if ($cache = \Drupal::cache('toolbar')->get($cid)) {
$hash = $cache->data;
}
else {
$subtrees = toolbar_get_rendered_subtrees();
$hash = Crypt::hashBase64(serialize($subtrees));
// Cache using a tag 'user' so that we can invalidate all user-specific
// caches later, based on the user's ID regardless of language.
// Clear the cache when the 'locale' tag is deleted. This ensures a fresh
// subtrees rendering when string translations are made.
$role_list_cache_tags = \Drupal::entityManager()->getDefinition('user_role')->getListCacheTags();
\Drupal::cache('toolbar')->set($cid, $hash, Cache::PERMANENT, Cache::mergeTags(array('user:' . $uid, 'locale', 'config:system.menu.admin'), $role_list_cache_tags));
}
return $hash;
function _toolbar_get_subtrees_hash() {
list($subtrees, $cacheability) = toolbar_get_rendered_subtrees();
$hash = Crypt::hashBase64(serialize($subtrees));
return [$hash, $cacheability];
}
/**
* Returns a cache ID from the user and language IDs.
*
* @param int $uid
* A user ID.
* @param string $langcode
* The langcode of the current request.
*
* @return string
* A unique cache ID for the user.
*/
function _toolbar_get_user_cid($uid, $langcode) {
return 'toolbar_' . $uid . ':' . $langcode;
}