So I was using CCK link field to set a link for each user in their profile.

I was using the placeholder token {author-uid] and setting the default value to: My profile usr/[author-uid]

So if I am user 26. if someone where to click on “View my profile” they would be taken to user/26

Another example is
Add me as your friend which would like to
/add/[author-uid]?destination=user/[author-uid]

When I view the nodeprofile, it is fine.
usr/26 for View my profile
/add/26?destination=user/26 when someone else views my profile

However when I have this field in a view using the view module, I instead get “usr/” with no number. When I print out the value for the link I get “user/[author-uid]” It looks like [author-uid] is not interpreted at all.

Is there another way to create a link to an internal page that is created for each user like usr/uid. So in a membership directory, each user will have this link so others can click on it and see their profiles. I am using views for the membership directory

I am using node profile and views. The latest for both on 5.6. It wasn't working when I was using 5.5 and one rev back of these modules either.

Thanks,
Chris

CommentFileSizeAuthor
#10 full_object_fix.patch728 bytespopulist

Comments

socialnicheguru’s picture

When I use a token for a default value for the url, the substitution for the value is not made in the view.

socialnicheguru’s picture

Project: CCK Taxonomy Fields » Token

I think this is a problem with the token substitution for a cck link as default in a View

vivianspencer’s picture

I'm getting exactly the same problem

NukeHavoc’s picture

I've run into the same sort of problem with Link CCK and tokens in both Blocks and Views. In my case, I've got a simple "Link" content type that looks like this:

Title: My Great Site
Web Address Title: [title]
Web Address Link: http://www.mygreatsite.com

If I'm looking at the node, then this works; the [title] token is replaced by the node title. However, if I'm looking at a View or a Block, "Web Address" appears blank. The only exception is when I'm on the node for that particular link; in that case the title appears properly in the Block.

I'm assuming that it's not supposed to work this way, and that it's a problem with CCK's implementation of tokens.

socialnicheguru’s picture

#4 description is exactly right.
But this is a problem with cck link and token. It has driven me CRAZY. anyone know how to fix it.

So it was suggested that I not use CCK link for this, but just do it on the theme level in the view.

http://drupal.org/node/250007

Chris

greggles’s picture

Status: Active » Postponed (maintainer needs more info)

Could someone start with:

1. Install a bare Drupal core site
2. ...

And step me through everything to get to:

I expect to see Foo and instead I see Bar!

As it stands, I have no idea exactly which contributed modules are involved or what is/isn't happening.

NukeHavoc’s picture

Here you go.

Summary
I think there's a problem with the token_replace function in token.module not having the full node information it needs to do the token replacement. I don't know, however, if this is an issue with CCK Link not passing it the right information in the form of the node object, or a problem with token assuming it's getting one kind of node when in reality it's getting something else. Full details, and a workaround hack, follows.

Recreating the problem

Install the modules

I recreated this problem using a clean install with the following modules:

* Install Drupal 5.7
* Install Views 5.x-1.6
* Install CCK cck-5.x-1.6-1
* Install CCK: link-5.x-2.1
* Install Token: token-5.x-1.10

I also installed Devel 5x-dev after the fact to analyize what was happening.

Create the "Link" content type

* Administer > Content Types > Add content type
* Use all the standard options for the content type (title field: Title, body field: Body, etc.)
* Edit the new content-type.
* In the "Create new field" group, specify the name "web_address" as the name
* Choose "Link: Text Fields for Title and URL" from the as the field type.
* Click the "Create field" button.
* Go to "Manage fields" tab.
* Click configure on web_address.
* Set "Link Title" to "Static Title". In the field for "Static Title", specify the token "[title]".
* Click to "Display fields"; use the defaults: Label/Above; Teaser/Default, as link with title, Full/Default, as link with title
* Add three new nodes using that content-type.

Create the View

* Create a view called "Links View"
* Page:
** Provide page view [checked]
** URL: links
** Provide Page View
** View type: Table view
** Nodes per page: 50
* Fields
** Add a field: Link (web_address)
*** Label: Title
*** Handleer: Group multiple values
*** Option: Default as link with title
*** Sortable (none)
*** Default Sort (none)
** Add a field: Node Body
*** Label: Teaser
*** Handler: Teaser
* Create a Filter "Node:Type"
** Specify operate as "is one of" with a value of "Link".

