You are here

public function PathautoGenerator::updateEntityAlias in Pathauto 8

Creates or updates an alias for the given entity.

Parameters

EntityInterface $entity: Entity for which to update the alias.

string $op: The operation performed (insert, update)

array $options:

  • force: will force updating the path
  • language: the language for which to create the alias

Return value

array|null

  • An array with alias data in case the alias has been created or updated.
  • NULL if no operation performed.

Overrides PathautoGeneratorInterface::updateEntityAlias

File

src/PathautoGenerator.php, line 345

Class

PathautoGenerator
Provides methods for generating path aliases.

Namespace

Drupal\pathauto

Code

public function updateEntityAlias(EntityInterface $entity, $op, array $options = []) {

  // Skip if the entity does not have the path field.
  if (!$entity instanceof ContentEntityInterface || !$entity
    ->hasField('path')) {
    return NULL;
  }

  // Skip if pathauto processing is disabled.
  if ($entity->path->pathauto != PathautoState::CREATE && empty($options['force'])) {
    return NULL;
  }

  // Only act if this is the default revision.
  if ($entity instanceof RevisionableInterface && !$entity
    ->isDefaultRevision()) {
    return NULL;
  }
  $options += [
    'language' => $entity
      ->language()
      ->getId(),
  ];
  $type = $entity
    ->getEntityTypeId();

  // Skip processing if the entity has no pattern.
  if (!$this
    ->getPatternByEntity($entity)) {
    return NULL;
  }

  // Deal with taxonomy specific logic.
  // @todo Update and test forum related code.
  if ($type == 'taxonomy_term') {
    $config_forum = $this->configFactory
      ->get('forum.settings');
    if ($entity
      ->bundle() == $config_forum
      ->get('vocabulary')) {
      $type = 'forum';
    }
  }
  try {
    $result = $this
      ->createEntityAlias($entity, $op);
  } catch (\InvalidArgumentException $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
    return NULL;
  }

  // @todo Move this to a method on the pattern plugin.
  if ($type == 'taxonomy_term') {
    foreach ($this
      ->loadTermChildren($entity
      ->id()) as $subterm) {
      $this
        ->updateEntityAlias($subterm, $op, $options);
    }
  }
  return $result;
}