You are here

class Amswap in Admin Menu Swap 8

Provides a pre-render callback for the administration menu.

Hierarchy

Expanded class hierarchy of Amswap

1 file declares its use of Amswap
amswap.module in ./amswap.module
Contains amswap.module.

File

src/Render/Element/Amswap.php, line 12

Namespace

Drupal\amswap\Render\Element
View source
class Amswap implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRender',
    ];
  }

  /**
   * Pre-render callback to replace the default menu with a role specific menu.
   *
   * @param array $element
   *   The administration toolbar.
   *
   * @return array
   *   The altered element.
   */
  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 the specified role matches the users current role
        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 un-altered
      return $element;
    }

    // Add manipulators
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];
    if (function_exists('toolbar_tools_menu_navigation_links')) {

      // If Admin Toolbar module is installed, use its toolbar manipulation function
      $manipulators[] = [
        'callable' => 'toolbar_tools_menu_navigation_links',
      ];
    }
    else {

      // Otherwise use Toolbar module's toolbar manipulation function
      $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Amswap::preRender public static function Pre-render callback to replace the default menu with a role specific menu.
Amswap::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.