Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [event-subscribers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
19 views

How to create a view with filters using data from an api?

We have a multisite setup and need to share taxonomy data from one site to all of the other sites. I have created a REST Export view on the site that has the taxonomy data. The next thing I've done is ...
Jordan's user avatar
  • 49
0 votes
2 answers
87 views

Why isn't my eventsubscriber firing when a user logs out?

I working on the advanced_pwa module. My use case is that I want to tell the client side service worker to flush it's cache when a user logs out. I concluded that creating a event subscriber would be ...
gMaximus's user avatar
0 votes
0 answers
37 views

Custom event only fired single eventsubscriber, if in a AdvancedQueueJob, but all if fired from module

I have a custom event in my commerce 2 shop. filename: custom_module/src/Event/OrderShippedEvent.php namespace Drupal\custom_module\Event; class OrderShippedEvent extends EventBase { public const ...
HasseWilson's user avatar
0 votes
0 answers
19 views

Flag event is invoked twice on double submission of flag confirmation form

The method invoked during the flagging event is implemented in Symfony\Component\EventDispatcher\EventSubscriberInterface. class JIEventSubscriber implements EventSubscriberInterface{ protected $...
peebs124's user avatar
0 votes
1 answer
99 views

How to dispatch a ResponseEvent so that a cookie is added after login?

I want to add a session cookie when a user logs in. The previous implementation I had, was setting the cookie in hook_user_login with this snippet: $response = new Response(); $response->headers-&...
Maria Ioannidou's user avatar
-2 votes
1 answer
88 views

Constantly creating new Anonymous users

I find that my instance of drupal is always "creating" new anonymous users. I have an even on Create New User that simply logs the name to a file, and it is filled with entries like 2023-10-...
dmgig's user avatar
  • 129
0 votes
1 answer
136 views

How to return failure with an event subscriber?

I have an event subscriber that deducts a credit when someone makes a booking. If there are no credits left, how do I return a failure and stop the booking being made? The subscriber is acting on ...
Interlated's user avatar
  • 1,423
3 votes
1 answer
124 views

Check for active/qualified session in EventSubscriber, destroy session and cookies if not

I have an application that has a custom SSO with a third party service. We are porting Drupal 7 code to Drupal 10 to continue the functionality. Previously we had a hook_init function that did the ...
Kevin's user avatar
  • 27.9k
1 vote
0 answers
51 views

How can show a message for products that are not in stock?

I am implementing custom stock management in Commerce. I followed the following steps. I have added a stock integer field in product variant. I have written a event subscriber which reduce this ...
Kuntal Das's user avatar
0 votes
1 answer
398 views

How do I port this event subscriber?

The following code works fine with Drupal 9. namespace Drupal\vl_login\EventSubscriber; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; use ...
user avatar
0 votes
1 answer
121 views

Use specific theme [not template file] for 404 and 403 generated for specific path

I have 2 themes, both enabled, assume they are called my_first_theme (default) and my_second_theme. I have /whatever path and lots of the pages under this path, and I have a theme negotiator, which ...
Alex Smirnoff's user avatar
-2 votes
1 answer
163 views

Set variable for drupal 9 website [closed]

I need to get the hostname and then define a variable which is going to be used in all controllers of the website. I am able to call the code in controller as follows: $host = \Drupal::request()->...
Saili Jaguste's user avatar
0 votes
1 answer
257 views

Multiple Custom Module Event Subscribers Override Each Other

I've been working on custom modules that alter RequestEvents based on the entity type provided by the route. The functionality from one entity to another doesn't change, but the data does, hence I ...
FrankieD's user avatar
  • 103
1 vote
1 answer
394 views

Is it possible to send an argument to a title callback?

I am trying to figure out how to set page titles on various pages. I have seen a lot of posts suggesting preprocess functions. These are not correctly setting the route's page title but only setting ...
liquidcms's user avatar
  • 1,278
0 votes
2 answers
397 views

How do I get the product item author id from the order object?

I have a website, where users can add their products. After a user purchases a product and pays for it, I want to create some custom logic. I need to know the authors of the individual items purchased....
ventura's user avatar
  • 103
1 vote
1 answer
202 views

How can I return a templated response from an Exception Event subscriber?

I'm currently using an Event Subscriber to catch exceptions and return a plain-text message response to the user (instead of the default white screen of death). Is there a way to return a templated ...
user1414202's user avatar
0 votes
1 answer
523 views

How to get route inside RequestEvent

I need to redirect all existing links targeting a few hand-picked nodes to an external domain. I created an EventSubscriber listening to RequestEvents my.services.yml services: my.request_subscriber:...
Hudri's user avatar
  • 5,530
3 votes
1 answer
601 views

How to add additional drupalSettings to all pages with an event subscriber

I need to set the user timezone on all pages in the JS drupalSettings object, so that it can be accessed on any page. As drupalSettings.user is provided by core, I want to set drupalSettings.user....
Jaypan's user avatar
  • 17.6k
0 votes
1 answer
895 views

How can I catch a login event in a subscriber?

I've got an event that is triggered on hook_user_login in a module. $event_dispatcher = \Drupal::service('event_dispatcher'); $event_dispatcher->dispatch('mymodule_user_login', $event); I also ...
dibs's user avatar
  • 818
-1 votes
1 answer
527 views

Class not found for service in custom module [closed]

I am trying to create an event subscriber service in a custom module and I keep getting a "Class not found" error. The module directory is modules/custom/avgc The class is in src/...
F Grassi's user avatar
1 vote
0 answers
98 views

phpword in an EventSubscriber

I want to download a Drupal page to a Word document in an Event Subscriber. The code I'm starting with will download a document which msword will open. Here's the original code: protected function ...
esod's user avatar
  • 319
1 vote
2 answers
87 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....
Ryan Hartman's user avatar
3 votes
1 answer
467 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. ...
Frank's user avatar
  • 185
5 votes
1 answer
3k 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/...
May 's user avatar
  • 163
0 votes
0 answers
413 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-...
Dimetry's user avatar
  • 440
0 votes
1 answer
527 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 ...
Frank6's user avatar
  • 103
3 votes
1 answer
597 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 ...
Sean Robertson's user avatar
0 votes
0 answers
84 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 ...
Yaroslav's user avatar
1 vote
1 answer
268 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) { ...
Sergey Kravchenko's user avatar
0 votes
0 answers
432 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 ...
Nicholas's user avatar
0 votes
1 answer
260 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 ...
Daniel Harper's user avatar
1 vote
1 answer
942 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
104 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. ...
Steve's user avatar
  • 351
-2 votes
1 answer
601 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 ...
Kartagis's user avatar
  • 612
0 votes
2 answers
195 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() ...
Dylan's user avatar
  • 121
0 votes
1 answer
224 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('...
Joost's user avatar
  • 1,348
1 vote
1 answer
291 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 = $...
pglatz's user avatar
  • 469
0 votes
2 answers
69 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) ...
Patrick Kenny's user avatar
1 vote
1 answer
244 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 ...
hirani89's user avatar
0 votes
1 answer
1k 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 ...
hockey2112's user avatar
  • 1,244
1 vote
1 answer
88 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 ...
sbrattla's user avatar
  • 1,149
1 vote
1 answer
380 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 ...
jscrilla's user avatar
1 vote
1 answer
1k 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 ...
David montera's user avatar
1 vote
1 answer
242 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($...
Sidney Sousa's user avatar
0 votes
0 answers
121 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 $...
Mrweiner's user avatar
  • 1,921
2 votes
2 answers
788 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 ...
Binny's user avatar
  • 902
4 votes
1 answer
1k 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 ...
Joe Spinelli's user avatar
1 vote
0 answers
66 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\...
Joe Spinelli's user avatar
0 votes
2 answers
614 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 /...
Joe Spinelli's user avatar
0 votes
0 answers
149 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 ...
user7355700's user avatar