I'm trying to add "token support" to the cck "computed field" component.

For the moment, I've added the following HelloWorld code to the computed_field.module:
(as described in the API.txt from the token module)

/**
* Implementation of hook_token_list()
*/
function computed_field_token_list($type = 'all') {
if ($type == 'node' || $type == 'all') {
$tokens['node']['testtoken'] = 'list';
return $tokens;
}
}
/**
* Implementation of hook_token_values()
*/
function computed_field_token_values($type, $object = NULL) {
if ($type == 'node') {
$tokens['testtoken'] = 'value';
return $tokens;
}
}

But I'm getting the following error:
* warning: Invalid argument supplied for foreach() in /var/www/virtual/test.com/htdocs/modules/token/token_cck.inc on line 41.
* warning: Invalid argument supplied for foreach() in /var/www/virtual/test.com/htdocs/modules/token/token_cck.inc on line 41.
The token part works, as I can view it in my workflow, yet I'm puzzled why I'm getting these warnings. As I can see it's a way of walking thru the cck fields, and trying to add token support to them. But why didn't it work on the computed field in the first place?

Comments

jp.stacey’s picture

When those if statements aren't true, your functions won't return any definite value. Could token_cck be being confused by that? Maybe always return something, even if it's just an empty array:

 function computed_field_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['testtoken'] = 'list';
  }
  return $tokens;
}

(ps: if you surround the code you post here with &lt?php ... ?> then it gets syntax-highlighted!)

yched’s picture

Project: Content Construction Kit (CCK) » Token
Component: CCK in core » Miscellaneous

This support request is about token hooks, so it should be filed for Token.module

And jp.stacey is probably right, you should always return an array.

kvaes’s picture

thanks for the response (& for the ps!), will check into that
(didn't know where to post it, as it's an integration between different modules)

kvaes’s picture


/**
* Implementation of hook_token_list()
*/
function computed_field_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['testtoken'] = 'list';
  }
  print_r($tokens);
  return $tokens;
}
/**
* Implementation of hook_token_values()
*/
function computed_field_token_values($type, $object = NULL) {
  if ($type == 'node') {
     $tokens['testtoken'] = 'value';
  }
  print_r($tokens);
  return $tokens;
}

This prints the following:
Array ( [node] => Array ( [testtoken] => list ) ) Array ( [node] => Array ( [testtoken] => list ) )

And gives the following errors:

    * warning: Invalid argument supplied for foreach() in /var/www/virtual/test.com/htdocs/modules/token/token_cck.inc on line 42.
    * warning: Invalid argument supplied for foreach() in /var/www/virtual/test.com/htdocs/modules/token/token_cck.inc on line 42.
jp.stacey’s picture

@kvaes,

Sorry for the delay. I'm not familiar with the module, but could looking token_cck.module, line 40, your function hook_token_list is being called with type 'field', not 'node' or 'all'. Then there are two foreach loops over the returned value on line 42, but I think your functions will return a NULL.

Try starting each function with $tokens = array();, so that something iterable will always be returned. Do a dump of $sub_list in the token_cck.module file itself.

clivesj’s picture

@kvaes,
I also need Token support for my CCK computed fields.
Did you finalize the integration?
Could you post the code here?
Thank you.

clivesj’s picture

I PM'ed KVAES but other that a personal solution he doesn't have the token integration available.
Anyone who has a solution for token support of the computed field

karens’s picture

Status: Active » Fixed

See http://drupal.org/node/243775 for a solution (which needs to go into the Computed Field module).

Anonymous’s picture

Status: Fixed » Closed (fixed)

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