2

I tried overwrite all links from my site in Drupal 8 in accordance with a URL parameter.

I use the service path_processor_outbound and implemented the processOutbound() method, extending the OutboundPathProcessorInterface interface.

services.yml

services:
   test_concept.test_concept_sayHelloService:
   class: Drupal\test_concept\Services\TestConceptRepeater
   tags:
      - { name: 'path_processor_inbound', priority: 100 }
      - { name: 'path_processor_outbound', priority: 300 }

Method of my class

public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  $current_uri = \Drupal::request()->getRequestUri();
  $path_args = explode('/', $current_uri);
  $sede = $path_args[1];
  $path = "/" . $sede . $path;
  return $path;
}

This work very good when reloading the site alter flushing the cache, but when I change the URL parameter and reload the site again, the URLs don't change, even if processOutbound() runs again.

Is a problem with the cache?

1 Answer 1

4

That's because those links get cached and Drupal doesn't know by what to vary those caches.

That's why you receive that additional argument, $bubbleable_metadata. You need to use it to specify that our changes depend on a certain url parameter. See https://www.drupal.org/docs/8/api/cache-api/cache-contexts and https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays.

You need something like this:

if ($bubbleable_metadata) {
  $bubbleable_metadata->addCacheContexts(['url.query_args:your_parameter']);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.