For other Views, I also do an argument based on taxonomy (either numeric or text) but it isn't necessary for this experiment.

Load the View

Now load the view. What you should see is:

* Title Column: Displays nothing. It *should* be showing the node title, passed via the token.
* Teaser: Displays the "web_address" label, but without the web_address title. Also shows teaser from node. What it should show is the label with the title of the node, passed via token, as well as the teaser.

If I go back to the Link Content Type and set "Title" to "Optional", and then manually enter a "web address title", then that title will show up correctly in this view. It only breaks when we tokenize things.

Looking at this via Devel 5.x-dev's "Dev Queries" tab, i can see it's using this query to construct the view:

SELECT node.nid, node_data_field_web_address.field_web_address_url AS node_data_field_web_address_field_web_address_url, node_data_field_web_address.field_web_address_title AS node_data_field_web_address_field_web_address_title, node_data_field_web_address.field_web_address_attributes AS node_data_field_web_address_field_web_address_attributes FROM {node} node LEFT JOIN {content_type_link} node_data_field_web_address ON node.vid = node_data_field_web_address.vid WHERE (node.type IN ('link'))

I can also see the objects its returning:

Array
(
    [0] => stdClass Object
        (
            [nid] => 1
            [node_data_field_web_address_field_web_address_url] => http://modelrocketmission.blogspot.com/
            [node_data_field_web_address_field_web_address_title] => 
            [node_data_field_web_address_field_web_address_attributes] => N;
        )

    [1] => stdClass Object
        (
            [nid] => 2
            [node_data_field_web_address_field_web_address_url] => http://www.hiddenfrontier.com
            [node_data_field_web_address_field_web_address_title] => 
            [node_data_field_web_address_field_web_address_attributes] => N;
        )

    [2] => stdClass Object
        (
            [nid] => 3
            [node_data_field_web_address_field_web_address_url] => http://www.system47.com
            [node_data_field_web_address_field_web_address_title] => 
            [node_data_field_web_address_field_web_address_attributes] => N;
        )

)

Code Delving

In trying to debug this, I started with the CCK Link and the link.module. At line 619, we have this logic:

// Replace tokens
if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
  $title = token_replace($title, 'node', $node);
  }
  $output = l($title, $url, $attributes, $query, $fragment);
}

I confirmed that this code is triggered, and it does call token_replace, which is a function wihtin token_module. The problem is that this simply doesn't work; while the arguments for token_replace appear to be correct, the function returns a blank value. This causes $title to be blank. $title is what's outputed to the screen, so that's why we lose the title.

As for why it breaks, I looked at token_replace at line 145 in the token.module:

function token_replace($original, $type = 'global', $object = NULL, $leading = '[', $trailing = ']', $options = array()) {
  
  $full = token_get_values($type, $object, FALSE, $options);
  return _token_replace_tokens($original, $full->tokens, $full->values,   $leading, $trailing);
}

token_get_values is supposed to go out and get the list of replaceable tokens based on the object, which in this case is the node that was passed to it as part of token replace.

If I use Devel's dvr function on $full, I get the list of tokens and values for each node (I'm limiting it to the first node for brevity's sake:

object(stdClass)#4 (2) {
  ["tokens"]=>
  array(26) {
    [0]=>
    string(9) "user-name"
    [1]=>
    string(7) "user-id"
    [2]=>
    string(9) "user-mail"
    [3]=>
    string(8) "site-url"
    [4]=>
    string(9) "site-name"
    [5]=>
    string(11) "site-slogan"
    [6]=>
    string(9) "site-mail"
    [7]=>
    string(9) "site-date"
    [8]=>
    string(3) "nid"
    [9]=>
    string(4) "type"
    [10]=>
    string(9) "type-name"
    [11]=>
    string(5) "title"
    [12]=>
    string(9) "title-raw"
    [13]=>
    string(10) "author-uid"
    [14]=>
    string(11) "author-name"
    [15]=>
    string(15) "author-name-raw"
    [16]=>
    string(8) "menupath"
    [17]=>
    string(12) "menupath-raw"
    [18]=>
    string(4) "menu"
    [19]=>
    string(8) "menu-raw"
    [20]=>
    string(4) "term"
    [21]=>
    string(8) "term-raw"
    [22]=>
    string(7) "term-id"
    [23]=>
    string(5) "vocab"
    [24]=>
    string(9) "vocab-raw"
    [25]=>
    string(8) "vocab-id"
  }
  ["values"]=>
  array(26) {
    [0]=>
    string(16) "Kenneth Newquist"
    [1]=>
    string(1) "2"
    [2]=>
    string(18) "My Email Address"
    [3]=>
    string(19) "http://www.example.com"
    [4]=>
    string(6) "My Site Name"
    [5]=>
    string(0) ""
    [6]=>
    string(0) ""
    [7]=>
    string(23) "Sun, 04/27/2008 - 18:28"
    [8]=>
    string(1) "1"
    [9]=>
    NULL
    [10]=>
    NULL
    [11]=>
    string(0) ""
    [12]=>
    NULL
    [13]=>
    NULL
    [14]=>
    string(0) ""
    [15]=>
    NULL
    [16]=>
    string(0) ""
    [17]=>
    string(0) ""
    [18]=>
    string(0) ""
    [19]=>
    string(0) ""
    [20]=>
    string(0) ""
    [21]=>
    string(0) ""
    [22]=>
    string(0) ""
    [23]=>
    string(0) ""
    [24]=>
    string(0) ""
    [25]=>
    string(0) ""
  }
}

