Skip to content
Snippets Groups Projects
Commit 63e61f48 authored by Stef De Waele's avatar Stef De Waele Committed by Oleksandr Kuzava
Browse files

Issue #3339984: Add node alias token

parent f9f63ece
No related branches found
No related tags found
1 merge request!6Issue #3339984: Add node alias token
......@@ -140,18 +140,22 @@ function node_keep_token_token_info() {
$nodes = $keeper_helper->getProtectedNodes();
foreach ($nodes as $data) {
$machine_name = $data['machine_name'];
$label = $data['label'];
$info['tokens']['node-keep'][$machine_name . ':id'] = [
'name' => t('@label ID', ['@label' => $label]),
'description' => t('ID of @label node.', ['@label' => $label]),
$t_args = ['@label' => $data['label']];
$info['tokens']['node-keep']["{$machine_name}:alias"] = [
'name' => t('@label alias', $t_args),
'description' => t('Alias of "@label" node.', $t_args),
];
$info['tokens']['node-keep'][$machine_name . ':url'] = [
'name' => t('@label URL', ['@label' => $label]),
'description' => t('URL path of @label node.', ['@label' => $label]),
$info['tokens']['node-keep']["{$machine_name}:id"] = [
'name' => t('@label ID', $t_args),
'description' => t('ID of "@label" node.', $t_args),
];
$info['tokens']['node-keep'][$machine_name . ':uri'] = [
'name' => t('@label URI', ['@label' => $label]),
'description' => t('URI of @label node.', ['@label' => $label]),
$info['tokens']['node-keep']["{$machine_name}:url"] = [
'name' => t('@label URL', $t_args),
'description' => t('URL path of "@label" node.', $t_args),
];
$info['tokens']['node-keep']["{$machine_name}:uri"] = [
'name' => t('@label URI', $t_args),
'description' => t('URI of "@label" node.', $t_args),
];
}
......@@ -169,7 +173,14 @@ function node_keep_token_tokens($type, $tokens, array $data, array $options, Bub
$keeper_helper = \Drupal::service('node_keep_token.helper');
$machine_names = $keeper_helper->getProtectedMachineNames();
foreach ($tokens as $name => $original) {
if (in_array(str_replace(':url', '', $name), array_keys($machine_names))) {
if (in_array(str_replace(':alias', '', $name), array_keys($machine_names))) {
/** @var \Drupal\path_alias\AliasManagerInterface $manager */
$manager = \Drupal::service('path_alias.manager');
$base_name = str_replace(':alias', '', $name);
$nid = $machine_names[$base_name]['id'];
$replacements[$original] = $manager->getAliasByPath("/node/{$nid}", $options['langcode'] ?? NULL);
}
elseif (in_array(str_replace(':url', '', $name), array_keys($machine_names))) {
$base_name = str_replace(':url', '', $name);
$nid = $machine_names[$base_name]['id'];
$replacements[$original] = Url::fromRoute('entity.node.canonical', ['node' => $nid])->toString();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment