This references issue http://drupal.org/node/1065666 titled: "Link 'Title' as Token?"

I think it would be awesome to integrate Tokens (possible using Entities api) so that it could be possible to use [node:link-title] for URL Aliases, Node Auto Titles, etc...

[node:link-path] is the URI
[node:link-title] is the title for the URI.

CommentFileSizeAuthor
#4 link_tokens-1111034-4.patch2.5 KBdeciphered

Comments

tonycpsu’s picture

Subscribing

esculcar’s picture

Subscribing

jastraat’s picture

I believe all that needs to be done is replace link_token_list() and link_token_values() with the following:

function link_token_info() {
  $info['types']['link-field-field'] = array(
    'name' => t('Link field'),
    'description' => t('Tokens related to a link field instance.'),
    'needs-data' => 'link-field-field',
    'field' => TRUE,
    'module' => 'token',
  );

  $info['types']['link-field-field-value'] = array(
    'name' => t('Link field values'),
    'description' => t('Tokens related to a link field values.'),
    'needs-data' => 'link-field-field-value',
    'field-value' => TRUE,
    'module' => 'token',
   );
   $info['tokens']['link-field-field-value']['url'] = array(
    'name' => t('URL'),
    'description' => t('The link field url.'),
  );
  $info['tokens']['link-field-field-value']['title'] = array(
    'name' => t('Title'),
    'description' => t('The link field title.'),      
  );
  return $info;
}

function link_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  if ($type == 'link-field-field-value' && !empty($data['item'])) {
    $item = $data['item'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'url':
          $replacements[$original] = url($item['url']);
          break;
        case 'title':
          $replacements[$original] = $sanitize ? check_plain($item['title']) : $item['title'];
          break;
      }
    }
  }

  return $replacements;
}

(Note: this assumes that the patch from http://drupal.org/node/691078 gets into the token module)

deciphered’s picture

Status: Active » Reviewed & tested by the community
StatusFileSize
new2.5 KB

Patch attached with my version of Token support, which is basically the same but with an extra 'view' token for a simple formatted link.

Everett Zufelt’s picture

I didn't read through the patch, but I did apply it.

I expected to be able to use:

[node:field-name:title]
[node:field-name:url]

or at the least
[node:field-name:value]

None of these supplied me with a valid token.

deciphered’s picture

Everett,

They worked fine for me, have you applied the Token module Field Tokens issue patch (http://drupal.org/node/691078?page=1#comment-4594082)?

Cheers,
Deciphered.

dalin’s picture

Status: Reviewed & tested by the community » Needs work

I too do not see any tokens available for link fields. I'm using Entity API module rather than the core patch. I do see tokens for other field types.

deciphered’s picture

Dalin,

I don't know about any core patch, but you do require the Token module and Token module patch.

Maybe try that.

Everett Zufelt’s picture

@dalin

I am using token 7.x-1.x with the field tokens patch applied. I can access tokens for some of the other fields on my node, but not the link field.

If I go to node/1/devel/token the field is listed as

[node:field-name-of-link-field

But, I don't seem to be able to access anything from that token with :value], :title] or :url]

jastraat’s picture

Hey Everett,
While the dev module doesn't display the expanded set of tokens, they do exist. If you are using auto nodetitle or pathautho, you should see the expanded set in those places.

Everett Zufelt’s picture

Where do I find the token list with the auto_nodetitle module? When I edit the content-type and the auto nodetitle tab is active I don't see anything that would indicate a list of tokens.

note: I am using a screen-reader, so it is possible that this feature of auto nodetitle is not accessible, I can file a bug against that module if this is the case.

Everett Zufelt’s picture

Crazy, I just looked again and they are there. Thanks for the heads up.

jastraat’s picture

no problem :)

jastraat’s picture

Status: Needs work » Needs review
dalin’s picture

I get the tokens now with that patch to Token module. Odd that I didn't need that to use tokens for other field types.

Crazy, I just looked again and they are there. Thanks for the heads up.

I think Token module isn't doing enough cache clearing.

This patch works for me, though I didn't do a technical review so I won't mark it RTBC.

dalin’s picture

Status: Needs review » Reviewed & tested by the community

Oh this was already RTBC, so I'll return it to that.

dqd’s picture

Status: Reviewed & tested by the community » Closed (fixed)
justmagicmaria’s picture

I am using Token 7.x-1.0-beta7 and the Link 7.x-1.x-dev from Oct. 28. I see the field token but it does not have the tokens for the title or url components. I have cleared the cache after upgrading. (The Token patch referred to in an earlier comment no longer works with the current Token module.) Has anyone tried this with the latest and had it work?

hibersh’s picture

Status: Closed (fixed) » Patch (to be ported)
hibersh’s picture

Status: Patch (to be ported) » Needs work

patch not working

hackwater’s picture

Issue tags: +field tokens

Looks like we've got a couple of options: wait for the Token module to resolve its architectural issues (see #691078: Field tokens) or try to get this done via the Entity API module's Entity Tokens module, which may have its own challenges. As it happens, the Date module is facing similar issues; there's a currently postponed issue over there summarizing the challenges (see #1103032: Document how to use date tokens).

I haven't tried Link fields with the Entity Tokens enabled yet; I did try to see how far I could get with core Token functionality and the Token module enabled, using the Examples module, the code in comment #3 above, and the Date module's tokens.inc as a template, but it doesn't look like I'll have any more success than the rest of you given the issues I cited.

yannickoo’s picture

You can access the link title and url via the entity token module which is included in the Entity API module.

jastraat’s picture

Pathauto unfortunately requires the token module, and the token module + entity api tokens do not play nice together.

yannickoo’s picture

What do you mean exactly? It works fine for me.

jastraat’s picture

mgifford’s picture

Status: Needs work » Needs review

@jastraat what about #1272560: Entity tokens duplicates field tokens created by token module ?

@hackwater I don't think there's a need to wait till the issues you highlighted in #21 are fixed. Others modules aren't.

There's even a patch from @Deciphered, thought it needs review.

I expect what was needed #691078: Field tokens is available in Token 7.x-1.4.

jastraat’s picture

@mgifford - The duplication of all tokens doubles the number of tokens that the token UI has to handle and leads to a memory problem: http://drupal.org/node/1203018

spencerthayer’s picture

Subscribing

letrotteur’s picture

Anything new regardind this issue? using token 1.5 and link 1.1 doesn't seem to provide token for the title or url.

axe312’s picture

Issue summary: View changes
Status: Needs review » Needs work

This is still not working.

Any updates on this problem? I tried to apply the patch from #4 manually, but the tokens are replaced by "". (I also needed to enable entity_token module)

jcfiala’s picture

So, looking at #1103032: Document how to use date tokens, it seems the Date module doesn't have field token support either.

This seems to be due to some kind of strange technical problem that the folks in charge of Token haven't worked out yet.

I'm willing to see another possible patch, but for the moment this issue seems stalled on problems elsewhere.

heddn’s picture

Status: Needs work » Fixed

I'm going to go out on a limb here and suggest that the entity api integration provided by property_callbacks => link_field_property_info_callback and entity tokens provides this functionality. With that in mind, marking this fixed.

Entity api's tokens provide tokens for each of the defined items in link_field_item_property_info().

Status: Fixed » Closed (fixed)

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

Road Kill’s picture

So what is the status here is it possible to use tokens in field link or not?