A website might have more than one login destination url based on the request parameters. So let other module alter the redirect Url.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

b.ravanbakhsh created an issue. See original summary.

b.ravanbakhsh’s picture

Title: provide hook module functionality » provide hook functionality
b.ravanbakhsh’s picture

Issue summary: View changes
b.ravanbakhsh’s picture

Status: Active » Needs review
FileSize
3.94 KB
b.ravanbakhsh’s picture

Assigned: b.ravanbakhsh » Unassigned
b.ravanbakhsh’s picture

pfrenssen’s picture

Seems a bit strange to me to introduce new hooks in Drupal 8 now that we have EventSubscribers which have lots of advantages over hooks. Hooks are probably going to go away completely in D9, so if we introduce something new then let's go with EventSubscribers immediately.

shrop’s picture

Agree with @pfrenssen. EventSubscribers are really slick and provide a supported future path for new versions of Drupal, as he mentioned. Love to hear if there is a good reason to consider adding hooks to the D8 version.

b.ravanbakhsh’s picture

A website might have more than one login destination based on how the user logs in such as using CAS, drupal login form, custom form, another custom form and ... but $config->get('redirect_authenticated_users_to') is single.
Adding the ability to customise role-based 'redirect_authenticated_users_to' URLs would be my suggestion.

shrop’s picture

Issue summary: View changes

@b.ravanbakhsh: That sounds like a pretty good use case. Thanks for the thought on that! Is that the feature that you drove you to thinking hook functionality?

If we add module hook capabilities to the 8.x release, it will be come a feature that is depended on and removal later would be a challenge, probably forcing a new 8.x branch like 8.x-2.x. With that, are there ways to accomplish this by building in a role based redirection function with EventSubcribers or maybe a submodule that is part of the Redirect 403 to User Login module?

Not just asking you, but anyone with thoughts around this.

shrop’s picture

I had a conversation with @DamienMcKenna on this subject and he had a great suggestion/compromise. What if we add @deprecated in the related docblock? We could mention that it will be removed in the Drupal 9 version and suggest using EventSubscribers instead.

I think this would give everyone notice about future plans and flexibility.

Reference: https://www.drupal.org/core/deprecation

New patch?

shrop’s picture

Status: Needs review » Needs work
pingers’s picture

Here's a re-roll against HEAD.

  • Nixou committed c1771fa on 8.x-1.x
    Issue #2700797 by nixou: provide hook functionality
    
Nixou’s picture

Status: Needs work » Fixed

As @pfrenssen and @shrop said above, adding hook is not what is expected of us.
Hooks will be remove in the future, replaced by Event Subscribers.

Thanks you all for your work around the hook, but I wrote another patch based on an Event Subscriber which we do the same job.

Redirect Url can now be override using this subscriber.

Below is an Event Subscriber example to implement this override :

your_module.services.yml :

services:
  your_module.subscriber:
    class: Drupal\your_module\EventSubscriber\YourModuleSubscriber
    tags:
      - { name: event_subscriber }

YourModuleSubscriber.php :

namespace Drupal\your_module\EventSubscriber;

use Drupal\r4032login\Event\RedirectEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Redirect 403 to User Login event subscriber.
 */
class YourModuleSubscriber implements EventSubscriberInterface  {

  static function getSubscribedEvents() {
    $events[RedirectEvent::EVENT_NAME] = ['onRedirect', 100];

    return $events;
  }

  /**
   * Alter redirect url before the url is perform.
   *
   * @param \Drupal\r4032login\Event\RedirectEvent
   *   The Event to process.
   */
  public function onRedirect(RedirectEvent $event) {
    $event->setUrl('/node/2');

    $options = $event->getOptions();
    $options['query']['destination'] = 'test';
    $event->setOptions($options);
  }

}
estoyausente’s picture

@Nixou This information is too valuable to leave in a issue comment, where can I move it? Has the module any unlinked guide or can I add to the readme? I think that include it in readme is not the correct way but the module is not too big and create a documentation page maybe is overkill.

Nixou’s picture

If we add this code snippet in the readme file, it could become unreadable.
So a documentation page will be better certainly.

However it needs some access rights to create a guide for the module and I have not.

I'll ask @bdone to determine how we can achieve this.

Thanks for the proposition.

estoyausente’s picture

Great! thank for considering it!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.