domain_microsite-1.0.0-alpha4/src/DomainMicrositePathProcessor.php
src/DomainMicrositePathProcessor.php
<?php
namespace Drupal\domain_microsite;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\path_alias\AliasManagerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Processes inbound and outbound paths for domain microsites.
*/
class DomainMicrositePathProcessor implements OutboundPathProcessorInterface, InboundPathProcessorInterface {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Domain negotiator.
*
* @var \Drupal\domain\DomainNegotiatorInterface
*/
protected $domainNegotiator;
/**
* Alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* DomainMicrositePathProcessor constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\domain\DomainNegotiatorInterface $domain_negotiator
* The domain negotiator.
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* The path alias manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, DomainNegotiatorInterface $domain_negotiator, AliasManagerInterface $alias_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->domainNegotiator = $domain_negotiator;
$this->aliasManager = $alias_manager;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function processInbound($path, Request $request) {
$microsite_path = domain_microsite_base_path();
if ($microsite_path != '' && strpos($path, '/' . $microsite_path) === 0) {
$new_path = substr($path, strlen('/' . $microsite_path));
if ($new_path == '') {
$path = '/';
}
// Only modify the path if the removed word matched whole word path parts.
elseif (strpos($new_path, '/') === 0) {
$path = $new_path;
}
}
return $path;
}
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if ($domain = $this->domainNegotiator->getActiveDomain()) {
// If an entity is not assigned to the active domain or all affiliates,
// the path needs to be altered using an assigned domain or else the path
// will be inaccessible to non-admins.
$entity = $options['entity'] ?? FALSE;
$new_domain_id = FALSE;
$assigned_domains = [];
if ($entity instanceof ContentEntityInterface) {
if ($entity->hasField('field_domain_access')) {
$assigned_domains = array_column($entity->get('field_domain_access')
->getValue(), 'target_id');
// @todo make domain_entity module compatible - it uses no domain assignment to mean all affiliates.
$all_affiliates = ($entity->hasField('field_domain_all_affiliates') && $entity->get('field_domain_all_affiliates')->value) ?? FALSE;
if ($all_affiliates == FALSE && !empty($assigned_domains) && !in_array($domain->id(), $assigned_domains)) {
// Use the first assigned domain.
$new_domain_id = $assigned_domains[0];
}
}
// If Domain Source is set, use the source domain.
if ($entity->hasField('field_domain_source')) {
$target_ids = array_column($entity->get('field_domain_source')->getValue(), 'target_id');
$domain_source = array_shift($target_ids);
if (!empty($domain_source) && $domain_source != '_none') {
if (empty($assigned_domains) or in_array($domain_source, $assigned_domains)) {
$new_domain_id = $domain_source;
}
}
}
if ($new_domain_id) {
/** @var \Drupal\domain\DomainInterface $new_domain */
$new_domain = $this->entityTypeManager->getStorage('domain')->load($new_domain_id);
$options['base_url'] = domain_microsite_scheme_and_host($new_domain->getPath());
$microsite_path = domain_microsite_base_path($new_domain);
if ($microsite_path != '') {
$options['prefix'] = $microsite_path . '/' . ($options['prefix'] ?? '');
}
// Get the domain path alias on the new domain.
// Domain_path 8.x-1.0 switching from path processors to path
// manager alias override is why this is like this.
if ($this->moduleHandler->moduleExists('domain_path')) {
$source = $this->aliasManager->getPathByAlias($path);
$this->domainNegotiator->setActiveDomain($new_domain);
$path = $this->aliasManager->getAliasByPath($source);
$this->domainNegotiator->setActiveDomain($domain);
}
}
}
$microsite_path = domain_microsite_base_path();
if (!isset($new_domain) && $microsite_path != '') {
$options['prefix'] = $microsite_path . '/' . ($options['prefix'] ?? '');
}
}
if ($bubbleable_metadata) {
$bubbleable_metadata->addCacheContexts(['url.site']);
}
return $path;
}
}
