Skip to content

Commit

Permalink
Merge pull request #35 from City-of-Helsinki/UHF-1621_tpr_custom_url_…
Browse files Browse the repository at this point in the history
…alias

UHF-1621: TPR custom URL alias
  • Loading branch information
tuutti authored Jun 16, 2021
2 parents 50400c6 + ec60551 commit 0451fe1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions helfi_tpr.tokens.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function helfi_tpr_token_info() {
$info['tokens'][$entity_type]['label'] = [
'name' => t('Label'),
];

if ($entity_type == 'tpr_unit' || $entity_type == 'tpr_service') {
$info['tokens'][$entity_type]['menu-link-parents'] = [
'name' => t('Menu link parents'),
'description' => t('Custom token for entity URL alias with menu parents.'),
];
}
}

$info['tokens']['tpr_unit']['picture'] = [
Expand Down Expand Up @@ -88,6 +95,32 @@ function helfi_tpr_tokens(
$replacements[$original] = $entity->getDescription('summary');
}
}

// Custom token for entity URL alias with menu parents.
if ($name == 'menu-link-parents') {
// Get entity menu link.
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_link = $menu_link_manager->loadLinksByRoute('entity.' . $type . '.canonical', [$type => $entity->id()]);

if (is_array($menu_link) && count($menu_link)) {
$menu_link = reset($menu_link);
if ($menu_link->getParent()) {
// Get entity menu link parents.
$parents = $menu_link_manager->getParentIds($menu_link->getParent());

// Put the cleaned parent titles in an array.
$titles = [];

foreach (array_reverse($parents) as $parent) {
$title = $menu_link_manager->createInstance($parent)->getTitle();
$titles[] = \Drupal::service('pathauto.alias_cleaner')->cleanString($title, $options);
}

// Return the titles as a string separated with /'s.
$replacements[$original] = implode('/', $titles);
}
}
}
}

return $replacements;
Expand Down

0 comments on commit 0451fe1

Please sign in to comment.