I created a content type and added a link field to it. I wanted the link title to use the node's title so I selected "Static Title" and tried both "[title]", and "[title-raw]". I then created a view in which I wanted to provide a block menu which lists only "title" that links to the link url. The actual view did not include the "Node:Title" field and therefore the note title was not available for the placeholder token in the link's title.

This may be a bug with the Token module or possibly the Views module as this may be an issue with other field types as well. But in my case it was specific to the Link CCK module which is why I'm posting this here.

In Summary:
Using the Placeholder Tokens [title] and [title-raw] are not available in views which do not include the Node:Title field.

The fix would need to come in one of mentioned modules to where it loads the node's data when a placeholder is found but is not yet loaded.

Temporary Solution:
To get the result I was looking for I had to require the Link Title on the Content Type, therefore requiring the user to enter the title twice.

Please advise if I should report this under another module.

CommentFileSizeAuthor
#8 link_view_tokens.patch847 bytesquicksketch

Comments

lee20’s picture

After further review this seems to be a "bug" with the token module (or atleast that is where the fix would be most useful).

THIS BUG SHOULD BE MOVED TO THE TOKEN PROJECT (I don't know how)

In the token module, specifically in token_node.inc within the function node_token_values is where I think the fix should be made. This function is passed an a (node) object. This function then translates the node values into the token values.

In my opinion, is the the token modules responsibility at this point to ensure the necessary values (the values that if offers for use to other modules) should be loaded. Here is the fix that I made which ensures that the node's title, created, changed, name, and type are available.

function node_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'node':
      $node = $object;

	  if (isset($node->nid)) {
		$fetch = array();
		
		if (! isset($node->title)) $fetch[]   = 'n.title';
		if (! isset($node->created)) $fetch[] = 'n.created';
		if (! isset($node->changed)) $fetch[] = 'n.changed';
	    if (! isset($node->uid)) $fetch[]     = 'u.uid';
		if (! isset($node->name)) $fetch[]    = 'u.name';
		

	    if (count($fetch)) {
			$res = db_query("SELECT ".implode(',', $fetch)." FROM {node} n LEFT JOIN {users} u ON u.uid = n.uid WHERE nid = '{$node->nid}' LIMIT 1");
			$tmp = db_fetch_object($res);
			foreach ($tmp AS $k => $v) {
				$node->$k = $v;
			}
			unset($tmp);
		}
	  }
....
quicksketch’s picture

Project: Link » Token
Version: 5.x-2.1 » 5.x-1.x-dev

When updating an issue, just change the "Project" select list to the module you want to transfer the issue to.

greggles’s picture

Project: Token » Link
Version: 5.x-1.x-dev » 5.x-2.1

Well, your query has some security holes in it. The better way is to use the "%d" replacement mechanism in db_query to prevent sql injection - see http://drupal.org/writing-secure-code for more information. I think a better solution would be if (isset($node->nid) {$node = node_load($node->nid) }.

Beyond that, token module expects that the calling module has prepared the $node object with all the data that is normally available in those objects after a "node_load". Assigning back to link but I'm not sure tht's the right place.

jscheel’s picture

Ok, it looks like link is adding it's values to the node twice. Once, it is getting added properly through cck, generating the key "field_field_name". But then, it's getting added to the node again, this time without a key. Here's the relevant part of a node_load():

    [field_description_link] => Array
        (
            [0] => Array
                (
                    [url] => http://www.google.com
                    [title] => 
                    [attributes] => N;
                )

        )

    [field_enroll_link] => Array
        (
            [0] => Array
                (
                    [url] => http://www.yahoo.com
                    [title] => 
                    [attributes] => N;
                )

        )

    [0] => Array
        (
            [url] => http://www.google.com
            [title] => 
            [attributes] => 
        )

    [1] => Array
        (
            [url] => http://www.yahoo.com
            [title] => 
            [attributes] => 
        )

I don't know enough about the cck api to say why this might be happening.

jscheel’s picture

Well, it seems like the token replacement only breaks when the link is in a view. I ran a quick test, below, in link_field_formatter(). I just removed all the keys that link adds to the node, and ran a basic token_replace test. It doesn't work:

$node = node_load($node->nid);
unset($node->{0});
unset($node->{1});
unset($node->field_description_link);
unset($node->field_enroll_link);
dsm(token_replace('[title]', 'node', $node));
jscheel’s picture

It looks like token is dumping out to prevent infinite recursion. Not sure what's going on honestly. Anyone have any ideas?

greggles’s picture

That's certainly possible. There were plenty of "token kills my site" bugs that were caused by the infinite recursion problem. So, we instituted that check. I didn't write it, just tested/committed it so I can't really speak to it. I think quicksketch might have some experience with it, though.

quicksketch’s picture

Status: Postponed (maintainer needs more info) » Fixed
StatusFileSize
new847 bytes

I found the cause of the original bug (maybe the duplicate link data, but I think that's another issue). When displaying in a view, the full node isn't available. It's just the psuedo-node handed to us by views for efficiency. Because token requires a full node, we need to do node_loads if using token replacements.

I've committed this patch. Please reopen if you still experience the same issue.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

jmlane’s picture

Patch works for me, although I patched the code by hand, as the line numbers didn't seem to add up in quicksketch's patch.

Great fix!

Jonathan M. Lane
OpenConcept Consulting Inc. http://www.openconcept.com/