Questions tagged [event-subscribers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
53 views

Is "event_subscriber" a reserved naming convention for services? Are there guidelines to follow?

So I was working on a project today, and an event subscriber we had written the better part of a year ago was determined to no longer be firing. Here is the definition: services: my_module....
user avatar
3 votes
1 answer
178 views

Custom 404 handling for images

I'm looking for a way to handle 404's on images in Drupal. I have created several subscribers, but none seem to get triggered. My first try was to create subscriber that extends RouteSubscriberBase. ...
user avatar
  • 185
2 votes
1 answer
242 views

In event subscriber, how to redirect if value doesnt exist?

I am trying to check a condition before page load, if value exists to load page if not to redirect. I am following this page I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/...
user avatar
  • 133
0 votes
0 answers
80 views

Alter route path within event_subscriber with GET parameters

I know I can alter route path with event_subscriber class AdminRouteModifier extends RouteSubscriberBase { protected function alterRoutes(RouteCollection $collection) { if ($route = $collection-...
user avatar
  • 431
0 votes
1 answer
123 views

EventSubscriber to add cache context for specific path

I am working with Mobile Detect module and from what I understand, we need to manually add the cache context mobile_detect_is_mobile in order to have the page cached differently based on the user ...
user avatar
  • 103
3 votes
1 answer
125 views

Is there an event related to the creation of a session cookie for anonymous users?

I need to set an additional cookie whenever an anonymous user received a session cookie. This happens when selecting a facet in Search API for example. The specific use case is that I need an extra ...
user avatar
0 votes
0 answers
65 views

How to use methods from entity class if it doesn't have such service?

I'm writing an EventSubscriber for the config entity of the domain module. My goal is to insert some data in its config on preSave event (it becomes from the patch for D9). Here is my code from ...
user avatar
1 vote
1 answer
74 views

How to alter routes defined in route_callbacks?

Usually I alter static routes in routes subscriber: protected function alterRoutes(RouteCollection $collection) { $route = $collection->get('gutenberg.media.load_media'); if ($route) { ...
user avatar
0 votes
0 answers
98 views

Alter a controller's render array via KernelEvents::VIEW

I'm trying to alter a contrib module controller's output via KernelEvents::VIEW. I want to display the output from the controller and append the markup returned from the event handler. This is the ...
user avatar
0 votes
1 answer
68 views

Feeds event subscriber stopped working

For some reason my feeds event subscribers have just stopped working. My code looks like this, the dpm for debugging never gets fired so I think the function isn't being run. I can't work out what's ...
user avatar
1 vote
1 answer
233 views

EventSubscriber not trigger

I have a EventSubscriber which I want to trigger before the entity is saved (to alter the title). But I can't figure why it doesn't work. I got back the projet, do the migration from D8 to D9. Is it ...
user avatar
0 votes
1 answer
50 views

How can I invalidate cached field access grants for anonymous users?

We have a Drupal 8 application that uses an event subscriber class to validate IP addresses against a table of white-listed addresses on kernel requests, and adds that value to a session variable. ...
user avatar
  • 349
-2 votes
1 answer
168 views

Why did my event subscriber suddenly stop working and how can I debug it? [closed]

I've been working on a module in which I'm subscribing to commerce_order.place.post_transition event. However, it suddenly stopped working. Any ideas how I can debug it? I tried Xdebug but it won't ...
user avatar
  • 537
0 votes
2 answers
75 views

hook_init() equivalent for console command

I have a module which subscribes to the kernel.request event to set some environment variables for use with LDAP. E.g. setting the LDAP_CACERT environment variable. I'm using it to replace hook_init() ...
user avatar
  • 121
0 votes
1 answer
109 views

Hide path on frontpage redirect

In a custom module i'm using an EventSubscriber to redirect different people to different frontpages. Below is the simplified code I'm using. //GET PATH $nid = 1; $domain_url = \Drupal::service('...
user avatar
  • 892
1 vote
1 answer
61 views

Intercepting when a page is requested

I am trying to call a function every time a page is loaded. With Drupal 7, I used hook_init, now I'm using a subscribed event. public function logRegEntry(GetResponseEvent $event) { $request = $...
user avatar
  • 399
0 votes
2 answers
51 views

How to remove a role after a user adds a payment method in Commerce?

I am trying to set up a subscription-based site in Drupal 8/Commerce 2. I'm using Commerce Stripe to process payments. I want to remove a role from a user ("needs to add new card" role) ...
user avatar
1 vote
1 answer
100 views

EventSubscriber returning correct content but status code is 404

As the title says, content is being returned correctly but the status code is 404. I have checked the .htaccess file and the Apache config file as well. All seems to be fine. I have the following in ...
user avatar
0 votes
1 answer
800 views

Send email to Commerce customer after order status is changed to Completed

I am attempting to develop a module that will send an email to a customer in my Commerce 2 store when that customer's order's status is updated to "Completed". I am basing my module off of ...
user avatar
  • 1,234
1 vote
1 answer
69 views

What is the "contract" for EventSubscriberInterface functions?

The EventSubscriberInterface can be used to listen for events, and getSubscribedEvents is used to announce which events an implementation would like to subscribe to. The getSubscribedEvents must ...
user avatar
  • 1,149
0 votes
1 answer
36 views

On entity save, identify the user fields that have been changed

I have a need to track changes to users in a separate database, apart from Drupal. When a user changes their email address or their password, I need to communicate that change with a shadow table in ...
user avatar
0 votes
1 answer
583 views

how to add an event subscriber when the user reaches the login form

I am new on Drupal 8 , I have implemented a hook 'hook_form_alter' in my module to add a new field to the login form , but now I want to handle it with the event , I want to trigger an event when the ...
user avatar
1 vote
1 answer
101 views

My site only listens to the event after I clear all caches

I have a module with an event subscribe that I use to redirect to an age verification page if a cookie is not set, and as long as the current request is not the age verification uri. if(!isset($...
user avatar
0 votes
0 answers
97 views

Does drush stop events from firing and/or bypass event subscribers?

I'm running some migrations and have logic dependent on their Events public static function getSubscribedEvents() { $events[MigrateEvents::POST_IMPORT][] = ['onMigratePostImport']; return $...
user avatar
  • 1,781
2 votes
2 answers
348 views

Which event should we subscribe to if we want code to be triggered when a new user registers?

I noticed that account.set is the event invoked for a user account. I want to create an event subscriber which adds a callback invoked when a new user registers on the site. I am not sure whether ...
user avatar
  • 804
4 votes
1 answer
790 views

How to implement StackMiddleware for a redirection correctly?

I'm working on a redirection (for unauthenticated users) for my Drupal 8 site. Currently, the redirection works however, the redirection persists even after user is authenticated. I found this article ...
user avatar
1 vote
0 answers
60 views

How to make EventSubscriber run on every page load? [duplicate]

Tripped up on how to make my EventSubscriber code run on every page load. In my src/EventSubscriber/RedirectAnonymousSubscriber.php file here's my code: <?php namespace Drupal\mymodule\...
user avatar
-1 votes
2 answers
440 views

Redirecting Anonymous Users to external URL on Drupal 8 site

I am attempting to create a custom module that redirects anonymous/unauthenticated users away from the site. This site is only supposed to be used by admins, so in order to access it, they must type /...
user avatar
0 votes
0 answers
66 views

Change system.404 path dynamically based on context

I have a site that makes use of the groups module to denote various locations all with their own content. I'd like to give the ability to programmatically alter the system.404 path depending on the ...
user avatar
1 vote
1 answer
85 views

adding new validation method using HookEventDispatcherInterface::FORM_ALTER event

I'm trying to add a new validation method for 'user_login_form' using HookEventDispatcher module and events: class MyFormEventsSubscriber implements EventSubscriberInterface { public static function ...
user avatar
0 votes
1 answer
70 views

Don't cache custom Front Page domain

I'm working on a module that shows a different frontpage per role. I have three frontpages: the base www.domain.nl for anonymous users, set in the basic site settings for users with role_1 they go to ...
user avatar
  • 892
0 votes
1 answer
203 views

Looking for a way to trigger an event for testing custom event subscriber

I am looking for ways to trigger an event in my local dev to check out a custom event subscriber. The actual event is triggered by Google social auth and I don't think it is possible to do that ...
user avatar
  • 804
0 votes
0 answers
145 views

Bypassing page cache for Drupal events

I need some session verification code to be run on each request to Drupal. The most obvious solution that I have found was to use the EventSubscriber interface, and the KernelEvents::REQUEST event. ...
user avatar
  • 400
1 vote
0 answers
20 views

Limit entity type/bundle for EventSubscriber inside services.yml?

I've got an EventSubscriber that I'd like to run for only a specific content type. Is there a way to do this from the services.yml file as an argument or something, or do I just need to check for the ...
user avatar
  • 1,781
1 vote
1 answer
1k views

Run code once on each page load

In the past there were some hooks, such as described here: How to run check on each page load? to execute arbitrary code on each page load. Now this has been replaced with an event based approach. I ...
user avatar
  • 400
3 votes
1 answer
860 views

Is it possible to override/replace an event subscriber?

I would like to replace an event subscriber. Can I write a class that implements EventSubscriberInterface which replaces another event subscriber?
user avatar
  • 161
0 votes
1 answer
664 views

Event subscriber to invalidate a cache

I am invalidating a cache tag in KernelEvents::REQUEST event: public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['onRequest', 100]; return $events; } public ...
user avatar
  • 3,461
3 votes
1 answer
588 views

How can trusted host patterns be set dynamically?

I'm working on a site which uses the Groups module that will allow users to create groups on the site. The site will then allow for users to point custom domain names at the site, with the content for ...
user avatar
  • 14.7k
0 votes
1 answer
50 views

Forward authenticated user to user.page on /login uri

If an authenticated user navigates to /login on a Drupal 8 site, she gets the "access denied" error. I want the site to redirect that user to /user (route user.page) instead. Unfortunately my ...
user avatar
  • 2,363
0 votes
0 answers
468 views

How can I trigger the EntityTypeEvents::UPDATE event?

I tried to trigger event on each entity update. This is my src/EventSubscriber/MySub.php use Drupal\Core\Entity\EntityTypeEvents; use Symfony\Component\EventDispatcher\...
user avatar
  • 25
0 votes
1 answer
84 views

Alter existing route's output formats?

I want to alter the entity.node.canonical route so that when it is accessed using ?_format=json it will return the rendered node in JSON. So far I have created a RouteSubscriber that alters the route ...
user avatar
0 votes
0 answers
50 views

How do I redirect a user basing on its roles?

I'm having an issue with writing a redirect based on whether or not the current user's role matches the one listed or not I've been trying to find solutions related to getting the current path for ...
user avatar
-1 votes
1 answer
586 views

Error with Event Subscriber using getRouteName()

I have a problem with event subscriber. Here is the error: The website encountered an unexpected error. Please try again later. Error: Call to a member function getRouteName() on null in Drupal\...
user avatar
  • 1
1 vote
0 answers
361 views

Return an Ajax Response from EventSubscriber?

I am trying to return an ajax response when a user logs in while the page is loading post-login. /#1 approach was attaching an ajax callback to the user login form submit with form_alter. However ...
user avatar
1 vote
1 answer
1k views

How to alter response headers from module hook

I am puzzled how to alter the response headers from specific hook (hook_file_download)? /** * Implements hook_file_download(). */ function example_file_download($uri) { $scheme = FileSystem::...
user avatar
  • 159
1 vote
2 answers
1k views

View all subscribed events?

I have a feature where if a user navigates to a specific url (www.mysite.com/special-content) they are redirected to the login page, where we've appended a destination to the request (www.mysite.com/...
user avatar
  • 3,829
1 vote
1 answer
193 views

setContainer Error When Registering an EventSubscriber Service

I am trying to create a basic event subscriber, but when I add the services.yml file and attempt to rebuild the cache with drush cr, I get this error. In Drupal.php line 130: \Drupal::$...
user avatar
2 votes
1 answer
360 views

Would replacing hooks with events be a good practice?

Do you think it would be a good practice to replace hooks with events? I mean dispatching an event inside a hook to subscribe afterwards. For example if you have 20 hooks in your module, is it ...
user avatar
0 votes
4 answers
587 views

How can you remove another modules events?

So, I have an event subscriber for Drupal commerce that listens for "commerce_order.place.post_transition" and will send a bespoke order email (due to not being able to change some of the things like ...
user avatar
  • 3
0 votes
0 answers
173 views

Why are KernelEvents not triggered on cached pages? [duplicate]

Why Kernal events not triggering on cached pages.I have tried using priorities.. $events[KernelEvents::REQUEST][] = ['onRequest', 27]; Services.yml file tags: - { name: 'event_subscriber', ...
user avatar
  • 1