By Gaelan on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Description:
hook_url_outbound_alter was removed. Instead:
- Create a class implementing OutboundPathProcessorInterface.
- Add that as a service to your module's
*.services.yml, with thepath_processor_outboundtag.
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
Missing parameter
At a later stage, the BubbleableMetadata has been added as a fourth parameter. So the
processOutboundfunction now looks like: