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.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 1164030-test.patch | 1.07 KB | dave reid |
| #8 | 1164030-test.patch | 1.88 KB | dave reid |
| #7 | 1164030-test.patch | 2.17 KB | dave reid |
| #6 | 1164030-test.patch | 2.16 KB | dave reid |
| #5 | 1164030-test.patch | 1.04 KB | dave reid |
Comments
Comment #1
dave reidVery odd - you are correct. Yes, there's no validation at all.
Committed to Git: http://drupalcode.org/project/token.git/commit/2cb9981
Comment #2
dave reidTesting adding validation.
Comment #3
tr commentedI 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 ...
Comment #4
dave reidWondering if we should throw E_USER_NOTICE or E_USER_ERROR.
Comment #5
dave reidComment #6
dave reidHad 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.
Comment #7
dave reidGoing to guess we'll need to use E_USER_WARNING.
Comment #8
dave reidOne last patch leaving validation in but leaving alone the url type, this should pass testing.
Comment #9
tr commentedYour patch in #7 reverts the patch committed in #1: http://drupalcode.org/project/token.git/commit/2cb9981
Comment #10
dave reidYes, the point was to test it was working.
Comment #11
dave reidFinal patch.
Comment #12
dave reidCommitted #11 to Git: http://drupalcode.org/project/token.git/commit/e838089