Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
I want created a custom token to get all parents items with a specific type.
If I use my code in my node template it's ok and functionnal. But if I insert this code in my module I retrieve no tokens:
function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
$path = 'node/'.$node->nid;
$mlid = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $path)
->fields('ml' , array('mlid'))
->execute()
->fetchField();
module_load_include('inc', 'pathauto', 'pathauto');
$menuParents = token_menu_link_load_all_parents($mlid);
foreach ($menuParents as $itemParent_mlid => $original) {
$item = menu_link_load($itemParent_mlid);
$item_path = $item['link_path'];
$item_path_args = explode('/', $item_path);
$item_id = $item_path_args[1];
$item_entity = entity_load('node', array($item_id));
$item_type = $item_entity[$item_id]->type;
if ($item_type == 'type1' or $item_type =='type2') {
$replacements[$original] = pathauto_cleanstring($original);
break;
}
}
}
return $replacements;
}
I made a mistake?
Thanks for your help.
Comments
Comment #1
kumkum29 CreditAttribution: kumkum29 commentedComment #2
Dave ReidProbably because you did not also create a corresponding hook_token_info(). You will also want to review your use of the $original variable. Its value should be something like [node:some-token-name] in order to perform replacement.
Comment #3
kumkum29 CreditAttribution: kumkum29 commentedHello,
in my token_info I have:
Comment #4
kumkum29 CreditAttribution: kumkum29 commentedI have change this line:
But I get warnings errors and no tokens:
Comment #5
kumkum29 CreditAttribution: kumkum29 commentedHello,
I get a token with this code:
But i retrieves only the first parent. The foreach is not working.
Thanks.
Comment #6
kumkum29 CreditAttribution: kumkum29 commentedComment #7
kumkum29 CreditAttribution: kumkum29 commentedHello,
i have found the solution after several hours:
Maybe the code is not clean but it's functionnal. Do you have any ideas to simplify it?
Thanks.
Comment #8
kumkum29 CreditAttribution: kumkum29 commented