Would be great to see a Drupal 7 version of this handy module.

Comments

hanoii’s picture

will probably be working on this on Jan

W.M.’s picture

I will happily test and provide feedback.

mskicker’s picture

i think that is important for drupal 7 too and i try help you about test it

darkadept’s picture

I would love to test it as well. This module would help me very much!

gaspaio’s picture

how's work on custom tokens D7 going ? When are you planning to do a first release ?

jeffwidman’s picture

subscribe

Rix-1’s picture

+1 (please go for it! be a hero)

basicmagic.net’s picture

subscribe

gaspaio’s picture

Could we have some feedback here ?
If you don't have the time to do it i'll gladly work on it ; just don't want to duplicate the effort.

hanoii’s picture

Sorry for the silence. To be honest, I haven't really moved into heavy drupal 7 development.

If you want to contribute a working version of this module for D7 I will be more than fine to try it out, and commit/release it if it looks Ok.

gaspaio’s picture

Thanks for the feedback.
I'll try to produce a quick port in the next few days, as a starting point.

gaspaio’s picture

StatusFileSize
new18 KB

As promised here's a quick and quite untested port of token_custom to D7.
As most of the token api has been put in core, this version is no longer dependent on Token.
Appart from the expected bug hunting, typo correcting, etc, i'll be working on several other features :

  • Allow users to define their own token types (and needs-data dependencies)
  • Add a plain-text custom token types with a separate permission. Users with 'create plain-text custom tokens' will not be able to add php code, only plain text. Sometimes we only need custom tokens to provide
    for sitewide constant values (like specific nids, tids, etc.), plain text is more than enough for this.
  • Integration with the ctools exportables API. It would be nice to be able to export our custom tokens individually via Features wouldn't it ?

Feedback is of course most welcome.

gaspaio’s picture

StatusFileSize
new19 KB

Same as #12 but drupal coding standards compliant.

W.M.’s picture

Thanks very much gaspaio. I will look into this and try it.

W.M.’s picture

The attachment file under #13 results in extraction error. The tar.gz file appears to be broken.

gaspaio’s picture

I just downloaded the file and extracted it.
Did you change the name to token_custom-7.x-1.x-dev.tar.gz ?
I'm using : tar (GNU tar) 1.23, on Ubuntu.

W.M.’s picture

@gaspaio

It is fine now. I have managed to exract the archive. I have created two custom tokens to return:

a. the last two digits of a nid
b. 3rd and 4th digits of a nid

The codes are:

a:

if (strlen($node->nid) >= 2) {
$l00_01 = substr($node->nid, -2);
}
elseif (strlen($node->nid) == 1) {
$l00_01 = substr($node->nid, -1);
}
else {
    echo "Failed";
}
return $l00_01;

b:

// function for 2nd level
if (strlen($node->nid) >= 4) {
$l0000_02 = substr($node->nid, -4);
$l0000_01 = substr($l0000_02, 0, -2);
}
elseif (strlen($node->nid) == 3) {
$l0000_01 = substr($node->nid, 0, 1);
}
elseif (strlen($node->nid) <= 2) {
$l0000_01 = 0;
}
else {
    echo "Failed";
}
return $l0000_01;

This worked fine on D6 and official release of Custom Tokens module. Yet in D7 the dev version posted above results in these errors:

