1

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.

  • With that exact code you'll get nothing but a fatal error ($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? – Clive Apr 16 '14 at 18:24
0

I have rewrited my code from an example of a custom token:

$replacements = array();

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

    $node = $data['node'];

    foreach ($tokens as $name => $original) {

        switch ($name) {

            case 'team_url':

            $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 => $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]->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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.