Created
January 26, 2018 04:49
-
-
Save mohit-rocks/d67db9ca07d111f2003cbf80a0f59b53 to your computer and use it in GitHub Desktop.
Multilingual URL Alias Fallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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