2

For some SEO related reasons, I want to remove the destination paramaters that are attached to the login and register link urls below nodes.

The Link-URLs look like this:

http://example.com/user/login?destination=node/55%23comment-form

But I want it that way:

http://example.com/user/login

For Drupal 7, someone has made it with:

function moon_url_outbound_alter(&$path, &$options, $original_path) {
 // here "moon" is my theme, use your own.
  switch ($path) {
  case 'user/login':
  unset( $options['query']['destination'] );
  break;
 }
}

Source: How to remove the Destination parameter from an URL in Drupal 7?

This source https://www.drupal.org/node/2238759 says, in Drupal 8 hook_url_outbound_alter has been replaced with OutboundPathProcessorInterface. But with the given example there, I don't understand how I could make it to unset the destination paramaters with my .theme file.

Can someone please help and tell how to unset the destination paramaters in Drupal 8 ?

Thanks in Advance!

1
  • not possible in a .theme file, you need a module to implement the code of the linked change record
    – 4uk4
    Commented Jul 3, 2017 at 11:29

1 Answer 1

4

Before proceeding, realize that although this might be better for SEO; it might be negative for user experience. Personally I'd hate it if I were to comment on a node and after the login I have to navigate back to that node. You might want to perform some session magic to redirect the user back to where he came from.

The answer to your question involves a custom module, with a Path Processor service. In addition to the default module files (mymodule.module and mymodule.info.yml), you'll need mymodule.services.yml and src/PathProcessor/MyModulePathProcessor.php.

The services file will expose your path processor to Drupal with this code:

services:
  mymodule.path_processor:
    class: Drupal\mymodule\PathProcessor\MyModulePathProcessor
    # Optionally provide arguments.
    #arguments: ['@config.factory', '@entity_type.manager', '@current_user']
    tags:
      - { name: path_processor_outbound, priority: -10} # Low priority acts last.

This 'subscribes' the class to the path_processor_outbound event.

The Outbound path processor should look something like this:

<?php

namespace Drupal\mymodule\PathProcessor;

use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class MyModulePathProcessor.
 */
class MyModulePathProcessor implements OutboundPathProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    if ($path == 'user/login') {
      unset( $options['query']['destination'] );
    }
    return $path;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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