We are running Drupal 8.8.5. I can see duplicate aliases when I look at /admin/config/search/path.
Unfortunately, there are many duplicate aliases on our website with 6000 URLs. The /node/1 node has three translations: en, es, and zh-hans. /node/1 also has a second entry for es containing a duplicate path alias which I would like to delete.
I need to find a way to programmatically delete path aliases. If I have Create, Read, Update and Delete (CRUD) operations on the path/alias/langcode, I can create a solution to eliminate the duplicates.
The following code obtains the alias data associated with the node whose ID is equal to 1 and dumps the Path, Alias and LangCode.
$alias = \Drupal::entityTypeManager()->getStorage('path_alias')->load('1');
var_dump($alias->getPath());
var_dump($alias->getAlias());
var_dump($alias->language()->getId());
The base language of the website is English, and the last var_dump()
yields en. So far so good.
How can I gain access to the alias data for these two languages? I know the following works.
$alias_es = \Drupal::service('path_alias.manager')->getAliasByPath('/node/1','es');
I need to delete the duplicate Spanish path alias to accomplish my goal, so I need to figure out how to use entities which have a delete()
method. You might think the following code should work.
$alias_es = $alias->getTranslation('es');
It returns:
InvalidArgumentException: Invalid translation language (es) specified.
I also tried $alias->getTranslationLanguages()
, which returns an array with only one element (['en'] => object(Drupal\Core\Language\Language)
).
It appears that our original $alias = \Drupal::entityTypeManager()->getStorage('path_alias')->load('1');
only has one language translation which is English.
How can I gain access to the path aliases for es and zh-hans?