As you can see, Title (Token #11) comes up blank. Interestingly, when I look at the node view for this link, the token *does* work. When I look do a dvr($full) when looking at the node I see that, sure enough, the title value (Token #11) for the token has an entry: [11]=> string(21) "Model Rocket Mission".

So here's what I think is happening.

* When looking at a View or a Block, the node information being passed by the line "$title = token_replace($title, 'node', $node);" is not what "token_replace" is expecting. See my break out of the View node objects for a list of what's included in the View-generated node.
* token_replace wants the full node, NOT just the abbrevated node that Views assembles for its purposes.

I tried two things to fix this. First, I tried passing the full node to token_replace, by doing this:

$node = node_load($node->nid);
$title = token_replace($title, 'node', $node);

That didn't work, so clearly my theory is a little wonky. However, when I went into the token module, and tried the same trick, *it did work*:

$object = node_load($object->nid);

function token_replace($original, $type = 'global', $object = NULL, $leading = '[', $trailing = ']', $options = array()) {
  // load the full node into $object, based on the NID of the original object.
  $tempobject = node_load($object->nid);
  $full = token_get_values($type, $tempobject, FALSE, $options);
  return _token_replace_tokens($original, $full->tokens, $full->values,   $leading, $trailing);
}

This caused the web_address title token to be replaced correctly in every instance, regardless of whether I was looking at a Block, a View, or a Node.

I'm not fluent enough in CCK, Tokens and Views to be able to say which one's malfunctioning -- is Token wrong for wanting a full node? Is Link doing something wrong when passing the argument? -- but I hope this clarifies exactly where things are falling down.

ambereyes’s picture

I had a similar problem. I tried using a static title field with [title-raw], the view was a complete blank. I then modified my content type and added the token as a default for a required field, and now the urls list but not the title unless I typed it in - no token replacement.

I then tried the suggestion from #7, and patched the token_replace function within the token module - it works! My view now lists the title as a link.

Can this be rolled into a patch? Or is there other issues that need resolved?

ahojchris’s picture

Seeing the same issue.

Using CCK + imagefield + link + views. The Content type I've specified is fairly simple, as is the view: one image field and one link field for the content type, and a basic "show all content of this content type" view.

In the link attributes under Link Title, I'm selecting a static title with a token for the "Fully formatted HTML image tag" (i.e. [field_FIELDNAME-view] ) ...

Token is on and works with the non-cck tokens (i.e. [nid]) so obviously some of the token functionality works. Just seems to ignore tokens for CCK fields and passes them straight through as hard-coded text.

I suppose a work-around is to define some views functions in PHPTemplate to create links... Does anyone have an elegant workaround to share while this (hopefully) gets addressed?

populist’s picture

Category: support » bug
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new728 bytes

I had the same problem and the node_load bit from #7 worked wonderfully. I rolled a patch.

greggles’s picture

Status: Needs review » Needs work

I believe this will break token under other circumstances. Whatever is calling token_replace should make sure that the $object is properly populated prior to calling token_replace.

scooper@drupal.org’s picture

Yes, it did break for me in other circumstances:

I had a similar problem - my CCK link field was blank when displayed in a context sensitive view embedded in another CCK node. The revised code from #7/#10 fixed it perfectly.

However, this patch broke my use of tokens to generate node titles using module Automatic Nodetitles, which works fine with the original token_replace function.

And, it causes a SQL error when using the node_import module to import data into a CCK link field.

So a check needs to be added to the patch:

if (node is loaded)
$tempobject=$object;
else
$tempobject = node_load($object->nid);

Does anyone know how to code this check before calling node_load()?

I agree that a better solution is to properly populate $object prior to calling token_replace, but in the meanwhile...

greggles’s picture

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

Yes, the proper way to do this check is just not to do it :) The calling function should fully load the node prior to passing it to token.

