You are here

class DomainPathautoGenerator in Domain Path 8

Provides methods for generating domain path aliases.

For now, only op "return" is supported.

Hierarchy

Expanded class hierarchy of DomainPathautoGenerator

See also

\Drupal\pathauto\PathautoGenerator

1 string reference to 'DomainPathautoGenerator'
domain_path_pathauto.services.yml in modules/domain_path_pathauto/domain_path_pathauto.services.yml
modules/domain_path_pathauto/domain_path_pathauto.services.yml
1 service uses DomainPathautoGenerator
domain_path_pathauto.generator in modules/domain_path_pathauto/domain_path_pathauto.services.yml
Drupal\domain_path_pathauto\DomainPathautoGenerator

File

modules/domain_path_pathauto/src/DomainPathautoGenerator.php, line 18

Namespace

Drupal\domain_path_pathauto
View source
class DomainPathautoGenerator extends PathautoGenerator {

  /**
   * {@inheritdoc}
   */
  public function createEntityAlias(EntityInterface $entity, $op, $domain_id = '') {

    // Retrieve and apply the pattern for this content type.
    $pattern = $this
      ->getPatternByEntity($entity);
    if (empty($pattern)) {

      // No pattern? Do nothing (otherwise we may blow away existing aliases...)
      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();

    // Core does not handle aliases with language Not Applicable.
    if ($langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
      $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    }

    // Build token data.
    $data = [
      $this->tokenEntityMapper
        ->getTokenTypeForEntityType($entity
        ->getEntityTypeId()) => $entity,
    ];

    // Allow other modules to alter the pattern.
    $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();

    // Special handling when updating an item which is already aliased.
    $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:

            // If an alias already exists,
            // and the update action is set to do nothing,
            // then gosh-darn it, do nothing.
            return NULL;
        }
      }
    }

    // Replace any tokens in the pattern.
    // Uses callback option to clean replacements. No sanitization.
    // Pass empty BubbleableMetadata object to explicitly ignore cacheablity,
    // as the result is never rendered.
    $alias = $this->token
      ->replace($pattern
      ->getPattern(), $data, [
      'clear' => TRUE,
      'callback' => [
        $this->aliasCleaner,
        'cleanTokenValues',
      ],
      'langcode' => $langcode,
      'pathauto' => TRUE,
    ], new BubbleableMetadata());

    // Check if the token replacement has not actually replaced any values. If
    // that is the case, then stop because we should not generate an alias.
    // @see token_scan()
    $pattern_tokens_removed = preg_replace('/\\[[^\\s\\]:]*:[^\\s\\]]*\\]/', '', $pattern
      ->getPattern());
    if ($alias === $pattern_tokens_removed) {
      return NULL;
    }
    $alias = $this->aliasCleaner
      ->cleanAlias($alias);

    // Allow other modules to alter the alias.
    $context['source'] =& $source;
    $context['pattern'] = $pattern;
    $this->moduleHandler
      ->alter('pathauto_alias', $alias, $context);

    // If we have arrived at an empty string, discontinue.
    if (!mb_strlen($alias)) {
      return NULL;
    }

    // If the alias already exists, generate a new, hopefully unique, variant.
    $original_alias = $alias;
    $this->aliasUniquifier
      ->uniquify($alias, $source, $langcode, $domain_id);
    if ($original_alias != $alias) {

      // Alert the user why this happened.
      $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);
    }

    // Return the generated alias if requested.
    if ($op == 'return') {
      return $alias;
    }

    // Build the new path alias array and send it off to be created.
    $path = [
      'source' => $source,
      'alias' => $alias,
      'language' => $langcode,
    ];

    // TODO: allow
    $return = $this->aliasStorageHelper
      ->save($path, $existing_alias, $op);

    // Because there is no way to set an altered pattern to not be cached,
    // change it back to the original value.
    if ($pattern_altered !== $pattern_original) {
      $pattern
        ->setPattern($pattern_original);
    }
    return $return;
  }

  /**
   * Check if "Generate URL alias" is enabled for entity/domain combination.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param string $domain_id
   *
   * @return boolean
   *   Return TRUE or FALSE.
   */
  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;
  }

  /**
   * Returns the key value collection for the given entity/domain combination.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param string $domain_id
   *
   * @return string
   */
  protected function getDomainPathPathautoStateCollection(EntityInterface $entity, $domain_id) {
    return 'domain_path_pathauto_state.' . $domain_id . '.' . $entity
      ->getEntityTypeId();
  }

  /**
   * Set the stored state.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param string $domain_id
   */
  public function setDomainPathPathautoState($entity, $domain_id, $value) {
    $collection = $this
      ->getDomainPathPathautoStateCollection($entity, $domain_id);
    \Drupal::keyValue($collection)
      ->set($entity
      ->id(), $value);
  }

  /**
   * Deletes the stored state.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param string $domain_id
   */
  public function deleteDomainPathPathautoState($entity, $domain_id) {
    $collection = $this
      ->getDomainPathPathautoStateCollection($entity, $domain_id);
    \Drupal::keyValue($collection)
      ->delete($entity
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainPathautoGenerator::createEntityAlias public function Apply patterns to create an alias. Overrides PathautoGenerator::createEntityAlias
DomainPathautoGenerator::deleteDomainPathPathautoState public function Deletes the stored state.
DomainPathautoGenerator::domainPathPathautoGenerationIsEnabled public function Check if "Generate URL alias" is enabled for entity/domain combination.
DomainPathautoGenerator::getDomainPathPathautoStateCollection protected function Returns the key value collection for the given entity/domain combination.
DomainPathautoGenerator::setDomainPathPathautoState public function Set the stored state.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PathautoGenerator::$aliasCleaner protected property The alias cleaner.
PathautoGenerator::$aliasStorageHelper protected property The alias storage helper.
PathautoGenerator::$aliasTypeManager protected property Manages pathauto alias type plugins.
PathautoGenerator::$aliasUniquifier protected property The alias uniquifier.
PathautoGenerator::$configFactory protected property Config factory.
PathautoGenerator::$entityTypeManager protected property The entity type manager.
PathautoGenerator::$moduleHandler protected property Module handler.
PathautoGenerator::$pathautoMessenger protected property The messenger service.
PathautoGenerator::$patterns protected property Calculated pattern for a specific entity.
PathautoGenerator::$patternsByEntityType protected property Available patterns per entity type ID.
PathautoGenerator::$token protected property Token service.
PathautoGenerator::$tokenEntityMapper protected property The token entity mapper.
PathautoGenerator::getPatternByEntity public function Load an alias pattern entity by entity, bundle, and language. Overrides PathautoGeneratorInterface::getPatternByEntity
PathautoGenerator::getPatternByEntityType protected function Loads pathauto patterns for a given entity type ID.
PathautoGenerator::loadTermChildren protected function Finds all children of a term ID.
PathautoGenerator::resetCaches public function Resets internal caches. Overrides PathautoGeneratorInterface::resetCaches
PathautoGenerator::updateEntityAlias public function Creates or updates an alias for the given entity. Overrides PathautoGeneratorInterface::updateEntityAlias
PathautoGenerator::__construct public function Creates a new Pathauto manager.
PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING constant Leave the punctuation as it is in the alias.
PathautoGeneratorInterface::PUNCTUATION_REMOVE constant Remove the punctuation from the alias.
PathautoGeneratorInterface::PUNCTUATION_REPLACE constant Replace the punctuation with the separator in the alias.
PathautoGeneratorInterface::UPDATE_ACTION_DELETE constant "Create a new alias. Delete the old alias."
PathautoGeneratorInterface::UPDATE_ACTION_LEAVE constant "Create a new alias. Leave the existing alias functioning."
PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW constant "Do nothing. Leave the old alias intact."
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.