I want create a custom token to retrieve all parents with a specific type of a menu item.
I have created a custom module with that :
function mytoken_token_info() {
$info['tokens']['node']['team_url'] = array(
'name' => t('Team URL'),
'description' => t('Returns a custom url for team and team pages'),
);
return $info;
}
function mytoken_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();
$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;
$item_title = pathauto_cleanstring($item_entity[$item_id]->title);
if ($item_type == 'type1' or $item_type =='type2') {
$replacements[$tokens['team_url'] = $item_title;
break;
}
}
}
return $replacements;
}
With this code I get only one parent. The foreach seems not functionnal.
if I use $replacements[$original] = $item_title I get no parent.
Can you help me ?
Thanks.
P.S. If i use a similar code in my node template I get all parent items. It's ok in my node.
$replacements[$tokens['team_url'] = $item_title;
is not valid PHP). Could you edit the code you're actually using into the question just to be sure we're looking for problems in the right thing?