/** * {@inheritdoc} */ protected function buildItems(array $tree, CacheableMetadata &$tree_access_cacheability, CacheableMetadata &$tree_link_cacheability) { $items = parent::buildItems($tree, $tree_access_cacheability, $tree_link_cacheability); foreach ($items as $key => $item) { // Only operate if there is a menu item that points // to the route that should get the active-trail class. if ($item['url']->getRouteName() == 'view.blog.page_1') { // Add cacheability metadata. $tree_link_cacheability->addCacheContexts(['url']); // Set the in_active_trail key to TRUE if a blog node is displayed. $node = $this->routeMatch->getParameter('node'); if ($node instanceof NodeInterface && $node->bundle() == 'blog') { $items[$key]['in_active_trail'] = TRUE; } } } return $items; }
/** * Implements \Drupal\Core\Entity\Entity::getCacheTags(). */ public function getCacheTags() { if ($node = $this->routeMatch->getParameter('node')) { $cache_tags = Cache::mergeTags($node->getCacheTags(), ['node_list']); return Cache::mergeTags(parent::getCacheTags(), $cache_tags); } else { return parent::getCacheTags(); } }
/** * Retrieve the migrations belonging to the appropriate group. * * @return array * An array of entity IDs. */ protected function getEntityIds() { $query = $this->getStorage()->getQuery('OR'); $keys = $this->entityType->getKeys(); $migration_group = $this->currentRouteMatch->getParameter('migration_group'); // Add groupless migrations to the default group. if ($migration_group == 'default') { $query->notExists('migration_group'); } return $query->condition('migration_group', $migration_group)->sort($keys['id'])->pager($this->limit)->execute(); }
/** * @covers ::__construct * @covers ::getRouteObject * @covers ::getCurrentRouteMatch * @covers ::getRouteMatch */ public function testGetCurrentRouteObject() { $request_stack = new RequestStack(); $request = new Request(); $request_stack->push($request); $current_route_match = new CurrentRouteMatch($request_stack); // Before routing. $this->assertNull($current_route_match->getRouteObject()); // After routing. $route = new Route('/test-route/{foo}'); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'test_route'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route); $request->attributes->set('foo', '1'); $this->assertSame('1', $current_route_match->getParameter('foo')); // Immutable for the same request once a route has been matched. $request->attributes->set('foo', '2'); $this->assertSame('1', $current_route_match->getParameter('foo')); // Subrequest. $subrequest = new Request(); $subrequest->attributes->set(RouteObjectInterface::ROUTE_NAME, 'test_subrequest_route'); $subrequest->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test-subrequest-route/{foo}')); $subrequest->attributes->set('foo', '2'); $request_stack->push($subrequest); $this->assertSame('2', $current_route_match->getParameter('foo')); // Restored original request. $request_stack->pop(); $this->assertSame('1', $current_route_match->getParameter('foo')); }
/** * Constructs a new OrderReassignForm object. * * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match * The current route match. */ public function __construct(CurrentRouteMatch $current_route_match) { $this->order = $current_route_match->getParameter('commerce_order'); }
/** * @covers ::resetRouteMatch */ public function testResetRouteMatch() { $route = new Route('/test-route/{foo}'); $request = new Request(); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'test_route'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route); $request_stack = new RequestStack(); $request_stack->push($request); $current_route_match = new CurrentRouteMatch($request_stack); $route_name = $current_route_match->getRouteName(); $this->assertSame('test_route', $route_name); // Replace the matched route on the request. $request->attributes->set(RouteObjectInterface::ROUTE_NAME, NULL); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, NULL); // Reset the route match. $current_route_match->resetRouteMatch(); $route_name = $current_route_match->getRouteName(); $this->assertNull($route_name); }
/** * @covers ::getRouteMatchFromRequest */ public function testGetRouteMatchFromRequest() { $request_stack = new RequestStack(); $request = new Request(); $request_stack->push($request); $route = new Route('/test-route/{foo}'); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'test_route'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route); $request->attributes->set('foo', '1'); $current_route_match = new CurrentRouteMatch($request_stack); $route_match = $current_route_match->getRouteMatchFromRequest($request); $this->assertEquals('test_route', $route_match->getRouteName()); $this->assertEquals($route, $route_match->getRouteObject()); }
/** * 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(); }