You are here

class ToolbarMenuPrerender in Toolbar Menu 8.2

Defines a service for toolbar menu prerender elements.

Hierarchy

Expanded class hierarchy of ToolbarMenuPrerender

1 file declares its use of ToolbarMenuPrerender
toolbar_menu.module in ./toolbar_menu.module
Add menu entries in the Drupal Toolbar.

File

src/ToolbarMenuPrerender.php, line 11

Namespace

Drupal\toolbar_menu
View source
class ToolbarMenuPrerender implements TrustedCallbackInterface {

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

  /**
   * Pre-render the toolbar_menu tray element.
   *
   * @param array $element
   *   The tray element to pre-render.
   *
   * @return array
   *   The pre-rendered tray element.
   */
  public static function prerenderToolbarTray(array $element) {

    /** @var \Drupal\toolbar\Menu\ToolbarMenuLinkTree $menu_tree */
    $menu_tree = \Drupal::service('toolbar.menu_tree');
    $parameters = new MenuTreeParameters();
    $parameters
      ->excludeRoot()
      ->onlyEnabledLinks();
    $tree = $menu_tree
      ->load($element['#id'], $parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
      [
        'callable' => 'toolbar_menu.menu_link_tree_manipulators:addIcons',
      ],
    ];
    $tree = $menu_tree
      ->transform($tree, $manipulators);
    $element['toolbar_menu_' . $element['#id']] = $menu_tree
      ->build($tree);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ToolbarMenuPrerender::prerenderToolbarTray public static function Pre-render the toolbar_menu tray element.
ToolbarMenuPrerender::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.