Many modules allow the use of tokens, but generally not when using PHP (I can tinker, but not code). I know there are other ways to get at this data, but unlike tokens, they are not readily available, whereas token names are available in admin/help/token, and elsewhere.
I've looked at api.txt in the token module, but it goes straight into token replacement, which seems to be for chunks of text, not for PHP code to be executed. And token_get_values seems to be far more complicated than I need (or understand!).
- Is there a basic function to get the value or key(label) of a token. Something like:
$myname = gettokenvalue("[field_myname]")
$mynamelabel = gettokenlabel("[field_myname]") - Am I right that not all tokens will be available to all PHP fields, and other than "global" tokens, only those token that are used in a node will be available, and only before the PHP field is used?
- Using PHP can I write tokens back to their source in the database, ie. something like:
writetokenvalue("[field_myname]", "Fred");
Comments
Comment #1
donwinchell commentedMaybe I can help a tiny bit, and I hope it is Ok to do this in this place.
I am about in the same place you are, but I did discover 1/2 of what you are asking. this works, I am using it, it is not a guess. But it probably took 4 hours to just find, this part of it.
So I can't tell you if it is correct, good code or just lucky, but it works.
// Run site creation script, then redirect to completed page
// this is the start of the code, no magic stuff happens before this.
$var = token_replace('[current-user:name]', array('current-user' => $user));
//strip out spaces from name if there are any
$var=str_replace(" ", "", $var);
$var2 = token_replace('[current-user:mail]', array('current-user' => $user));
$var3 = "aberdeen";
$var4 = token_replace('[commerce-order:order-number]', array('commerce-order' => $order));
// run the external script
print exec("sudo makesite_simple.sh $var $var2 $var3 $var4");
// return the user to the "completion" page
header( 'Location: http://www.xxxxx.ca/node/7' ) ;
exit();
Comment #2
iantresman commentedThanks for that, all very useful
Comment #3
kejmil commentedHello friends,
when I create a new content, I want the "title" (the mandatory fixed field) to have a specific form and to be generated automatically based on the filled fields.
That's why I've set a few nodes in "AUTO NODE LABEL" that generate my page title. It looks roughly like this: [node: field_per_degree_before_n] [node: field_per_first_name] [node: field_per_surname] [node: field_per_degree_after_n], [node: field_per_profession], [node: field_per_narozen]
Between some of that tokens I want to add comma "," but I want the comma to be generated only if token has value (e.c. the field of that token is filled). If not, then I do not want to generate this comma. The reason is that in the case of some no-filled fields I do not want to have commas generated, for example, the title incorrectly generated could look like this:
Ing. Kamil Blazek, , student,
correctly should look like this: Ing. Kamil Blazek, student
Why should be generated like this? Because I left empty field "field_per_degree_after_n" and "field_per_narozen" - so tokens are not added, but the commas yes, because they are as fixed text.
How can I ensure something like this: if "field_per_degree_after_n" haas value Than "," and if "field_per_narozen" has value Than ","?
I do not know PHP codes, so if there is some tool or if someone have solved already this php, could you help me and send me it? Also I have information to use function "implode". Also I do not know how. I am lost...
Please for help.
Thank you
Comment #4
kejmil commentedSolved:
[node:field_example]
Comment #5
PoidsTotal commentedSometing similar to above worked for me.
I was trying to use
in views and got to work with one of the views fields.
Comment #6
PoidsTotal commentedTo update:
The format above works on views, but not in rules, I have to use token_replace
Comment #7
baltazarz3 commented#4 made my day, thanks mates
Comment #8
bluegeek9 commented