Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multilingual URL Alias Fallback
<?php
namespace Drupal\my_module;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasStorageInterface;
class FallbackAliasStorage implements AliasStorageInterface {
/**
* The base AliasStorage class that we're decorating.
*
* @var \Drupal\Core\Path\AliasStorageInterface
*/
protected $subject;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $language_manager;
public function __construct(AliasStorageInterface $subject, LanguageManagerInterface $language_manager) {
$this->subject = $subject;
$this->language_manager = $language_manager;
}
/**
* @inheritdoc
*/
public function save($source, $alias, $langcode = \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
return $this->subject->save($source, $alias, $langcode, $pid);
}
/**
* @inheritdoc
*/
public function load($conditions) {
return $this->subject->load($conditions);
}
/**
* @inheritdoc
*/
public function delete($conditions) {
return $this->subject->delete($conditions);
}
/**
* @inheritdoc
*/
public function preloadPathAlias($preloaded, $langcode) {
return $this->subject->preloadPathAlias($preloaded, $langcode);
}
/**
* @inheritdoc
*/
public function lookupPathAlias($path, $langcode) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
return $this->subject->lookupPathAlias($path, $langcode);
}
$context = [
'langcode' => $langcode,
'operation' => 'path_alias',
];
$fallbacks = $this->language_manager->getFallbackCandidates($context);
unset($fallbacks[LanguageInterface::LANGCODE_NOT_SPECIFIED]);
foreach ($fallbacks as $fallback) {
$alias = $this->subject->lookupPathAlias($path, $fallback);
if ($alias) {
return $alias;
}
}
return FALSE;
}
/**
* @inheritdoc
*/
public function lookupPathSource($path, $langcode) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
return $this->subject->lookupPathSource($path, $langcode);
}
$context = [
'langcode' => $langcode,
'operation' => 'path_alias',
];
$fallbacks = $this->language_manager->getFallbackCandidates($context);
unset($fallbacks[LanguageInterface::LANGCODE_NOT_SPECIFIED]);
foreach ($fallbacks as $fallback) {
$source = $this->subject->lookupPathSource($path, $fallback);
if ($source) {
return $source;
}
}
return FALSE;
}
/**
* @inheritdoc
*/
public function aliasExists($alias, $langcode, $source = NULL) {
return $this->subject->aliasExists($alias, $langcode, $source);
}
/**
* @inheritdoc
*/
public function languageAliasExists() {
return $this->subject->languageAliasExists();
}
/**
* @inheritdoc
*/
public function getAliasesForAdminListing($header, $keys = NULL) {
return $this->subject->getAliasesForAdminListing($header, $keys);
}
/**
* @inheritdoc
*/
public function pathHasMatchingAlias($initial_substring) {
return $this->subject->pathHasMatchingAlias($initial_substring);
}
}
services:
my_module.path.decorating_alias_storage:
class: Drupal\my_module\FallbackAliasStorage
decorates: path.alias_storage
arguments: ['@my_module.path.decorating_alias_storage.inner', '@language_manager']
public: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment