Commit a1b29b60 authored by catch's avatar catch
Browse files

Issue #3549608 by amateescu, plach: Language-neutral path aliases are not...

Issue #3549608 by amateescu, plach: Language-neutral path aliases are not deleted with the parent entity

(cherry picked from commit 018bfdae)
parent 034eaaff
Loading
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComputedItemListTrait;

@@ -55,12 +56,19 @@ public function defaultAccess($operation = 'view', ?AccountInterface $account =
   * {@inheritdoc}
   */
  public function delete() {
    // Delete all aliases associated with this entity in the current language.
    // Delete all aliases associated with this entity.
    $entity = $this->getEntity();
    $langcode_list = [$entity->language()->getId()];

    // The default translation may also have language-neutral aliases.
    if ($entity->isDefaultTranslation()) {
      $langcode_list[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    }

    $path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
    $entities = $path_alias_storage->loadByProperties([
      'path' => '/' . $entity->toUrl()->getInternalPath(),
      'langcode' => $entity->language()->getId(),
      'langcode' => $langcode_list,
    ]);
    $path_alias_storage->delete($entities);
  }
+16 −0
Original line number Diff line number Diff line
@@ -196,6 +196,22 @@ public function testAliasTranslation(): void {
    $this->drupalGet($english_alias);
    $this->assertPathAliasExists('/' . $english_alias, 'en', NULL, 'English alias is not deleted when French translation is removed.');
    $this->assertSession()->pageTextContains($english_node->body->value);

    // Replace the English alias with a language-neutral one.
    $en_alias_entity = $this->loadPathAliasByConditions(['alias' => '/' . $english_alias]);
    $en_alias_entity->delete();

    $und_alias = $this->randomMachineName();
    $this->createPathAlias('/node/' . $english_node->id(), '/' . $und_alias);

    $this->assertPathAliasExists('/' . $und_alias, LanguageInterface::LANGCODE_NOT_SPECIFIED);
    $this->drupalGet($und_alias);
    $this->assertSession()->pageTextContains($english_node->body->value);

    // Delete the default translation (English) of the node and check that the
    // language-neutral alias was deleted as well.
    $english_node->delete();
    $this->assertPathAliasNotExists('/' . $und_alias, LanguageInterface::LANGCODE_NOT_SPECIFIED, NULL, 'Language-neutral alias is removed when the default translation is deleted.');
  }

}