Questions tagged [event-subscribers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
16
votes
3answers
11k views

Redirect Anonymous user to login page

I want if user not logged in and visit any page of my website except my REST VIEWS routers I redirect him/her to login page in drupal 8, I found this solutions for drupal 7 but didn't find any ...
16
votes
2answers
7k views

KernelEvents::REQUEST is not fired on cached pages

I am trying to implement a KernelEvents::REQUEST event subscriber to perform some action on page load. I need this event to fire regardless of whether the requested page exists in the Drupal cache - ...
9
votes
3answers
9k views

Does Drupal 8 entities have any events I can listen to?

I know we can use hook_entity_update() hook to do something when an entity is updated. But is there an event to listen for node updates/deletes, or other entities such for that matter?
8
votes
3answers
2k views

Custom 500 Error page: Is there a way to theme a fatal error?

In /core/includes/errors.inc, when there is a fatal error it outputs a response with the message "The website encountered an unexpected error. Please try again later." and a back-trace, if error_level ...
5
votes
2answers
1k views

Can't find cookie for validation in EventSubscriber

There's this module Splash Redirect in which src/EventSubscriber/SplashRedirectEventSubscriber.php is supposed to check if the cookie $config_cookie exists or not, if it already exists then the user ...
5
votes
1answer
3k views

EventSubscriber endless redirect

I have Drupal 8.5.1 and I am trying to force a redirect for anonymous users to the CAS login page. First I thought that the issue was related with the firing of the checkAuthStatus function below, ...
5
votes
1answer
1k views

Why is it better to use event subscribers rather than hooks

In drupal 8, we now have the ability to use event subscribers. What's the reason for this? Is it to work with the new cache API better? Recently I had a task of needing to extract data from a node ...
5
votes
2answers
1k views

Event Response not firing anonymous user

I have a module in drupal8 which only job is on an event response is to redirect a URL. It works fine for everyone except anonymous users. How do I give the module privileges to fire on every user no ...
4
votes
3answers
2k views

How to issue 404 from a KernelEvents::REQUEST event

I'm trying to issue a 404 from a KernelEvents::REQUEST event, but it's resulting in the following error: Fatal error: Maximum function nesting level of '256' reached, aborting! in /var/www/vendor/...
4
votes
1answer
443 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 ...
3
votes
1answer
502 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 ...
3
votes
1answer
1k views

Run KernelEvents::REQUEST event before hook_node_access?

I'm doing some central static variable setup for the request and was hoping to use KernelEvents::REQUEST to run first and set the variables. However, I've found that while the documentation states: ...
3
votes
1answer
599 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?
3
votes
1answer
73 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 ...
2
votes
2answers
170 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 ...
2
votes
3answers
258 views

How are hooks implemented?

Are hooks a thing in their own right, or are they actually implemented as events under the hood?
2
votes
1answer
573 views

Create an EventSubscriber for a specific module

I'm attempting to make something occur at a specific event in the GatherContent module found here https://www.drupal.org/project/gathercontent Based on the information here http://help.gathercontent....
2
votes
2answers
1k views

How to discover events for event dispatcher

I want to write an implementation of EventSubscriberInterface for an event in commerce, there is a handy example, but How do I find out what events are available (as a whole for my site) and/or How ...
2
votes
1answer
1k views

How do I redirect in a class sub-classing the EventSubscriber class?

I want to redirect to a specific URL in an EventSubscriber class instead of showing the Page not found page. I followed Symfony2 redirect for event listener? but the redirect goes to the home. This ...
2
votes
1answer
74 views

How to suppress default HtmlException?

I need to turn off 404 & related Exceptions. Need to handle it in custom way. I've implemented an EventSubscriber with listener for kernel.exception event. But $event->setException() cannot ...
2
votes
1answer
269 views

How do I unpromoted existing node when new node is promoted to front page (Programatically)?

In Drupal 8 I want only one node at a time to be promoted to front page. If user promoted new node the existed should un-promoted automatically. Can we implement this with events in Drupal 8?
2
votes
1answer
1k views

Event does not trigger each time I load a page

I'm using event dispatcher in a custom module to auto login users in a Drupal 8 website. I'm using the KernelEvents::REQUEST, with a onRequest fonction. I put an echo for testing that the function was ...
2
votes
1answer
300 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 ...
2
votes
1answer
66 views

Hook or event before a page gets cached

Is there a hook or event I can tie into before a page gets cached. I need a hook/event that I can call so that I can run some logic on the page that is about to be cached to tell Drupal if I want a ...
2
votes
1answer
948 views

Event subscription for EntityTypeEvents

So I'm trying to get an event subscriber working for EntityTypeEvents and I can't figure out why it's not firing. This is a general outline of the code: namespace Drupal\subscriber_class_module; ...
2
votes
3answers
7k views

How do I fix a ReflectionException error (class doesn't exist)?

I'm new to drupal 8. I would like to extend the user login form by deleting the username field and adding the email field. So I created a custom module, I implemented an Event Subscriber and declared ...
2
votes
1answer
64 views

What replaced the ContentFormControllerSubscriber class?

In the past, there was a ContentFormControllerSubscriber class in Drupal, but there is no track of it anymore; the change records don't report anything about that class too. The reason I am looking ...
2
votes
1answer
1k views

EventSubscriber called on specific node type or path

I'm trying to set an event that will launch function that loads some data from outside server and print it to javascript div element. I tried many ways but I can't figure out how to make this event be ...
2
votes
3answers
4k views

How do I make a custom module trigger a function when content is unpublished?

In short: Utilizing Drupal 8, I'm hoping to to "hook" into an entity update event for a specific content type so that I can then take further action when that event occurs. In this case, delete the ...
1
vote
2answers
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/...
1
vote
1answer
63 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($...
1
vote
1answer
330 views

Handling access denied result

In Drupal 8, what is the correct way of returning access denied to a user (outside of access requirement on a controller)? I'd say it is to throw an AccessDeniedHttpException most of the time, but I ...
1
vote
1answer
62 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 ...
1
vote
1answer
62 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 ...
1
vote
1answer
27 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 = $...
1
vote
1answer
141 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::$...
1
vote
1answer
148 views

How to change a field value in a product variation when someone edited the order manually?

I had implemented a "Event Subscriber" controller to change the stock value in the product variation. Stock is a field in product variation. Depending upon the quantity in order items I am ...
1
vote
1answer
438 views

Using Event Subscriber change the product custom field value after first checkout of that product

Using 'Event Subscriber', I'm able to get $order and $items from the order after completing the checkout. How can I get the product id from the $itemsand modify the product custom field value? ...
1
vote
1answer
953 views

What to attach event listener to in Drupal behavior

I have a use case where I need to listen to the insertIntoActiveEditor event in the insertIntoActiveEditor event in the Insert module, but with this code: (function ($) { /** * Custom behavior to ...
1
vote
0answers
58 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\...
1
vote
1answer
62 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 ...
1
vote
0answers
19 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 ...
1
vote
1answer
771 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 ...
1
vote
0answers
272 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 ...
1
vote
0answers
702 views

Throwing an AccessDeniedHttpException() in an event causes a "max nesting level" error

I am debugging an event subscriber that checks if a user has access based on certain conditions. If they don't have access, then I am doing: throw new AccessDeniedHttpException(); However, this was ...
1
vote
0answers
1k views

How to understand and fix a Reflection does not exist error?

I am trying to get my head around using EventSubscriber for adding HTTP Headers. In D7 I would have simply used drupal_add_http_header('Content-Type', 'application/javascript'); But I see that this ...
1
vote
1answer
123 views

How can an event subscriber have access to the entity type?

I'm using entity_print for PDF generation on two node types. I'd like each to have its own orientation configuration (or just have one override the default). There is an event in entity_print where ...
1
vote
0answers
239 views

How to fire an Event on node publish on Drupal 8

I am trying to figure out how to fire an event when a node type gets published so I can set up a listener to listen to that event. Can someone explain what is the best practice and how to fire an ...
1
vote
0answers
333 views

How to implement redirect in kernel.request event properly?

I can reach site by multiple aliases. Business logic requires to do some redirects in case of accessing be an alias. E.g: alias1.dev -> site.dev/landing-page. I've implemented EventSubscriber for ...
1
vote
0answers
145 views

url redirections not working for anonymous users - redirecting to homepage

I have set a redirect rule in EventSubscriber. So according to rule, when I hit [example.com/my-page][1] in browser, it should redirect to [example.com/success][1] or some external url also. But ...