class PageContext

Provides the Page Context top bar item.

Attributes

#[TopBarItem(id: 'page_context', region: TopBarRegion::Context, label: new TranslatableMarkup('Page Context'))]

Hierarchy

Expanded class hierarchy of PageContext

File

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

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\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;
    }
    $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 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 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. 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
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.