InboundPathProcessorInterface in Drupal with Cache – Easy Guide

In Drupal, URLs are very important. When a user visits a page like /about-us, Drupal must convert it into an internal system path like /node/5.

This is where InboundPathProcessorInterface comes in. It helps us change or rewrite incoming paths before Drupal routes them.

In this blog, we will learn:

  • What is InboundPathProcessorInterface in Drupal.
  • How to create a custom path processor.
  • How to add cache information for better performance.

What is InboundPathProcessorInterface?

InboundPathProcessorInterface is a Drupal core interface (found in \Drupal\Core\PathProcessor) that defines how inbound paths (the URLs requested by a user) can be altered before Drupal routes them.

In simpler words:
When a user visits a URL like https://example.com/some-alias, Drupal first needs to resolve that request into an internal system path (like /node/123). The Inbound Path Processor is what allows modules to intercept and rewrite paths before routing takes place.

It is a Drupal interface that allows developers to process incoming paths.

Example:
If a user visits /blog/123, you can use an inbound path processor to convert it into /node/123 internally.

This is useful for:

  • Handling legacy URLs.
  • Supporting custom path patterns.
  • Managing multilingual URLs.

Example: Custom Inbound Path Processor

Let’s create a simple inbound path processor.

Step 1: Create PHP Class

<?php
namespace Drupal\custom_module\PathProcessor;

use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;

class BlogPathProcessor implements InboundPathProcessorInterface {

  public function processInbound($path, Request $request) {
    // If path starts with /blog/, change it to /node/.
    if (strpos($path, '/blog/') === 0) {
      $path = preg_replace('/^\/blog\//', '/node/', $path);
    }
    return $path;
  }
}

?>

Step 2: Register as Service
In custom_module.services.yml:

yaml

services:
  custom_module.path_processor.blog:
    class: Drupal\custom_module\PathProcessor\BlogPathProcessor
    tags:
      - { name: path_processor_inbound, priority: 100 }

Adding Cache Information

Sometimes, you need to control caching for inbound paths.
For example, when paths depend on user roles, languages, or query parameters.

In that case, your class should also implement CacheableDependencyInterface.

Updated Example:

<?php

namespace Drupal\custom_module\PathProcessor;

use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;

class BlogPathProcessor implements InboundPathProcessorInterface, CacheableDependencyInterface {

  public function processInbound($path, Request $request) {
    if (strpos($path, '/blog/') === 0) {
      $path = preg_replace('/^\/blog\//', '/node/', $path);
    }
    return $path;
  }

  // Add cache metadata
  public function getCacheContexts() {
    return ['url.path']; // Cache depends on the path
  }

  public function getCacheTags() {
    return ['node_list']; // Invalidate when node list changes
  }

  public function getCacheMaxAge() {
    return CacheableMetadata::CACHE_PERMANENT; // Cache forever
  }
}


?>

Why Add Cache Information?

  • Performance: Pages load faster.
  • Accuracy: Cache clears when content changes.
  • Scalability: Works well with reverse proxies like Varnish or CDN.

InboundPathProcessorInterface in Drupal is a powerful way to rewrite incoming paths before routing.
By adding cache information, you ensure your site remains fast, reliable, and scalable.

As a fresher, remember these key points:

  • Use inbound processors to handle custom or legacy URLs.
  • Always add cache contexts, tags, and max-age for best performance.

Now you know how to use InboundPathProcessorInterface in Drupal with cache support