pathauto.tokens.inc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @file
  4. * Token integration for the Pathauto module.
  5. */
  6. use Drupal\Core\Render\BubbleableMetadata;
  7. /**
  8. * Implements hook_token_info().
  9. */
  10. function pathauto_token_info() {
  11. $info = array();
  12. $info['tokens']['array']['join-path'] = array(
  13. 'name' => t('Joined path'),
  14. 'description' => t('The array values each cleaned by Pathauto and then joined with the slash into a string that resembles an URL.'),
  15. );
  16. return $info;
  17. }
  18. /**
  19. * Implements hook_tokens().
  20. */
  21. function pathauto_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  22. $replacements = array();
  23. if ($type == 'array' && !empty($data['array'])) {
  24. $array = $data['array'];
  25. foreach ($tokens as $name => $original) {
  26. switch ($name) {
  27. case 'join-path':
  28. $values = array();
  29. foreach (token_element_children($array) as $key) {
  30. $value = is_array($array[$key]) ? render($array[$key]) : (string) $array[$key];
  31. $value = \Drupal::service('pathauto.alias_cleaner')->cleanString($value, $options);
  32. $values[] = $value;
  33. }
  34. $replacements[$original] = implode('/', $values);
  35. break;
  36. }
  37. }
  38. }
  39. return $replacements;
  40. }