diff --git a/src/AliasStorageHelper.php b/src/AliasStorageHelper.php index 06ce2c4..0c6e9e8 100644 --- a/src/AliasStorageHelper.php +++ b/src/AliasStorageHelper.php @@ -114,6 +114,11 @@ class AliasStorageHelper implements AliasStorageHelperInterface { return NULL; } + // Don't create a new alias if it is identical to the current alias. + if ($existing_alias && $existing_alias->getAlias() == $alias) { + return NULL; + } + // Update the existing alias if there is one and the configuration is set to // replace it. if ($existing_alias && $config->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_DELETE) { diff --git a/tests/src/Functional/PathautoTestHelperTrait.php b/tests/src/Functional/PathautoTestHelperTrait.php index 6473c85..dd7694d 100644 --- a/tests/src/Functional/PathautoTestHelperTrait.php +++ b/tests/src/Functional/PathautoTestHelperTrait.php @@ -134,6 +134,17 @@ trait PathautoTestHelperTrait { $this->assertEmpty($alias, t('Alias with conditions @conditions not found.', ['@conditions' => var_export($conditions, TRUE)])); } + public function assertAliasIsUnique($conditions) { + $storage = \Drupal::entityTypeManager()->getStorage('path_alias'); + $query = $storage->getQuery()->accessCheck(FALSE); + foreach ($conditions as $field => $value) { + $query->condition($field, $value); + } + $entities = $storage->loadMultiple($query->execute()); + + return $this->assertCount(1, $entities); + } + public function deleteAllAliases() { \Drupal::service('pathauto.alias_storage_helper')->deleteAll(); \Drupal::service('path_alias.manager')->cacheClear(); diff --git a/tests/src/Kernel/PathautoKernelTest.php b/tests/src/Kernel/PathautoKernelTest.php index dd70405..853ace1 100644 --- a/tests/src/Kernel/PathautoKernelTest.php +++ b/tests/src/Kernel/PathautoKernelTest.php @@ -304,6 +304,11 @@ class PathautoKernelTest extends KernelTestBase { $this->assertEntityAlias($node, '/content/third-title'); $this->assertAliasExists(['path' => '/' . $node->toUrl()->getInternalPath(), 'alias' => '/content/second-title']); + // Confirm that aliases are not duplicated when entities are re-saved. + $node->save(); + $this->assertEntityAlias($node, '/content/third-title'); + $this->assertAliasIsUnique(['path' => '/' . $node->toUrl()->getInternalPath(), 'alias' => '/content/third-title']); + $config->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_DELETE); $config->save(); $node->setTitle('Fourth title');