5

When writing my custom function I need to manually append node name to a taxonomy term link. To get the taxonomy term part done I use:

  $link_uri = taxonomy_term_uri($term);
  $path_alias = drupal_get_path_alias($link_uri['path']);

This works ok, now - I want to append node name ("/" . $node_name), but instead of raw string, I want the Pathauto transliterated output, as set in the URL alias configuration, for that node context.

What function should I use to mimic that output? I looked at hook_pathauto_alias_alter(&$alias, array $context) but I'm not sure that's what I need.

2
  • By this, you want to have /taxonomy/term/1 and /taxonomy/term/1/nodename to point to the same page? I'm not sure what you really want as the first url points to the term while the second one includes a (which?) node name.
    – Paul
    Jan 16, 2012 at 0:07
  • Sorry to be unclear: I know these are two different links. The goal of my code is to have /taxonomy/term/1/nodename. However I need the NOdENamE string (if you catch my point in the spelling) to pass through transliteration, so to have the result given by Pathauto. What would be the function for the transliteration then? AS per "which node?" - the code will be used on a node display, so the answer is THE node. The code I'm trying to write is a custom taxonomy link formatter. Jan 16, 2012 at 1:03

2 Answers 2

4

If you want to filter your string the way pathauto does it, you can use pathauto_cleanstring():

module_load_include('inc', 'pathauto', 'pathauto');
$node_title .= pathauto_cleanstring($node->title);
1
  • I tried using pathauto_cleanstring() in hook_menu(), which caused an error stating that the function didn't exist (even though the site in question has that module installed). As a work-around, the code used a regex to replace anything non-alphanumeric (except spaces) with a blank string, then replaced spaces with hyphens. It's perhaps not the best solution, but works well enough. Oct 11, 2014 at 11:02
0

On Drupal 8/9, you can use the Pathauto Alias Cleaner service :

/** @var Drupal\pathauto\AliasCleaner $cleaner */
$cleaner = \Drupal::service('pathauto.alias_cleaner');
$cleaner->cleanString($node->getTitle());
2
  • oh, so Drupal is already at 9, nice, congrats! Mar 2 at 18:13
  • @Artur, if you have any constructive suggestions, do not hesitate. Mar 3 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.