/** * {@inheritdoc} */ public function getCacheableMetadata() { // Since this depends on State this can change at any time and is not // cacheable. $metadata = new CacheableMetadata(); $metadata->setCacheMaxAge(0); return $metadata; }
/** * {@inheritdoc} */ public function getCacheableMetadata() { // The book active trail depends on the node and data attached to it. // That information is however not stored as part of the node. $cacheable_metadata = new CacheableMetadata(); if ($node = $this->requestStack->getCurrentRequest()->get('node')) { // If the node is part of a book then we can use the cache tag for that // book. If not, then it can't be optimized away. if (!empty($node->book['bid'])) { $cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]); } else { $cacheable_metadata->setCacheMaxAge(0); } } return $cacheable_metadata; }
/** * {@inheritdoc} */ public function processOutbound($route_name, Route $route, array &$parameters, CacheableMetadata $cacheable_metadata = NULL) { if ($route->hasRequirement('_csrf_token')) { $path = ltrim($route->getPath(), '/'); // Replace the path parameters with values from the parameters array. foreach ($parameters as $param => $value) { $path = str_replace("{{$param}}", $value, $path); } // Adding this to the parameters means it will get merged into the query // string when the route is compiled. $parameters['token'] = $this->csrfToken->get($path); if ($cacheable_metadata) { // Tokens are per user and per session, so not cacheable. // @todo Improve in https://www.drupal.org/node/2351015. $cacheable_metadata->setCacheMaxAge(0); } } }
/** * Provides test data for createFromObject(). * * @return array */ public function providerTestCreateFromObject() { $data = []; $empty_metadata = new CacheableMetadata(); $nonempty_metadata = new CacheableMetadata(); $nonempty_metadata->setCacheContexts(['qux'])->setCacheTags(['foo:bar'])->setCacheMaxAge(600); $uncacheable_metadata = new CacheableMetadata(); $uncacheable_metadata->setCacheMaxAge(0); $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT); $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600); $uncacheable_object = new \stdClass(); $data[] = [$empty_cacheable_object, $empty_metadata]; $data[] = [$nonempty_cacheable_object, $nonempty_metadata]; $data[] = [$uncacheable_object, $uncacheable_metadata]; return $data; }
/** * {@inheritdoc} */ public function getActionsForRoute($route_appears) { if (!isset($this->instances[$route_appears])) { $route_names = array(); $this->instances[$route_appears] = array(); // @todo - optimize this lookup by compiling or caching. foreach ($this->getDefinitions() as $plugin_id => $action_info) { if (in_array($route_appears, $action_info['appears_on'])) { $plugin = $this->createInstance($plugin_id); $route_names[] = $plugin->getRouteName(); $this->instances[$route_appears][$plugin_id] = $plugin; } } // Pre-fetch all the action route objects. This reduces the number of SQL // queries that would otherwise be triggered by the access manager. if (!empty($route_names)) { $this->routeProvider->getRoutesByNames($route_names); } } $links = array(); /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */ foreach ($this->instances[$route_appears] as $plugin_id => $plugin) { $cacheability = new CacheableMetadata(); $route_name = $plugin->getRouteName(); $route_parameters = $plugin->getRouteParameters($this->routeMatch); $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE); $links[$plugin_id] = array('#theme' => 'menu_local_action', '#link' => array('title' => $this->getTitle($plugin), 'url' => Url::fromRoute($route_name, $route_parameters), 'localized_options' => $plugin->getOptions($this->routeMatch)), '#access' => $access, '#weight' => $plugin->getWeight()); $cacheability->addCacheableDependency($access); // For backward compatibility in 8.0.x, plugins that do not implement // the \Drupal\Core\Cache\CacheableDependencyInterface are assumed // to be cacheable forever. if ($plugin instanceof CacheableDependencyInterface) { $cacheability->addCacheableDependency($plugin); } else { $cacheability->setCacheMaxAge(Cache::PERMANENT); } $cacheability->applyTo($links[$plugin_id]); } $links['#cache']['contexts'][] = 'route'; return $links; }
/** * {@inheritdoc} */ public function getCacheableMetadata($operation = NULL) { $cacheable_metadata = new CacheableMetadata(); if (!\Drupal::moduleHandler()->getImplementations('node_grants')) { return $cacheable_metadata; } // The node grants may change if the user is updated. (The max-age is set to // zero below, but sites may override this cache context, and change it to a // non-zero value. In such cases, this cache tag is needed for correctness.) $cacheable_metadata->setCacheTags(['user:' . $this->user->id()]); // If the site is using node grants, this cache context can not be // optimized. return $cacheable_metadata->setCacheMaxAge(0); }
/** * {@inheritdoc} */ public function getCacheableMetadata() { $cacheable_metadata = new CacheableMetadata(); return $cacheable_metadata->setCacheMaxAge(0); }
/** * Test domain language negotiation and outbound path processing. * * @dataProvider providerTestDomain */ public function testDomain($http_host, $domains, $expected_langcode) { $this->languageManager->expects($this->any())->method('getCurrentLanguage')->will($this->returnValue($this->languages['en'])); $config = $this->getConfigFactoryStub(['language.negotiation' => ['url' => ['source' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domains' => $domains]]]); $request = Request::create('', 'GET', array(), array(), array(), array('HTTP_HOST' => $http_host)); $method = new LanguageNegotiationUrl(); $method->setLanguageManager($this->languageManager); $method->setConfig($config); $method->setCurrentUser($this->user); $this->assertEquals($expected_langcode, $method->getLangcode($request)); $cacheability = new CacheableMetadata(); $options = []; $this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability)); $expected_cacheability = new CacheableMetadata(); if ($expected_langcode !== FALSE && count($domains) > 1) { $expected_cacheability->setCacheMaxAge(Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']); } $this->assertEquals($expected_cacheability, $cacheability); }
/** * {@inheritdoc} */ public function getCacheableMetadata() { $meta_data = new CacheableMetadata(); $meta_data->setCacheMaxAge(0); return $meta_data; }
/** * {@inheritdoc} */ public function getCacheableMetadata($operation = NULL) { $cacheable_metadata = new CacheableMetadata(); if (!\Drupal::moduleHandler()->getImplementations('node_grants')) { return $cacheable_metadata; } // If the site is using node grants, this cache context can not be // optimized. return $cacheable_metadata->setCacheMaxAge(0); }