You are here

class ContentPreprocess in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/ContentPreprocess.php \Drupal\content_moderation\ContentPreprocess

Determines whether a route is the "Latest version" tab of a node.

@internal

Hierarchy

Expanded class hierarchy of ContentPreprocess

2 files declare their use of ContentPreprocess
ContentPreprocessTest.php in core/modules/content_moderation/tests/src/Unit/ContentPreprocessTest.php
content_moderation.module in core/modules/content_moderation/content_moderation.module
Contains content_moderation.module.

File

core/modules/content_moderation/src/ContentPreprocess.php, line 15

Namespace

Drupal\content_moderation
View source
class ContentPreprocess implements ContainerInjectionInterface {

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   Current route match service.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_route_match'));
  }

  /**
   * @param array $variables
   *   Theme variables to preprocess.
   *
   * @see hook_preprocess_HOOK()
   */
  public function preprocessNode(array &$variables) {

    // Set the 'page' template variable when the node is being displayed on the
    // "Latest version" tab provided by content_moderation.
    $variables['page'] = $variables['page'] || $this
      ->isLatestVersionPage($variables['node']);
  }

  /**
   * Checks whether a route is the "Latest version" tab of a node.
   *
   * @param \Drupal\node\Entity\Node $node
   *   A node.
   *
   * @return bool
   *   True if the current route is the latest version tab of the given node.
   */
  public function isLatestVersionPage(Node $node) {
    return $this->routeMatch
      ->getRouteName() == 'entity.node.latest_version' && ($pageNode = $this->routeMatch
      ->getParameter('node')) && $pageNode
      ->id() == $node
      ->id();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentPreprocess::$routeMatch protected property The route match service.
ContentPreprocess::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ContentPreprocess::isLatestVersionPage public function Checks whether a route is the "Latest version" tab of a node.
ContentPreprocess::preprocessNode public function
ContentPreprocess::__construct public function Constructor.