token_token_info() defines the url token type as:

  // URL tokens.
  $info['types']['url'] = array(
    'name' => t('URL'),
    'description' => t('Tokens related to URLs.'),
    'needs-data' => 'url',
  );

This is wrong, because token_tokens() is actually looking for a 'path' key in the data, not a 'url' key:

  // URL tokens.
  if ($type == 'url' && !empty($data['path'])) {
    $path = $data['path'];

Patch changes the 'needs-data' value to be 'path' instead of 'url'. URL tests still run green after the patch.

Apparently the 'needs-data' key has no function except as documentation (and the documentation is wrong in this case...). This is unexpected - I would have thought that by specifying 'needs-data' there would be a type check built into the token API so that token_generate() and token_replace() would fail if the required data types were not present in the arguments. Instead, they just return silently without doing anything.

Comments

dave reid’s picture

Status: Active » Fixed

Very odd - you are correct. Yes, there's no validation at all.

Committed to Git: http://drupalcode.org/project/token.git/commit/2cb9981

dave reid’s picture

Status: Fixed » Needs review
StatusFileSize
new1.06 KB

Testing adding validation.

tr’s picture

StatusFileSize
new1.06 KB

I tested the patch. The placeholder key was wrong, and the '.' at the end of the sentence shouldn't be there because trigger_error() appends some text to the sentence. Updated patch attached.

This is a good thing to do for now, but I think the proper place for validation is higher up in the food chain. We can't really rely on all contributed modules that implement hook_tokens() to validate against 'needs-data'. This is something that can and IMO should be done within the token API functions. But that's a core issue ...

dave reid’s picture

Wondering if we should throw E_USER_NOTICE or E_USER_ERROR.

dave reid’s picture

StatusFileSize
new1.04 KB
dave reid’s picture

StatusFileSize
new2.16 KB

Had to move validation to the end to work on entity type tokens. Also discovered a problem with the content type token type's needs-data value.

dave reid’s picture

StatusFileSize
new2.17 KB

Going to guess we'll need to use E_USER_WARNING.

dave reid’s picture

StatusFileSize
new1.88 KB

One last patch leaving validation in but leaving alone the url type, this should pass testing.

tr’s picture

Status: Needs review » Needs work

Your patch in #7 reverts the patch committed in #1: http://drupalcode.org/project/token.git/commit/2cb9981

dave reid’s picture

Status: Needs work » Needs review

Yes, the point was to test it was working.

dave reid’s picture

StatusFileSize
new1.07 KB

Final patch.

dave reid’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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