ContentPreprocess.php 1.83 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\content_moderation;

5
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 7
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9 10

/**
11
 * Determines whether a route is the "Latest version" tab of a node.
12 13
 *
 * @internal
14
 */
15
class ContentPreprocess implements ContainerInjectionInterface {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

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

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

34 35 36 37 38 39 40 41 42
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_route_match')
    );
  }

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  /**
   * Wrapper for hook_preprocess_HOOK().
   *
   * @param array $variables
   *   Theme variables to preprocess.
   */
  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();
  }

}