Amswap.php in Admin Menu Swap 8
File
src/Render/Element/Amswap.php
View source
<?php
namespace Drupal\amswap\Render\Element;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Security\TrustedCallbackInterface;
class Amswap implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRender',
];
}
public static function preRender(array $element) {
$parameters = new MenuTreeParameters();
$parameters
->onlyEnabledLinks();
$toolbar_menu_tree = \Drupal::service('toolbar.menu_tree');
$current_user = \Drupal::currentUser();
$role_menu_pairs = \Drupal::config('amswap.amswapconfig')
->get('role_menu_pairs');
$menu_specified = FALSE;
$trees = [];
if (is_array($role_menu_pairs) || is_object($role_menu_pairs)) {
foreach ($role_menu_pairs as $pair) {
if (in_array($pair['role'], $current_user
->getRoles(), FALSE)) {
$parameters
->setMinDepth(1)
->setMaxDepth(4);
$trees[] = $toolbar_menu_tree
->load($pair['menu'], $parameters);
$menu_specified = TRUE;
}
}
}
if (!$menu_specified) {
return $element;
}
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
if (function_exists('toolbar_tools_menu_navigation_links')) {
$manipulators[] = [
'callable' => 'toolbar_tools_menu_navigation_links',
];
}
else {
$manipulators[] = [
'callable' => 'toolbar_menu_navigation_links',
];
}
$element['administration_menu'] = [];
foreach ($trees as $tree) {
$tree = $toolbar_menu_tree
->transform($tree, $manipulators);
$element['administration_menu'] = NestedArray::mergeDeep($toolbar_menu_tree
->build($tree), $element['administration_menu']);
}
return $element;
}
}
Classes
Name |
Description |
Amswap |
Provides a pre-render callback for the administration menu. |