77fcd786101af8491958c4c59e7a8bc22ecf8c18
[yaffs-website] / web / core / modules / content_moderation / src / ContentPreprocess.php
1 <?php
2
3 namespace Drupal\content_moderation;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Routing\RouteMatchInterface;
7 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 class ContentPreprocess implements ContainerInjectionInterface {
14
15   /**
16    * The route match service.
17    *
18    * @var \Drupal\Core\Routing\RouteMatchInterface $routeMatch
19    */
20   protected $routeMatch;
21
22   /**
23    * Constructor.
24    *
25    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
26    *   Current route match service.
27    */
28   public function __construct(RouteMatchInterface $route_match) {
29     $this->routeMatch = $route_match;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('current_route_match')
38     );
39   }
40
41   /**
42    * Wrapper for hook_preprocess_HOOK().
43    *
44    * @param array $variables
45    *   Theme variables to preprocess.
46    */
47   public function preprocessNode(array &$variables) {
48     // Set the 'page' template variable when the node is being displayed on the
49     // "Latest version" tab provided by content_moderation.
50     $variables['page'] = $variables['page'] || $this->isLatestVersionPage($variables['node']);
51   }
52
53   /**
54    * Checks whether a route is the "Latest version" tab of a node.
55    *
56    * @param \Drupal\node\Entity\Node $node
57    *   A node.
58    *
59    * @return bool
60    *   True if the current route is the latest version tab of the given node.
61    */
62   public function isLatestVersionPage(Node $node) {
63     return $this->routeMatch->getRouteName() == 'entity.node.latest_version'
64            && ($pageNode = $this->routeMatch->getParameter('node'))
65            && $pageNode->id() == $node->id();
66   }
67
68 }