DomainSourcePathProcessor.php in Domain Access 8
File
domain_source/src/HttpKernel/DomainSourcePathProcessor.php
View source
<?php
namespace Drupal\domain_source\HttpKernel;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
class DomainSourcePathProcessor implements OutboundPathProcessorInterface {
protected $negotiator;
protected $moduleHandler;
protected $entityTypeManager;
protected $aliasManager;
protected $configFactory;
protected $entityTypes;
protected $excludedRoutes;
protected $activeDomain;
protected $domainStorage;
public function __construct(DomainNegotiatorInterface $negotiator, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, AliasManagerInterface $alias_manager, ConfigFactoryInterface $config_factory) {
$this->negotiator = $negotiator;
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
$this->aliasManager = $alias_manager;
$this->configFactory = $config_factory;
}
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if (empty($options['active_domain'])) {
$active_domain = $this
->getActiveDomain();
}
if (empty($active_domain) || empty($path) || !empty($options['external'])) {
return $path;
}
$source = NULL;
$options['active_domain'] = $active_domain;
$langcode = NULL;
if (!empty($options['language'])) {
$langcode = $options['language']
->getId();
}
$alias = $this->aliasManager
->getPathByAlias($path, $langcode);
$url = Url::fromUserInput($alias, $options);
if ($url
->isRouted()) {
$options['route_name'] = $url
->getRouteName();
}
if ($url
->isRouted() && $this
->allowedRoute($url
->getRouteName())) {
if (!empty($options['entity'])) {
$entity = $options['entity'];
}
else {
$parameters = $url
->getRouteParameters();
if (!empty($parameters)) {
$entity = $this
->getEntity($parameters);
}
}
}
if (!empty($entity) && is_object($entity)) {
if (!empty($langcode) && method_exists($entity, 'hasTranslation') && $entity
->hasTranslation($langcode) && ($translation = $entity
->getTranslation($langcode))) {
$entity = $translation;
}
if (isset($options['domain_target_id'])) {
$target_id = $options['domain_target_id'];
}
else {
$target_id = domain_source_get($entity);
}
if (!empty($target_id)) {
$source = $this
->domainStorage()
->load($target_id);
}
$options['entity'] = $entity;
$options['entity_type'] = $entity
->getEntityTypeId();
$this->moduleHandler
->alter('domain_source', $source, $path, $options);
}
else {
if (isset($options['domain_target_id'])) {
$target_id = $options['domain_target_id'];
$source = $this
->domainStorage()
->load($target_id);
}
$this->moduleHandler
->alter('domain_source_path', $source, $path, $options);
}
if (!empty($source)) {
$options['base_url'] = trim($source
->getPath(), '/');
$options['absolute'] = TRUE;
}
return $path;
}
public function getEntity(array $parameters) {
$entity = NULL;
$entity_type = key($parameters);
$entity_types = $this
->getEntityTypes();
foreach ($parameters as $entity_type => $value) {
if (!empty($entity_type) && isset($entity_types[$entity_type])) {
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($value);
}
}
return $entity;
}
public function allowedRoute($name) {
$excluded = $this
->getExcludedRoutes();
$parts = explode('.', $name);
$route_name = end($parts);
return !isset($excluded[$route_name]);
}
public function getEntityTypes() {
if (!isset($this->entityTypes)) {
foreach ($this->entityTypeManager
->getDefinitions() as $type => $definition) {
if ($definition
->getGroup() == 'content') {
$this->entityTypes[$type] = $type;
}
}
}
return $this->entityTypes;
}
public function getExcludedRoutes() {
if (!isset($this->excludedRoutes)) {
$config = $this->configFactory
->get('domain_source.settings');
$routes = $config
->get('exclude_routes');
if (is_array($routes)) {
$this->excludedRoutes = array_flip($routes);
}
else {
$this->excludedRoutes = [];
}
}
return $this->excludedRoutes;
}
public function getActiveDomain() {
if (!isset($this->activeDomain)) {
$active = $this->negotiator
->getActiveDomain();
if (empty($active)) {
$active = $this->negotiator
->getActiveDomain(TRUE);
}
$this->activeDomain = $active;
}
return $this->activeDomain;
}
protected function domainStorage() {
if (!$this->domainStorage) {
$this->domainStorage = $this->entityTypeManager
->getStorage('domain');
}
return $this->domainStorage;
}
}