View source
<?php
namespace Drupal\domain_path_pathauto;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\pathauto\PathautoGenerator;
class DomainPathautoGenerator extends PathautoGenerator {
public function createEntityAlias(EntityInterface $entity, $op, $domain_id = '') {
$pattern = $this
->getPatternByEntity($entity);
if (empty($pattern)) {
return NULL;
}
try {
$internalPath = $entity
->toUrl()
->getInternalPath();
} catch (EntityMalformedException $exception) {
return NULL;
} catch (UndefinedLinkTemplateException $exception) {
return NULL;
} catch (\UnexpectedValueException $exception) {
return NULL;
}
$source = '/' . $internalPath;
$config = $this->configFactory
->get('pathauto.settings');
$langcode = $entity
->language()
->getId();
if ($langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
$data = [
$this->tokenEntityMapper
->getTokenTypeForEntityType($entity
->getEntityTypeId()) => $entity,
];
$context = [
'module' => $entity
->getEntityType()
->getProvider(),
'op' => $op,
'source' => $source,
'data' => $data,
'bundle' => $entity
->bundle(),
'language' => &$langcode,
'domain_id' => $domain_id,
];
$pattern_original = $pattern
->getPattern();
$this->moduleHandler
->alter('pathauto_pattern', $pattern, $context);
$pattern_altered = $pattern
->getPattern();
$existing_alias = NULL;
if ($op == 'update' || $op == 'bulkupdate') {
if ($existing_alias = $this->aliasStorageHelper
->loadBySource($source, $langcode)) {
switch ($config
->get('update_action')) {
case PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW:
return NULL;
}
}
}
$alias = $this->token
->replace($pattern
->getPattern(), $data, [
'clear' => TRUE,
'callback' => [
$this->aliasCleaner,
'cleanTokenValues',
],
'langcode' => $langcode,
'pathauto' => TRUE,
], new BubbleableMetadata());
$pattern_tokens_removed = preg_replace('/\\[[^\\s\\]:]*:[^\\s\\]]*\\]/', '', $pattern
->getPattern());
if ($alias === $pattern_tokens_removed) {
return NULL;
}
$alias = $this->aliasCleaner
->cleanAlias($alias);
$context['source'] =& $source;
$context['pattern'] = $pattern;
$this->moduleHandler
->alter('pathauto_alias', $alias, $context);
if (!mb_strlen($alias)) {
return NULL;
}
$original_alias = $alias;
$this->aliasUniquifier
->uniquify($alias, $source, $langcode, $domain_id);
if ($original_alias != $alias) {
$this->pathautoMessenger
->addMessage($this
->t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', [
'%original_alias' => $original_alias,
'%alias' => $alias,
]), $op);
}
if ($op == 'return') {
return $alias;
}
$path = [
'source' => $source,
'alias' => $alias,
'language' => $langcode,
];
$return = $this->aliasStorageHelper
->save($path, $existing_alias, $op);
if ($pattern_altered !== $pattern_original) {
$pattern
->setPattern($pattern_original);
}
return $return;
}
public function domainPathPathautoGenerationIsEnabled(EntityInterface $entity, $domain_id) {
$collection = $this
->getDomainPathPathautoStateCollection($entity, $domain_id);
$value = \Drupal::keyValue($collection)
->get($entity
->id());
if ($value === NULL) {
return FALSE;
}
return $value;
}
protected function getDomainPathPathautoStateCollection(EntityInterface $entity, $domain_id) {
return 'domain_path_pathauto_state.' . $domain_id . '.' . $entity
->getEntityTypeId();
}
public function setDomainPathPathautoState($entity, $domain_id, $value) {
$collection = $this
->getDomainPathPathautoStateCollection($entity, $domain_id);
\Drupal::keyValue($collection)
->set($entity
->id(), $value);
}
public function deleteDomainPathPathautoState($entity, $domain_id) {
$collection = $this
->getDomainPathPathautoStateCollection($entity, $domain_id);
\Drupal::keyValue($collection)
->delete($entity
->id());
}
}