Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

hook_url_outbound_alter was removed. Instead:

  1. Create a class implementing OutboundPathProcessorInterface.
  2. Add that as a service to your module's *.services.yml, with the path_processor_outbound tag.

Drupal 7

function foo_url_outbound_alter(&$path, &$options, $original_path) {
  // Add /foo to the end of every path.
  $path .= "/foo";
}

Drupal 8

modules/foo/src/PathProcessor/FooOutboundPathProcessor.php

class FooOutboundPathProcessor implements OutboundPathProcessorInterface {
  function processOutbound($path, &$options = array(), Request $request = NULL) {
    return $path . "/foo";
  }
}

modules/foo/foo.services.yml

services:
  foo.path_processor_foo:
    class: Drupal\foo\PathProcessor\FooOutboundPathProcessor
    tags:
      - { name: path_processor_outbound }

External URLs

Drupal 7 supported processing outbound external URLs with hook_url_outbound_alter, but this is not supported by a path processor. If the outbound URL is rendered as part of a link, then hook_link_alter can be used to alter the URL.

modules/foo/foo.module
function foo_link_alter(&$variables) {
  $url = $variables['url'];
  if ($url->isExternal()) {
    // Adds '/foo' to the end of every external URL path.
    $variables['url'] = \Drupal\Core\Url::fromUri($url->getUri() . '/foo');
  }
}
Impacts: 
Module developers

Comments

neograph734’s picture

At a later stage, the BubbleableMetadata has been added as a fourth parameter. So the processOutbound function now looks like:

function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {

}