Hey everybody. I'm working with Jose on messaging, and we've had a lot of requests for conditional messages based on common token values such as node-type or otherwise. To that end, I have created a little token_logic simple pseudocode module that allows conditions based on tokens to be added anywhere tokens are placed. An example of the resulting code is:

[if][type][equals]story[then]This is a story[endif]

The thing is that it requires more than the current str_replace logic to work, and to that end I needed to add a hook that allows other modules to modify the string token is working on. The new module and the requisite small patch for token 6.x are both attached. I look forward to your response!

Comments

eaton’s picture

Status: Needs review » Closed (won't fix)

I'm really uncomfortable with the attempts to put this kind of stuff into token module. If these kinds of needs exist, I think it's a good case to enable PHP Parsing in your module, ala the 'block visibility' field, and allow PHP to do the work. I'm open to input from Greggles and he other maintainers, but if there is a global need for an "Apple Automator-Like" conditional logic editor in Drupal, I think it should be handled as another project, not embedded as a pseudo-language inside of token.

greggles’s picture

Status: Closed (won't fix) » Needs review

This is the same kind of thing that http://drupal.org/project/workflow_ng and http://drupal.org/project/rules attempt to solve. I'm conflicted about those projects but not a huge fan.

Eaton is less conflicted about them and dislikes the idea (I think the quote is "why reinvent PHP with square brackets instead of $?").

So...I think this is probably a won't fix :/

Especially since a fairly different version of token is destined for core which would probably make this work less useful...I think this is not a good long term solution.

greggles’s picture

Status: Needs review » Closed (won't fix)

Wow, we cross posted but said basically the same thing.

jdln’s picture

Whats going on with this?

Ive read this;
http://developmentseed.org/blog/2009/mar/25/tokenlogic-helps-messaging-a...

And downloaded the module here;
http://code.developmentseed.org/token_logic/dashboard

But im getting this error;
Fatal error: Call to undefined function _token_replace_tokens() in /home/mysite/public_html/sitename/sites/all/modules/token_logic/token_logic.module on line 144

The link didnt have a patch, so do I need to patch the token module for this to work?
Thanks

greggles’s picture

I don't know, but as the comments here suggest it's not part of the token module and asking for help on it here in this queue is unlikely to be fruitful.

jvieille’s picture

That was a neat approach, though it will not work anymore following the changes in the Token module.
I succeeded at getting rid of the _token_replace_tokens() error by changing at line 143

if (count($arg) == 2) {
    if (_token_replace_tokens($arg[0], $tokens, $values, $leading, $trailing) == _token_replace_tokens($arg[1], $tokens, $values, $leading, $trailing)) {
       $return .= _token_logic_replace_recurse($evaluate[1], $tokens, $values, $leading, $trailing);
    }
  } else {
    $arg = explode($notequals, $evaluate[0]);
    if (_token_replace_tokens($arg[0], $tokens, $values, $leading, $trailing) != _token_replace_tokens($arg[1], $tokens, $values, $leading, $trailing)) {

by

if (count($arg) == 2) {
    token_prepare_tokens($tokens, TOKEN_PREFIX, TOKEN_SUFFIX);
    if (str_replace($tokens, $values, $arg[0]) == str_replace($tokens, $values, $arg[1])) {
       $return .= _token_logic_replace_recurse($evaluate[1], $tokens, $values, $leading, $trailing);
    }
  } else {
    $arg = explode($notequals, $evaluate[0]);
    if (str_replace($tokens, $values, $arg[0]) != str_replace($tokens, $values, $arg[1])) {

but this did not work.
I wanted to use it to do conditional info in messaging templates using the following syntax:

[if][type][equals]event[then]
Date : [field_date-view]
Lieu : [field_lieu-title] [field_location-formatted]
[endif]

I can't figure out another solution

jenlong’s picture

I've gotten token logic to work with a few fixes.

Line 122:
-- $thenp = strpos($original, $thenp, $start);
++ $thenp = strpos($original, $then, $start);

Line 125:
-- while ($within = strpos($original, $if, $within + strlen($if)) && $within < $end) {
++ while (($within = strpos($original, $if, ($within + strlen($if)))) && $within < $end) {

laraz’s picture

@jenlog

I have this error:

Fatal error: Call to undefined function _token_replace_tokens()

do you know what happens?

thanks.

goldlilys’s picture

Subscribing.

Getting similar error:

Fatal error: Call to undefined function _token_replace_tokens() in /sites/all/modules/token_logic/token_logic.module on line 144

laraz’s picture

This works for me

http://drupal.org/node/279491#comment-5711282

#3 Posted by sagannotcarl

jvieille’s picture

StatusFileSize
new3.25 KB

I finally got token_logic working, and I love it.

The last error was around line 150

Replace
if (str_replace($tokens, $values, $arg[0]) == str_replace($tokens, $values, $arg[1])) {

by
if (str_replace($tokens, $values, substr($arg[0],1,-1)) == str_replace($tokens, $values, $arg[1])) {

This module could never work without this change, I wonder how it could for someone...
Basically, it was testing [xxxx] against xxxx

If you want this module working, you have to make all the changes submitted in #6, #7 and #11 (this one)
Attached the working module

jvieille’s picture

StatusFileSize
new3.25 KB

The error was also for [notequals]...
This one should be OK

jvieille’s picture

Status: Closed (won't fix) » Needs review

Status: Needs review » Needs work

The last submitted patch, token.patch, failed testing.

dave reid’s picture

Status: Needs work » Closed (won't fix)

File your issue against token logic please, and not the token module.

jvieille’s picture

Status: Closed (won't fix) » Closed (fixed)

There is no token logic module project.
Apparently, José dropped its effort and support to this module after Eaton made his best to ridicule it.

I understand that for php coders, it looks dumb, but for ordinary, speedy web developers, it is a must.
Unfortunately, Drupal is not for ordinary developer.

I mark it as "fixed", because it is. We just need a placeholder for this dumb module.