Forked from mohit-rocks/FallbackAliasStorage.php
Last active
January 24, 2019 09:43
-
-
Save Wooody82/9161bf6e0d028a2a5cfcf8b6bb68c2ec to your computer and use it in GitHub Desktop.
Multilingual URL Alias Fallback.. Move FallbackAliasStorage.php inside src folder
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
name: Alias Fallback module | |
type: module | |
description: Alias Fallback module | |
core: 8.x | |
package: Other | |
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