Notice: Undefined variable: node in eval() (line 2 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 2 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Undefined variable: node in eval() (line 6 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 6 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Undefined variable: node in eval() (line 9 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 9 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Undefined variable: node in eval() (line 1 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 1 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Undefined variable: node in eval() (line 4 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 4 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
Notice: Undefined variable: l00_01 in eval() (line 10 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).

Any ideas what might be wrong here ?! Maybe in D7 $node->nid should be written in another manner ?!

gaspaio’s picture

StatusFileSize
new19 KB

I see what it is.
There was a change in the token api from the D6 Token module and the D7 core tokens.
The D7 function token_replace receives as argument the $data array, containing any loaded objects : the $user, the $node, etc as $data['user'], $data['node'], etc.
This allows for more data to the available to the hook_token implementations.

Your code should then start with :

$node = $data['node'];

The type of your custom token should also be 'node', to avoid runtime errors. Drupal should not try to replace your token if the 'node' entry is not available in $data.

Here a new build, with a small correction : in the token list page, we don't try to print a demo value if the token needs external data (like a user object, a node, etc).

W.M.’s picture

I have tested your suggested modification to my code. It works as it should now.

Thanks for all the effort you put into this project.

hdcoder’s picture

Please publish any simple example token_custom for use with path.

PS:

I create new token_custom:
- Token name: Test.
- Machine name: node-publ-term.
- Token description: Simple text.
- Token type: [needs:node].
- PHP replacement: return "test";

In path module in section for nodes set default template: [node:node-publ-term]/[node:nid]
Result: I have error save settings path module. [node:node-publ-term] not found.

Please help.

W.M.’s picture

My response relates to the dev version found under relpy #18 by gaspaio.

The module works great, however I have noticed a strange problem that may be caused by faulty code at:

a, above mentioned D7 dev release of Custom Tokens
b. Token module
c. Filefield paths (latest stable)

The problem is that I get an invalid (not found) token error when specifiying the name of a custom written token in the filefied path of an image field in a RTL site. If I switch the main language to Englsih and put the same token name in the custom path field and submit the form, it works (no error reported and the setting is stored to teh database).

I have that we can work togother to make this module better.

gaspaio’s picture

@hdcoder
I can't reproduce your issue. I installed pathauto RC2 and added a custom token using your instructions and set the path alias to [node:node-publ-term]/[node:nid].
When i create a new node, its path is "test/1", as expected.
Any ideas on what might be going wrong with your installation ?

gaspaio’s picture

@Geir19
This will sound suspicious (cf. #22 :-D) but i'm having trouble reproducing your issue also. Here's what i did :
1) installed filefield_paths (latest stable) and token
2) defined a custom token of type node
3) used it in the path of an image field
4) downloaded and installed the arabic language
5) Set it as the default language
6) blindly navigated to the add content section and added an article with an image.

I got no error messages, the file was saved in the correct directory using the custom token i defined.
Any ideas ?

gaspaio’s picture

Just thought of something : my keyboard is a french one, and the path and token were writen in a LTR language and latin alphabet. Perhaps the error doesn't comme up unless the token of path is typed in a RTL keyboard.
Does filefield_path perform well with normal tokens in a RTL context ?

hdcoder’s picture

gaspaio thank you! I had an older version pathauto (7.x-1.0-beta1). Updated and it worked!

W.M.’s picture

I think the Drupal 7 version provided by gaspaio should be made public (available at main module page). It simply works. Thanks a lot gaspaio :)

gaspaio’s picture

Thanks Geir19.

I actually would like to maintaint the D7 port and add some features (like features support) to it.

@hanoii : is it possible to add me as a co-maintainer for this module ?

If no release is done in a few weeks, i'll probably end up creating a new project for the D7 version, although its a shame to multiply related projects.

W.M.’s picture

@gaspaio

I want to inform you that the latest release of Token (version 7.x-1.0-beta4) conflicts with token_custom module for Drupal 7 (that you have provided earlier at this post).

