class PageContext
Same name and namespace in other branches
- 11.x core/modules/navigation/src/Plugin/TopBarItem/PageContext.php \Drupal\navigation\Plugin\TopBarItem\PageContext
Provides the Page Context top bar item.
Attributes
#[TopBarItem(id: 'page_context', region: TopBarRegion::Context, label: new TranslatableMarkup('Page Context'))]
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\navigation\TopBarItemBase implements \Drupal\navigation\TopBarItemPluginInterface extends \Drupal\Component\Plugin\PluginBase
- class \Drupal\navigation\Plugin\TopBarItem\PageContext implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait extends \Drupal\navigation\TopBarItemBase
- class \Drupal\navigation\TopBarItemBase implements \Drupal\navigation\TopBarItemPluginInterface extends \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of PageContext
1 file declares its use of PageContext
- PageContextTest.php in core/
modules/ navigation/ tests/ src/ Unit/ PageContextTest.php
File
-
core/
modules/ navigation/ src/ Plugin/ TopBarItem/ PageContext.php, line 24
Namespace
Drupal\navigation\Plugin\TopBarItemView 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\navigation\EntityRouteHelper $entityRouteHelper
* The entity route helper service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository.
* @param \Drupal\content_moderation\ModerationInformationInterface|null $moderationInformation
* The moderation information service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, protected EntityRouteHelper $entityRouteHelper, protected EntityRepositoryInterface $entityRepository, protected ?ModerationInformationInterface $moderationInformation = NULL) {
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(EntityRouteHelper::class), $container->get(EntityRepositoryInterface::class), $container->get(ModerationInformationInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
/**
* {@inheritdoc}
*/
public function build() : array {
$build = [
'#cache' => [
'contexts' => [
'route',
],
],
];
if (!$entity = $this->entityRouteHelper
->getContentEntityFromRoute()) {
return $build;
}
$entity_label = $this->getEntityLabel($entity);
if (!$entity_label) {
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 ($badge_label = $this->getBadgeLabel($entity)) {
$build[] = [
'#type' => 'component',
'#component' => 'navigation:badge',
'#props' => [
'status' => $this->getBadgeStatus($entity) ?? 'info',
],
'#slots' => [
'label' => $badge_label,
],
];
}
return $build;
}
/**
* Retrieves the label of the given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity from which the label will be retrieved.
*
* @return string|null
* The label as a string if available, NULL otherwise.
*/
protected function getEntityLabel(EntityInterface $entity) : ?string {
$label = $entity->label();
if (is_string($label)) {
return $label;
}
if ($label instanceof \Stringable) {
return (string) $label;
}
return NULL;
}
/**
* 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 ContentEntityInterface && $this->moderationInformation && $this->moderationInformation
->isModeratedEntity($entity)) {
$state_label = $this->moderationInformation
->getWorkflowForEntity($entity)
->getTypePlugin()
->getState($entity->get('moderation_state')->value)
->label();
if ($this->moderationInformation
->hasPendingRevision($entity) && $entity->isDefaultRevision()) {
$active_revision = $this->entityRepository
->getActive($entity->getEntityTypeId(), $entity->id());
$active_state_label = $this->moderationInformation
->getWorkflowForEntity($entity)
->getTypePlugin()
->getState($active_revision->get('moderation_state')->value)
->label();
$state_label = $this->t('@state_label (@active_state_label available)', [
'@state_label' => $state_label,
'@active_state_label' => $active_state_label,
]);
}
return (string) $state_label;
}
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 | 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::getEntityLabel | protected | function | Retrieves the label of 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. | 1 | |
| PluginBase::$pluginDefinition | protected | property | The plugin implementation definition. | 1 | |
| 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 | 2 |
| PluginBase::getPluginId | public | function | Gets the plugin ID of the plugin instance. | Overrides PluginInspectionInterface::getPluginId | |
| 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 | |
| TopBarItemBase::weight | public | function | Returns the plugin weight. | Overrides TopBarItemPluginInterface::weight |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.