forked from larowlan/pathauto
-
Notifications
You must be signed in to change notification settings - Fork 34
/
pathauto.tokens.inc
51 lines (42 loc) · 1.3 KB
/
pathauto.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* @file
* Token integration for the Pathauto module.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function pathauto_token_info() {
$info = array();
$info['tokens']['array']['join-path'] = array(
'name' => t('Joined path'),
'description' => t('The array values each cleaned by Pathauto and then joined with the slash into a string that resembles an URL.'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function pathauto_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
if ($type == 'array' && !empty($data['array'])) {
$array = $data['array'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'join-path':
module_load_include('inc', 'pathauto');
$values = array();
foreach (token_element_children($array) as $key) {
$value = is_array($array[$key]) ? render($array[$key]) : (string) $array[$key];
$value = \Drupal::service('pathauto.alias_cleaner')->cleanString($value, $options);
$values[] = $value;
}
$replacements[$original] = implode('/', $values);
break;
}
}
}
return $replacements;
}