annotate core/modules/content_moderation/src/ContentPreprocess.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\content_moderation;
Chris@0 4
Chris@0 5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Chris@0 6 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 7 use Drupal\node\Entity\Node;
Chris@0 8 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Determines whether a route is the "Latest version" tab of a node.
Chris@0 12 *
Chris@0 13 * @internal
Chris@0 14 */
Chris@0 15 class ContentPreprocess implements ContainerInjectionInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The route match service.
Chris@0 19 *
Chris@0 20 * @var \Drupal\Core\Routing\RouteMatchInterface
Chris@0 21 */
Chris@0 22 protected $routeMatch;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Constructor.
Chris@0 26 *
Chris@0 27 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 28 * Current route match service.
Chris@0 29 */
Chris@0 30 public function __construct(RouteMatchInterface $route_match) {
Chris@0 31 $this->routeMatch = $route_match;
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * {@inheritdoc}
Chris@0 36 */
Chris@0 37 public static function create(ContainerInterface $container) {
Chris@0 38 return new static(
Chris@0 39 $container->get('current_route_match')
Chris@0 40 );
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * @param array $variables
Chris@0 45 * Theme variables to preprocess.
Chris@0 46 *
Chris@0 47 * @see hook_preprocess_HOOK()
Chris@0 48 */
Chris@0 49 public function preprocessNode(array &$variables) {
Chris@0 50 // Set the 'page' template variable when the node is being displayed on the
Chris@0 51 // "Latest version" tab provided by content_moderation.
Chris@0 52 $variables['page'] = $variables['page'] || $this->isLatestVersionPage($variables['node']);
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Checks whether a route is the "Latest version" tab of a node.
Chris@0 57 *
Chris@0 58 * @param \Drupal\node\Entity\Node $node
Chris@0 59 * A node.
Chris@0 60 *
Chris@0 61 * @return bool
Chris@0 62 * True if the current route is the latest version tab of the given node.
Chris@0 63 */
Chris@0 64 public function isLatestVersionPage(Node $node) {
Chris@0 65 return $this->routeMatch->getRouteName() == 'entity.node.latest_version'
Chris@0 66 && ($pageNode = $this->routeMatch->getParameter('node'))
Chris@0 67 && $pageNode->id() == $node->id();
Chris@0 68 }
Chris@0 69
Chris@0 70 }