DomainPathAliasManager.php in Domain Path 8
File
src/DomainPathAliasManager.php
View source
<?php
namespace Drupal\domain_path;
use Drupal\path_alias\AliasManager;
use Drupal\path_alias\AliasRepositoryInterface;
use Drupal\path_alias\AliasWhitelistInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
class DomainPathAliasManager extends AliasManager {
protected $method;
protected $domainPath;
public function __construct($alias_repository, AliasWhitelistInterface $whitelist, LanguageManagerInterface $language_manager, CacheBackendInterface $cache) {
parent::__construct($alias_repository, $whitelist, $language_manager, $cache);
}
public function getPathByAlias($alias, $langcode = NULL) {
$config = \Drupal::config('domain_path.settings');
$this->method = $config
->get('language_method') ? $config
->get('language_method') : LanguageInterface::TYPE_CONTENT;
$active = \Drupal::service('domain.negotiator')
->getActiveDomain();
if ($active === NULL) {
$active = \Drupal::service('domain.negotiator')
->getActiveDomain(TRUE);
}
if ($active) {
$properties = [
'alias' => $alias,
'domain_id' => \Drupal::service('domain.negotiator')
->getActiveDomain()
->id(),
];
$langcode = $langcode ?: $this->languageManager
->getCurrentLanguage($this->method)
->getId();
if ($langcode != NULL) {
$properties['language'] = $langcode;
}
else {
$properties['language'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
$domain_paths = \Drupal::entityTypeManager()
->getStorage('domain_path')
->loadByProperties($properties);
$this->domainPath = reset($domain_paths);
if ($this->domainPath) {
return $this->domainPath
->getSource();
}
}
return parent::getPathByAlias($alias, $langcode);
}
public function getAliasByPath($path, $langcode = NULL) {
$config = \Drupal::config('domain_path.settings');
$this->method = $config
->get('language_method') ? $config
->get('language_method') : LanguageInterface::TYPE_CONTENT;
$active = \Drupal::service('domain.negotiator')
->getActiveDomain();
if ($active === NULL) {
$active = \Drupal::service('domain.negotiator')
->getActiveDomain(TRUE);
}
if ($active) {
$properties = [
'source' => $path,
'domain_id' => \Drupal::service('domain.negotiator')
->getActiveDomain()
->id(),
];
$langcode = $langcode ?: $this->languageManager
->getCurrentLanguage($this->method)
->getId();
if ($langcode != NULL) {
$properties['language'] = $langcode;
}
$domain_paths = \Drupal::entityTypeManager()
->getStorage('domain_path')
->loadByProperties($properties);
$this->domainPath = reset($domain_paths);
if ($this->domainPath) {
return $this->domainPath
->getAlias();
}
}
return parent::getAliasByPath($path, $langcode);
}
}