1

I am trying to implement a feature where I need to run different flow than default flow while the registering user on the site.

I am trying to alter the route of "user password reset" for the for the first time logging users, Below is the code

    <?php

    namespace Drupal\developer_registration\Routing;

    use Drupal\Core\Routing\RouteSubscriberBase;
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\HttpFoundation\Request;

    /**
     * Listens to the dynamic route events.
     */
    class RouteSubscriber extends RouteSubscriberBase {

      /**
       * {@inheritdoc}
       */
      protected function alterRoutes(RouteCollection $collection) {
      // The Test is getting printed on only cache clear and not on URL Hit       
      echo "Test"; exit;

      if(isset($_SESSION['developer_registration']['email_id'])) {  

       if ($route = $collection->get('user.reset')) {
                 $route->setDefaults(array(
                 '_controller' => '\Drupal\developer_registration\Controller\UserCustomController::setPasswd',
              ));
             }  
          }
       }
 }

The problem is that above alterRoute method doesn't runs on the URL hit. It only runs on cache. But my problem is that I want to run alter route on particular condition.

I am not getting the way to do the same. Please help me.

  • did you declare it in your .services.yml file ? – berramou Dec 31 '19 at 11:39
  • @berramou Yes. I declared it and thats why it is getting executed on cache clear but not on every URL Hit – Akki Dec 31 '19 at 11:41
  • Yes, it runs only on cache clear. You have to implement the dynamic behavior inside of the controller. By extending the core user controller you can call the parent method in case the dynamic condition doesn't apply. – 4k4 Dec 31 '19 at 12:38
  • @4k4 : Any Example to which I refer to ? I am new to Drupal 8 – Akki Dec 31 '19 at 13:02
0

Yes, it runs only on cache clear. You have to implement the dynamic behavior inside of the controller. By extending the core user controller you can call the parent method in case the dynamic condition doesn't apply:

class UserCustomController extends UserController {

  public function resetPass(Request $request, $uid, $timestamp, $hash) {
    if (!...) {
      return parent::resetPass(Request $request, $uid, $timestamp, $hash);
    }

    // return the redirect you want
  }

}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.