modules/custom/pco_cities_ext/challenge_pages/src/EventSubscriber/ChallengePagesRedirectSubscriber.php
public function redirectMyContentTypeNode(GetResponseEvent $event) {
    $request = $event->getRequest();
    $language = $this->languageManager->getCurrentLanguage()->getId();

    if ($request->attributes->get('_route') !== 'entity.node.canonical') {
      return;
    }

    if ($request->attributes->get('node')->getType() !== 'challenge') {
      return;
    }

    // $redirect_url = Url::fromUri('entity:node/123');.
    $node = $request->attributes->get('node');
    if ($language == 'fr') {
      $redirect_url = '/fr/defis/' . $node->getTranslation('fr')->get('field_friendly_url')->getValue()[0]['value'];
    }
    else {
      $redirect_url = '/en/challenges/' . $node->get('field_friendly_url')->getValue()[0]['value'];
    }

    $response = new RedirectResponse($redirect_url, 301);
    $event->setResponse($response);
  }
public function redirectMyContentTypeNode(GetResponseEvent $event) {
    $request = $event->getRequest();
    $language = $this->languageManager->getCurrentLanguage()->getId();

    if ($request->attributes->get('_route') !== 'entity.node.canonical') {
      return;
    }

    if ($request->attributes->get('node')->getType() !== 'challenge') {
      return;
    }

    // $redirect_url = Url::fromUri('entity:node/123');.
    $node = $request->attributes->get('node');
    if ($language == 'fr') {
      $redirect_url = '/fr/defis/' . $node->getTranslation('fr')->get('field_friendly_url')->getValue()[0]['value'];
    }
    else {
      $redirect_url = '/en/challenges/' . $node->get('field_friendly_url')->getValue()[0]['value'];
    }

    $response = new RedirectResponse($redirect_url, 301);
    $event->setResponse($response);
  }
public function redirectMyContentTypeNode ( GetResponseEvent $ event ) { $ request = $ event -> getRequest ( ) ; $ language = $ this -> languageManager -> getCurrentLanguage ( ) -> getId ( ) ; if ( $ request -> attributes -> get ( '_route' ) !== 'entity.node.canonical' ) { return ; } if ( $ request -> attributes -> get ( 'node' ) -> getType ( ) !== 'challenge' ) { return ; } // $redirect_url = Url::fromUri('entity:node/123');. $ node = $ request -> attributes -> get ( 'node' ) ; if ( $ language == 'fr' ) { $ redirect_url = '/fr/defis/' . $ node -> getTranslation ( 'fr' ) -> get ( 'field_friendly_url' ) -> getValue ( ) [ 0 ] [ 'value' ] ; } else { $ redirect_url = '/en/challenges/' . $ node -> get ( 'field_friendly_url' ) -> getValue ( ) [ 0 ] [ 'value' ] ; } $ response = new RedirectResponse ( $ redirect_url , 301 ) ; $ event -> setResponse ( $ response ) ; }
Redirect requests for challenge go to custom module controller route.

@param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
Redirect requests for challenge go to custom module controller route .