Hi Everybody,

I noticed odd issue with Rabbit Hole -> page redirect behaviour. When it redirects it always redirect to default language not current language...

For instance:

Foo node -> needs to redirect to /foo-parent

if I go go to /en/foo it takes me to /en/foo-parent -> all seems to be fine...
However if go to /de/foo it takes me to /en/foo-parent which is causes issues on multilingual sites.

Any suggestions?

Thanks,
Povilas

Comments

puogintas created an issue. See original summary.

povilas uogintas’s picture

Hey,

After experimentation I dropped using Rabbit hole approach to redirect nodes to another node and used my own script... I am attaching sample if anybody finds it useful....

I created custom event subscriber... Added this to my services.yml


services:
  my_module.RedirectToContent:
    class: Drupal\my_module\EventSubscriber\RedirectToContent
    tags:
      - { name: event_subscriber }

Then created this EventSubscriber it checks if current node has field field_redirect_to and that this field is not empty and then redirects to that node in correct language .... I got initial idea from https://drupal.stackexchange.com/questions/202351/how-can-i-redirect-nod... ....

/**
 * @file
 * Contains \Drupal\my_module\EventSubscriber\RedirectToContent
 */

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class RedirectToContent implements EventSubscriberInterface {
 
  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    /* This announces which events you want to subscribe to. We only need the request event for this example. Pass this an array of method names */
    return([
      KernelEvents::REQUEST => [
        ['RedirectToContentAction'],
      ]
    ]);
  }
 
  /**
   * Redirect requests for any node with field node detail pages to node/123.
   *
   * @param GetResponseEvent $event
   * @return void
   */
  public function RedirectToContentAction(GetResponseEvent $event) {
    $request = $event->getRequest();
 
    /* This is necessary because this also gets called on node sub-tabs such as "edit", "revisions", etc.  This prevents those pages from redirected. */
    if ($request->attributes->get('_route') !== 'entity.node.canonical') {
      return;
    }

    /* retrieve node object */
    $node = $request->attributes->get('node');

    /* ignore if field redirect_to does not exists */
    if(empty($node->field_redirect_to->entity)) {
      return;
    }

    /* Load referenced entity. */
    $redirect_node = $node->field_redirect_to->entity;
    $redirect_url = Url::fromUri('entity:node/' . $redirect_node->id());
    $response = new RedirectResponse($redirect_url->toString(), 301);

    $event->setResponse($response);
  }
}

0sarah0al’s picture

I am having the same problem, but with the user entity and page redirect.

dylan donkersgoed’s picture

Status: Active » Needs review
StatusFileSize
new1.29 KB

I'm attaching a patch that should fix this.

Target paths are run through Drupal's URL generation if they are not absolute, which will cause the language to be prepended when applicable.

  • Dylan Donkersgoed committed e0375d5 on 8.x-1.x
    Issue #2906587 by Dylan Donkersgoed: Page Redirect behaviour - languages
    
dylan donkersgoed’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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