Upon editing previously created nodes or creating new ones, I get these errors:

    Notice :Undefined index: node in eval() (line 2 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Trying to get property of non-object in eval() (line 3 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Trying to get property of non-object in eval() (line 7 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Trying to get property of non-object in eval() (line 10 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Undefined index: node in eval() (line 1 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Trying to get property of non-object in eval() (line 2 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Trying to get property of non-object in eval() (line 5 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Undefined variable: custom_token_nid_21_a in eval() (line 11 of /home/user/public_html/mysite/sites/all/modules/token_custom/token_custom.module(275) : eval()'d code).
    Notice :Array to string conversion in token_replace() (line 97 of /home/user/public_html/mysite/includes/token.inc).
    Notice :Array to string conversion in token_replace() (line 97 of /home/user/public_html/mysite/includes/token.inc).

The php codes I use to generate numbers based on nid did not change at all. The thing that resulted in all those errors is upgrade to Token (version 7.x-1.0-beta4).

In the image path, the directory name which should have the value provided by custom_tokens is "Array".

W.M.’s picture

@gaspaio

I have used the same PHP codes above inside a custom module of my own and hooked into hook_tokens and hook_token_info.

It works fine with no problems or conflicts. The custom module I have created works perfectly with the latest Token module (version 7.x-1.0-beta4).

I will happily see the original Custom Tokens module working flawlessly inside Drupal 7. I love the UI. Makes token management and authoring easier.

Thanks for the help so far, I have learned much from reading the code. Best wishes.

gaspaio’s picture

I gess there's something wrong with the eval function call on the custom token's php code.
Anyway, i'm thinking of using the core's php filter to evaluate custom tokens instead of the eval function, it seems much more like the 'drupal way' to do it.
I'll try to check it out over the weekend.

gaspaio’s picture

Indeed there's some weird stuff going on here. It seems that the token module is trying to evaluate tokens of type 'node' without filling the $data['node'] entry, instead it does $data['entity'] and $data['entity-type'].

There's another problem though, even when i evaluate simple text with a custom token i get errors from pathauto ; different ones but errors still.

This is more complicated than i though ; i'll have to give it another go soon.

@Geir19 : if you have any idea of how to correct this ...

dave reid’s picture

I'm confused by "It seems that the token module is trying to evaluate tokens of type 'node' without filling the $data['node'] entry, instead it does $data['entity'] and $data['entity-type']."

gaspaio’s picture

StatusFileSize
new19 KB

@DaveReid : and you have all the right to be confused because I was wrong, it's not the token module nor anybody else, there was a bug i my code. Sorry about that :-).

Here's an updated version that should work with @geir_19 test cases and correct the notices in #28.

W.M.’s picture

Status: Active » Reviewed & tested by the community

Thanks gaspaio for the great work. It works perfectly now :) New version of Sep. 3rd 2011, passed my tests.

gaspaio’s picture

I'm thinking of starting a new project with my D7 port. I haven't been able to context custom token's maintainer via DM.

What do you think ?
Since the machine name for custom tokens is actually token_custom, i could call my module custom_tokens.
I do think this is not the way to proceed, but i don't see any alternative. Any ideas ?

hanoii’s picture

Hi @gaspaio, Sorry, I am a bit busy but I generally answer DM always. It must have slipped when I read it. not necessary to add another project. So, are you up to adding and maintaining the D7 port? Will try to give a quick review to the code, but otherwise, I don't mind co-maintainship

hanoii’s picture

@gaspaio, just granted commit access. Leave you to fix this issue when necessary.

gaspaio’s picture

Thanks @hanoii.
Just commited the #33 version to the newly created 7.x-1.x branch.
Thanks to @geir19 for the precious feedback.

leanderl’s picture

Hi, I used this module for D6 and would love to help if I can with the D7 version.

Right now I've installed it and I only get the "white screen of death" after having tried to create a token. Has anyone encountered this? As far as I can see from the code, things related to the module should be stored in db table token_custom. So I wanted to clear that table "by hand", but it isn't there (hasn't been created). Hence it is weird that I have a white screen of death for "admin/structure/token-custom/list" when there isn't even any data. Hmm...

leanderl’s picture

It turns out it was my PHP/SQL that was out of order.

This was the D6 way of getting your token

$value = db_result(db_query("SELECT some_field FROM {some_table}"));
return $value;

This is the D7 syntax for the same thing

$value = db_query("SELECT some_field FROM {some_table}")->fetchField(); 
return $value;

Again: great work gaspaio!

justmagicmaria’s picture

Category: feature » bug

I'm using the version from #33 with the world's simplest PHP for my custom token: return "Test";

The token type is Node.

When I use the token (in the configuration of the Talk module to set the value for the Talk tab) I get:

Notice: Array to string conversion in str_replace() (line 97 of /
/includes/token.inc).

and the value that gets printed is "Array".

I'm using 7.x-1.0-beta6 of Token module.

Any ideas?

hdcoder’s picture

Thanks for fix #33!

jordanmagnuson’s picture

Can we please get an official 7.x dev release? Still no 7.x branch on the module page.

gaspaio’s picture

Actually i'm trying to do a release but there is a bug in Drupal.org that prevents my tag from appearing in the possible releases select. According to the support mailing list it should be easy to fix for a drupal.org administrator, but none has picked up the issue i opened so far.

I'll probably just create another tag one of these days and see if it works.

I'm releasing a beta version directly. Since the module is pretty straightforward, i don't see the need for a dev release right now.

gaspaio’s picture

Version: 6.x-1.2 » 7.x-1.0-beta2
Category: bug » feature

Release 7.x-1.0-beta2 is out and should be available in a few hours.
If no important bug is reported in the next week or two i'll roll a official 7.x-1.0 release.

dtj’s picture

I seem to be having a difficult time using more than one custom node token.

I can create one node token and it appears under "REPLACEMENT PATTERNS" but the second node token I create does not appear under "REPLACEMENT PATTERNS".

gaspaio’s picture

Indeed. The token module adds a cache table for token information. New tokens don't seem to be available without clearing the caches.

I just commited a token_clear_cache() after every custom token save and delete.

Can you empty the cache manually and see if it solves your problem ? After that, can you download the latest version of token_custom from the git repo and test it ? it should work.

Thanks for the bug report.

gaspaio’s picture

Status: Reviewed & tested by the community » Closed (fixed)

I am closing this issue since the release is out and we can now open new 7.x issues.