So, this needs to be fixed in link, then - reassigning.

ambereyes’s picture

Okay, in a act of total frustration ... I did the following starting at line 618 of the link.module.

    // Replace tokens
    if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
//      $title = token_replace($title, 'node', $node);
      $title = $node->node_title;  //hard code the fricking title AARRGGHHH!
    }
    $output = l($title, $url, $attributes, $query, $fragment);
  }

It ain't elegant, but it works. If someone has a better solution, I am all ears.

Katrina

NukeHavoc’s picture

I found that the hack in #14 only worked if the node's title field was included as part of the View; if I didn't include the node title in my view, then CCK's link title tokens displayed nothing.

greggles’s picture

Title: CCK link fields don't seem to work in a View » Time for new release? - was CCK link fields don't seem to work in a View
Category: bug » task
Status: Needs work » Active

I believe this is a duplicate of http://drupal.org/node/206077

The code in that issue has not been released yet, but you could test out the latest 5.x code in order to get this fixed.

Specifically, the commit which fixed this was http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/link/link.m...

@quicksketch - can you make a new 5.x-2.2 Link release so that folks can use that? If not, I believe that the 5.x-1.x-dev tarball is the same code, right?

quicksketch’s picture

Ah, thanks greggles. I didn't realize that change wasn't in the 2.1 version. I'll roll out a new version shortly.

NukeHavoc’s picture

Title: Time for new release? - was CCK link fields don't seem to work in a View » CCK link fields don't seem to work in a View
Category: task » bug
Status: Active » Needs work

After spending too long trying to debug this, I finally realized I was having a forest for trees moment. What I'm trying to do is use the node 'title' in place of the url field 'title' by passing a 'title' token. It's the 'title' bit that threw me.

We are absolutely right that token_replace expects to be passed a full node ... but my earlier attempts tried to pass it the full node for the *title* substitution NOT the *url* substitution. That'll fix the problem with broken tokens in the title, but hey, that's not what I was trying to do. :)

So in order to get this to work, we have to fix link.module so that it always loads the full node whenever it tries to do a token substituion for "title" AND "url".

Here's the fixes I propose for link.module:

Change the "url" token bit on line 581 from:

// Replace URL tokens
  if (module_exists('token') && $field['enable_tokens']) {
    $item['url'] = token_replace($item['url'], 'node', $node);
  }

to:

// Replace URL tokens
  if (module_exists('token') && $field['enable_tokens']) {
    $tempobject = node_load($node->nid);
    $item['url'] = token_replace($item['url'], 'node', $tempobject);
  }

And then do the same for "title" at line 618 from:

    // Replace tokens
    if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
      $title = token_replace($title, 'node', $node);
    }

to:

    if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
	 $tempobject = node_load($node->nid);
     $title = token_replace($title, 'node', $tempobject);
    }

This works on my development system. It doesn't muck with the token.module, so it shouldn't have the ill effects that folks saw with my earlier hack, and it passes the token module exactly what it's expecting (which is the full node).

Please try it out and let me know if you run into any snafus.

quicksketch’s picture

NukeHavoc, as greggles suggests, try out the dev version of link.module, where a nearly identical solution was posted some time ago.

NukeHavoc’s picture

heh. If only I'd found that earlier report a few weeks ago. :) That patch worked like a charm.

quicksketch’s picture

Status: Needs work » Fixed

Great. I'll marked this issue "fixed", and I'll get out a new version of link shortly.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

charlie-s’s picture

I see this is over a year old, but I'm having exactly this problem.

Using Drupal 6.15, this is a week-old installation with nothing hacked. I am actually able to see the Link output in a Page View but not in any Block View. Using all current modules (Link 6.x-2.8 and not dev).