Score:2

How to check if path alias exists

in flag

In Drupal 8, my custom module includes the code

if (\Drupal::service('path.alias_storage')->aliasExists($path, 'en')) {
    return true;
}

What should this look like in Drupal 9.2? Seems so simple, but I can't figure it out after reading every post I can find.

Score:6
cn flag

This service was deprecated in Drupal 8 and removed in Drupal 9.

Path aliases were converted to an entity. You can use the entity storage and additionally the path_alias.repository service for some common case-insensitive lookups:

$path_alias_repository = \Drupal::service('path_alias.repository');
if ($path_alias_repository->lookupByAlias($alias, 'en')) {
  return TRUE;
}

Interesting is that the service AliasRepository provides a pathHasMatchingAlias() method but not one for the other direction, so you have to check whether the lookup method above returns a result.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.