Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Modify _all_ Drupal links
namespace Drupal\wt_base;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Render\BubbleableMetadata;
* Class DefaultService.
class LinkModifierService implements OutboundPathProcessorInterface {
* Symfony\Component\HttpFoundation\RequestStack definition.
* @var \Symfony\Component\HttpFoundation\RequestStack
protected $requestStack;
* Constructs a new DefaultService object.
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
* {@inheritdoc}
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if (empty($request)) {
$request = $this->requestStack->getCurrentRequest();
// On every page of type "product" add the product id as query parameter to every link on that page
// no matter if that link is in a menu, a block or in a CKeditor text field of the node.
// It does only modify interal though, but not links to external homepages.
$node = $request->attributes->get('node');
if ($node && $node->bundle() === 'product') {
$options['query'][] = [$node->bundle() => $node->id()];
return $path;
class: Drupal\wt_base\LinkModifierService
- { name: path_processor_outbound, priority: 3000 }
arguments: ['@request_stack']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.