Custom Token + foreach not functionnal 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' &amp;&amp; !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.
I have rewrited my code from an example of a custom token:

$replacements = array();

if ($type == 'node' &amp;&amp; !empty($data['node'])) {

    $node = $data['node'];

    foreach ($tokens as $name =&gt; $original) {

        switch ($name) {

            case 'team_url':

            $path = 'node/'.$node-&gt;nid;

            $mlid = db_select('menu_links' , 'ml')
            -&gt;condition('ml.link_path' , $path)
            -&gt;fields('ml' , array('mlid'))
            -&gt;execute()
            -&gt;fetchField();

            $menuParents = token_menu_link_load_all_parents($mlid);

            foreach ($menuParents as $itemParent_mlid =&gt; $itemParent_title) {
                $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]-&gt;type;
                if ($item_type == 'team' or $item_type == 'page_team') {
                    $itemParent[]= pathauto_cleanstring($itemParent_title);
                }
            }

            $itemsParents = implode("/", $itemParent);
            $replacements[$original] = $itemsParents;

        }
    }

}

return $replacements;


It's functionnal. Perhaps we can simplify this code and get a cleanly syntax.