class PageActions

Provides the Page Actions basic top bar item.

@internal

Hierarchy

Expanded class hierarchy of PageActions

File

core/modules/navigation/src/Plugin/TopBarItem/PageActions.php, line 22

Namespace

Drupal\navigation\Plugin\TopBarItem
View source
final class PageActions extends TopBarItemBase implements ContainerFactoryPluginInterface {
    
    /**
     * Constructs a PageActions object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin ID for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\navigation\NavigationRenderer $navigationRenderer
     *   The navigation renderer.
     * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
     *   The route match.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, NavigationRenderer $navigationRenderer, RouteMatchInterface $routeMatch) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get(NavigationRenderer::class), $container->get(RouteMatchInterface::class));
    }
    
    /**
     * {@inheritdoc}
     */
    public function build() : array {
        $build = [
            '#cache' => [
                'contexts' => [
                    'route',
                ],
            ],
        ];
        // Local tasks for content entities.
        if (!$this->navigationRenderer
            ->hasLocalTasks()) {
            return $build;
        }
        $page_actions = $this->navigationRenderer
            ->getLocalTasks();
        $featured_page_actions = $this->getFeaturedPageActions($page_actions);
        // Filter actions to exclude featured ones from the main array.
        $page_actions['page_actions'] = array_filter($page_actions['page_actions'], static fn($action_route) => !array_key_exists($action_route, $featured_page_actions), ARRAY_FILTER_USE_KEY);
        $build += [
            '#theme' => 'top_bar_page_actions',
            '#page_actions' => $page_actions['page_actions'],
            '#featured_page_actions' => $featured_page_actions,
        ];
        assert($page_actions['cacheability'] instanceof CacheableMetadata);
        $page_actions['cacheability']->applyTo($build);
        return $build;
    }
    
    /**
     * Gets the featured local task.
     *
     * @param array $page_actions
     *   The array of local tasks for the current page.
     *
     * @return array|null
     *   The featured local task definition if available. NULL otherwise.
     */
    protected function getFeaturedPageActions(array $page_actions) : ?array {
        $featured_page_actions = [];
        $current_route_name = $this->routeMatch
            ->getRouteName();
        $canonical_pattern = '/^entity\\.(.+?)\\.(canonical|latest_version)$/';
        if (preg_match($canonical_pattern, $current_route_name, $matches)) {
            $entity_type = $matches[1];
            $edit_route = "entity.{$entity_type}.edit_form";
            // For core entities, the local task name matches the route name. If
            // needed, we could iterate over the items and check the actual route.
            if (isset($page_actions['page_actions'][$edit_route]) && $page_actions['page_actions'][$edit_route]['#access']?->isAllowed()) {
                $featured_page_actions[$edit_route] = [
                    'page_action' => $page_actions['page_actions'][$edit_route],
                    'icon' => 'thin-pencil',
                ];
            }
        }
        return $featured_page_actions;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
PageActions::build public function Builds and returns the renderable array for this top bar item plugin. Overrides TopBarItemBase::build
PageActions::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PageActions::getFeaturedPageActions protected function Gets the featured local task.
PageActions::__construct public function Constructs a PageActions object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin ID.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable Deprecated public function Determines if the plugin is configurable.
TopBarItemBase::label public function Returns the translated plugin label. Overrides TopBarItemPluginInterface::label
TopBarItemBase::region public function Returns the plugin region. Overrides TopBarItemPluginInterface::region

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.