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

kumkum29’s picture

Issue summary: View changes
Dave Reid’s picture

Status: Active » Fixed

Probably 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.

kumkum29’s picture

Hello,

in my token_info I have:

function mymodule_token_info() { 
	
	$info['tokens']['url']['team_url'] = array(
		'name' => t('Team URL'),
		'description' => t('My description'),
	);

	return $info;
	
}
kumkum29’s picture

I have change this line:

$info['tokens']['url']['team_url'] = array(
by
$info['tokens']['node']['team_url'] = array(

But I get warnings errors and no tokens:

    Warning : Illegal offset type in isset or empty dans pathauto_cleanstring() (ligne 180 dans /srv/data/web/vhosts/www.mysite.com/htdocs/sites/all/modules/pathauto/pathauto.inc).
    Warning : html_entity_decode() expects parameter 1 to be string, array given dans decode_entities() (ligne 459 dans /srv/data/web/vhosts/www.mysite.com/htdocs/includes/unicode.inc).
    Warning : Illegal offset type dans pathauto_cleanstring() (ligne 223 dans /srv/data/web/vhosts/www.mysite.com/htdocs/sites/all/modules/pathauto/pathauto.inc).
kumkum29’s picture

Hello,

I get a token with this code:

	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 = $item_entity[$item_id]->title;
			//if ($item_type == 'team' or $item_type =='page_team') {
				$replacements[$tokens['team_url']] = pathauto_cleanstring($item_title);
    			break;
			//}	
		}
				
	}

But i retrieves only the first parent. The foreach is not working.

Thanks.

kumkum29’s picture

Status: Fixed » Active
kumkum29’s picture

Hello,

i have found the solution after several hours:

function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {

	$replacements = array();
 
	if ($type == 'node' && !empty($data['node'])) {
		
		$node = $data['node'];
			
		foreach ($tokens as $name => $original) {
			
      		switch ($name) {
				
        		case 'mytoken':
				
				$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;

} 

Maybe the code is not clean but it's functionnal. Do you have any ideas to simplify it?

Thanks.

kumkum29’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.