comparison core/modules/content_moderation/src/ContentPreprocess.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 * @internal
14 */
15 class ContentPreprocess implements ContainerInjectionInterface {
16
17 /**
18 * The route match service.
19 *
20 * @var \Drupal\Core\Routing\RouteMatchInterface
21 */
22 protected $routeMatch;
23
24 /**
25 * Constructor.
26 *
27 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
28 * Current route match service.
29 */
30 public function __construct(RouteMatchInterface $route_match) {
31 $this->routeMatch = $route_match;
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public static function create(ContainerInterface $container) {
38 return new static(
39 $container->get('current_route_match')
40 );
41 }
42
43 /**
44 * @param array $variables
45 * Theme variables to preprocess.
46 *
47 * @see hook_preprocess_HOOK()
48 */
49 public function preprocessNode(array &$variables) {
50 // Set the 'page' template variable when the node is being displayed on the
51 // "Latest version" tab provided by content_moderation.
52 $variables['page'] = $variables['page'] || $this->isLatestVersionPage($variables['node']);
53 }
54
55 /**
56 * Checks whether a route is the "Latest version" tab of a node.
57 *
58 * @param \Drupal\node\Entity\Node $node
59 * A node.
60 *
61 * @return bool
62 * True if the current route is the latest version tab of the given node.
63 */
64 public function isLatestVersionPage(Node $node) {
65 return $this->routeMatch->getRouteName() == 'entity.node.latest_version'
66 && ($pageNode = $this->routeMatch->getParameter('node'))
67 && $pageNode->id() == $node->id();
68 }
69
70 }