class PageContext

Provides the Page Context top bar item.

Hierarchy

Expanded class hierarchy of PageContext

File

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

Namespace

Drupal\navigation\Plugin\TopBarItem
View source
class PageContext extends TopBarItemBase implements ContainerFactoryPluginInterface {
    use StringTranslationTrait;
    
    /**
     * Constructs a new PageContext instance.
     *
     * @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\Core\Entity\EntityTypeManagerInterface $entityTypeManager
     *   The entity type manager service.
     * @param \Drupal\navigation\EntityRouteHelper $entityRouteHelper
     *   The entity route helper service.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityRouteHelper $entityRouteHelper) {
        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(EntityTypeManagerInterface::class), $container->get(EntityRouteHelper::class));
    }
    
    /**
     * {@inheritdoc}
     */
    public function build() : array {
        $build = [
            '#cache' => [
                'contexts' => [
                    'route',
                ],
            ],
        ];
        if (!($entity = $this->entityRouteHelper
            ->getContentEntityFromRoute())) {
            return $build;
        }
        $build += [
            [
                '#type' => 'component',
                '#component' => 'navigation:title',
                '#props' => [
                    'icon' => 'database',
                    'html_tag' => 'span',
                    'modifiers' => [
                        'ellipsis',
                        'xs',
                    ],
                    'extra_classes' => [
                        'top-bar__title',
                    ],
                ],
                '#slots' => [
                    'content' => $entity->label(),
                ],
            ],
        ];
        if ($label = $this->getBadgeLabel($entity)) {
            $build += [
                '#type' => 'component',
                '#component' => 'navigation:badge',
                '#props' => [
                    'status' => $this->getBadgeStatus($entity) ?? 'info',
                ],
                '#slots' => [
                    'label' => $label,
                ],
            ];
        }
        return $build;
    }
    
    /**
     * Retrieves the badge label for the given entity.
     *
     * @param \Drupal\Core\Entity\EntityInterface $entity
     *   The entity for which the label is being retrieved.
     *
     * @return string|null
     *   The translated status if available. NULL otherwise.
     *   The status if available. NULL otherwise.
     */
    protected function getBadgeLabel(EntityInterface $entity) : ?string {
        if (!$entity instanceof EntityPublishedInterface) {
            return NULL;
        }
        return (string) ($entity->isPublished() ? $this->t('Published') : $this->t('Unpublished'));
    }
    
    /**
     * Retrieves the badge status for the given entity.
     *
     * @param \Drupal\Core\Entity\EntityInterface $entity
     *   The entity for which the status is being retrieved.
     *
     * @return string|null
     *   The badge status if available. NULL otherwise.
     */
    protected function getBadgeStatus(EntityInterface $entity) : ?string {
        if (!$entity instanceof EntityPublishedInterface) {
            return NULL;
        }
        return $entity->isPublished() ? 'success' : 'info';
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PageContext::build public function Builds and returns the renderable array for this top bar item plugin. Overrides TopBarItemBase::build
PageContext::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PageContext::getBadgeLabel protected function Retrieves the badge label for the given entity.
PageContext::getBadgeStatus protected function Retrieves the badge status for the given entity.
PageContext::__construct public function Constructs a new PageContext instance. 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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1
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.