diff --git a/src/PathautoState.php b/src/PathautoState.php index 4e1f63b..c25807c 100644 --- a/src/PathautoState.php +++ b/src/PathautoState.php @@ -42,11 +42,23 @@ class PathautoState extends TypedData { $this->value = \Drupal::keyValue($this->getCollection()) ->get(static::getPathautoStateKey($this->parent->getEntity()->id())); // If it was not yet saved or no value was found, then set the flag to - // create the alias if there is a matching pattern. + // create the alias if there is a matching pattern an no alias already + // exists. if ($this->value === NULL) { $entity = $this->parent->getEntity(); $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity); - $this->value = !empty($pattern) ? static::CREATE : static::SKIP; + + // If an entity is new, it will not have a node id or internal path set. + // Only apply the following logic to an entity if it is not new. + if (!$entity->isNew()) { + // Compare internal path to alias to determine if an manual alias has + // already been created. + $internal_path = '/' . $entity->toUrl()->getInternalPath(); + $path = $entity->toUrl()->toString(); + $create = !empty($pattern) && ($internal_path === $path); + + $this->value = $create ? static::CREATE : static::SKIP; + } } } return $this->value;