New answers tagged

3

If you want to run code before the Drupal kernel is started use a middleware. Use the drush code generator drush gen middleware and put your code in handle() of the generated middleware class: MymoduleMiddleware.php <?php namespace Drupal\mymodule; use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\HttpFoundation\Request; ...


2

In general, never send responses from custom code. See How do I set a cookie using $response->headers->setCookie() in a form submission handler? In this case you are sending a redirect response to the client while Drupal builds the real response containing the cookie to the session where the message gets stored. The real response never reaches the ...


3

The code depends on host name and IP address. So you could add the cache contexts url.site and ip: $response = new TrustedRedirectResponse('https://site.ch/'); $response->getCacheableMetadata()->addCacheContexts(['ip', 'url.site']); $event->setResponse($response); In this case, though, caching doesn't make much sense because it doesn't save many ...


Top 50 recent answers are included