I'm aware of this question Find all aliases for a given node - but this appears to be for 7, and I'm looking for a solution for 8.

I'm also aware of the AliasStorage and AliasManager classes, which respectively provide the lookupPathAlias and getAliasByPath methods. But these methods only return one path alias per path, even though I have multiple defined.

What is the best way return all aliases for a path?


The answer is the same as in Drupal 7, because there is no API to return multiple aliases for a single source path, so your only option, if you really need that (I guess there is a reason that you are not using redirects), is a custom query, which is the same as in 7.x, with the only difference that it has a leading /.

So, copied from the other issue:

$aliases = \Drupal::database()->query('
  SELECT alias
  FROM {url_alias}
  WHERE source = :source
', array(':source' => '/node/' . $nid))->fetchAll();
  • Hmm, interesting this might be the best approach for this. I have an edge case, where I'm not looking to redirect these multiple aliases, but I appreciate that's quite niche. – user34185 Apr 4 '17 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy