Token filter wasn't able to replace CCK field values so I created the attached patch that makes it possible. Maybe a nice addition to add to the next release?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markwittens’s picture

Maybe convenient for other users to provide a small manual:

Usage:
[token node [[cck-field-name]] ]

Example:
To put the contents of the field "field_customfield" in the text you can use the following token: [token node field_customfield-raw]

pvhee’s picture

Thanks for the patch.

I saw the code uses the global variable '$_customfilter_code_vars;'. From what I could find, it is a variable of the module Custom Filter (issue http://drupal.org/node/259819 as you mentioned in the patch). Does this mean you require dependency of this module? Can you explain a bit more about how your patch is supposed to work in absence of that module?

markwittens’s picture

There's no dependency on the Custom filter module, the only link is the name of the variable and that actually should be renamed to something else to prevent this kind of confusion in the future. My bad, I should have reviewed my code after copy-pasting :-)

WhenInRome’s picture

Thanks!

mpisano’s picture

Priority: Normal » Minor

Thanks its works great!!!

newToo’s picture

I'm trying to create a cck text field that allows me to use tokens to insert the node's url... would this module be used for that? I don't seem to have any options to add tokens to the text field.

pvhee’s picture

Status: Active » Needs work

@markwittens: Could you roll out a new patch of #0 with suggestions from #3?

markwittens’s picture

FileSize
1.5 KB

Here's the patch with the suggestion from #3 in it.

Hobbes-2’s picture

Hum, could you tell me when this patch will be in the dev version ???

thanks!

ckwon’s picture

hello,

thanks for this nice patch. i searched this function for hours ;).

There is only a little problem. Replacement does not work if you preview a new node, only if you save it.

Grayside’s picture

FileSize
1.88 KB

#10: That will be an ongoing problem with this module I expect. The problem is that Token Filter does not compute it's values from the node form, but rather by loading the current values from the database. Until you've saved the node, there are no current values.

Attached patch makes the code more clear about refusing NULL node objects, and makes sure the token syntax is left in place for preview.

imclean’s picture

I'm looking at embedding CCK fields too. What's the advantage of this method over Linodef? http://drupal.org/project/linodef

Murz’s picture

Linodef do the additional node_load, and in the key you must manually set the nid, but in this solution node loaded only once and cached in memory.

szantog’s picture

Status: Needs work » Reviewed & tested by the community

Attached patch in #11 works perfect.

drecute’s picture

patch #11 does not work for me. It rather removes the [token op] string from the token and falls back to the default i.e global and user

jvieille’s picture

#11 does not work.
In my case, nodeapi reports the OG group nid, not the node nid.
I don't use the hook function token_filter_nodeapi, I just changed this function:

/**
 * Helper function for preg_replace_callback to generate the html for each token filter
 */
function token_filter_replacetoken($matches) {  
  global $user;
  $type  = $matches[1];
  $token = $matches[2];
  switch ($type) {
    case 'user' :
      $object = $user;
      break;
+  case 'node':
+    $type = 'node';
+    $object = node_load(arg(1));
+    if (empty($object)) {
+      return;
+    }
+    break;
    case 'global':
    default:
      $object = NULL;
      break;
  } 
  
  // add [ ] to the token so str_replace correctly replaces token values
  // if not, a token like 'custom_text' won't be properly replaced if another token like 'custom' exists  
  $token = '['.$token.']';
  $output = token_replace($token, $type, $object, '[', ']');  
  return $output;
darvanen’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Closed (outdated)

Drupal 6 version is no longer supported. Please reopen this if you find it is relevant to Drupal 7 or 8.