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

Path alias management in Drupal 8 is now handled by two classes - one for CRUD operations (AliasStorage) and one for lookup (AliasManager). Both are provided as services in the DI container and can therefore be swapped out for different implementations, e.g. the 'path.alias_storage' service could be swapped out for an implementation that does not use the database. Many functions have been removed from core/includes/path.inc, their functionality being replaced by method calls on either the path.alias_storage service or the path.alias_manager.cached service. (There is also a path.alias_manager service, but it is not cached so should rarely be used directly.)

See AliasManager and AliasStorage for details.

Saving a path alias

  • Drupal 7
<?php
$alias['source'] = $source;
$alias['alias'] = $alias;
$alias['langcode'] = $langcode;
path_save($alias);
?>

  • Drupal 8
  • <?php
    $path = \Drupal::service('path.alias_storage')->save($system_path, $path_alias, $langcode);
    ?>

    Deleting a path alias

    • Drupal 7
    <?php
    path_delete($pid);
    ?>

  • Drupal 8
  • <?php
    $path = \Drupal::service('path.alias_storage')->delete(array('pid' => $pid));
    ?>

    Looking up a path alias from a system path

    • Drupal 7
    <?php
    drupal_get_path_alias($original_path, $langcode);
    ?>

  • Drupal 8
  • <?php
    $path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($system_path, $langcode);
    ?>

    Looking up a system path from a path alias

    • Drupal 7
    <?php
    drupal_get_normal_path($alias);
    ?>

  • Drupal 8
  • <?php
    $system_path = \Drupal::service('path.alias_manager')->getPathByAlias($path_alias, $langcode);
    ?>

    Altering the inbound url

    The process of resolving a path alias to a system path happens in a listener on the request, which now uses the path.alias_manager service to resolve the path. Because it no longer calls drupal_get_normal_path(), hook_url_inbound_alter is no longer invoked. In order to alter the inbound path, a module would need to add another request listener that would resolve it. It would do this by providing a service in a file named YOURMODULE.services.yml at the top level of your module:

    services:
      mymodule.path_subscriber:
        class: Drupal\my_module\PathSubscriber
        tags:
         - { name: event_subscriber }
    

    And then the PathSubscriber itself:

    <?php
    // This file should be located at: my_module/src/PathSubscriber.php
    /**
     * @file
     * Contains Drupal\my_module\PathSubscriber.
     */
    
    namespace Drupal\my_module;
    
    use Drupal\Core\EventSubscriber\PathListenerBase;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Path subscriber for my_module.
     */
    class PathSubscriber extends PathListenerBase implements EventSubscriberInterface {
    
      /**
       * Resolve the system path based on some arbitrary rules.
       *
       * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
       *   The Event to process.
       */
      public function onKernelRequestPathResolve(GetResponseEvent $event) {
        $request = $event->getRequest();
        $path = $this->extractPath($request);
    
        // Rewrite community/ to forum/.
        if ($path == 'community' || strpos($path, 'community/') === 0) {
          $path = 'forum' . substr($path, 9);
        }
    
        $this->setPath($request, $path);
      }
    
      /**
       * Registers the methods in this class that should be listeners.
       *
       * @return array
       *   An array of event listener definitions.
       */
      static function getSubscribedEvents() {
        $events[KernelEvents::REQUEST][] = array('onKernelRequestPathResolve', 100);
        return $events;
      }
    }
    ?>
    Impacts: 
    Module developers
    Updates Done (doc team, etc.)
    Online documentation: 
    Not done
    Theming guide: 
    Not done
    Module developer documentation: 
    Not done
    Examples project: 
    Not done
    Coder Review: 
    Not done
    Coder Upgrade: 
    Not done
    Other: 
    Other updates done

    Comments

    piyuesh23’s picture

    Was trying to port a module form d7 to d8 & needed to lookup for path aliases and i landed up here.

    The documentation above seems outdated. Drupal\Core\Path\AliasStorage.php is the callback for alias_storage service. I couldn't find the function mentioned above but instead there is a function lookupPathAlias with same arguments.

    \Drupal::service('path.alias_storage')->lookupPathAlias($query_path, 'en') works fine for me.