I am using Rules to notify editors when users post new/updated content. When the email goes out and I include some node fields in the message, any fields that are blank in the node do not display properly -- they show the actual replacement pattern, not blank text.

For example, here's an excerpt from a Rules-sent email:
Category: [node:field-rostercategory]
Name of Business: Test Business
Name: Johnny Q
Title: Owner
Phone: 123-456-0828
Website: [node:field-website]
Revision Log: [node:log]

I have yet to see any documentation saying this is by design (nor does it seem functionally beneficial) so I'm listing this as a bug.

I'm using Rules 2.1 on Drupal 7.14 (tho I had same issue with 7.12). Thanks to everyone involved in making this amaaazing module-- tho if someone could look into this small bug, that would be awesome.

Files: 
CommentFileSizeAuthor
#19 rules-token-evaluator-handles-empty-values-1559844-2c.patch457 bytesfluffy
FAILED: [[SimpleTest]]: [MySQL] 276 pass(es), 1 fail(s), and 3 exception(s).
[ View ]
#3 rules-token-evaluator-handles-empty-values-1559844-2b.patch481 bytesDRippstein
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch rules-token-evaluator-handles-empty-values-1559844-2b.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#1 rules-token-evaluator-handles-empty-values-1559844-2.patch469 bytesDRippstein
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch rules-token-evaluator-handles-empty-values-1559844-2.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Comments

Title:Actual replacement pattern displays in emails when field is blankField token is not replaced when fields are blank
Component:Rules Engine» Provided Module Integrations
Status:Active» Needs review
StatusFileSize
new469 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch rules-token-evaluator-handles-empty-values-1559844-2.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Determined cause and possible fix for the problem. Renamed issue to be more specific.

Rules' token evaluator is made to closely mimic the 'token_replace' function of the official token module. Except for three lines... these (if a "clean" option is passed) replace blank tokens with a '' (empty string).

This patch will add a line with similar functionality, though it doesn't check for options. It will prevent Rules emails from sending raw token patterns. But I don't know enough to say why this check is an "optional" thing at all. Is there some Token functionality that requires blanks to remain raw token patterns? Or specifically, is there something in Rules that does? If so, how could we pass an option? If we don't need it though, this little patch should do the job.

Status:Needs review» Needs work

The last submitted patch, rules-token-evaluator-handles-empty-values-1559844-2.patch, failed testing.

StatusFileSize
new481 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch rules-token-evaluator-handles-empty-values-1559844-2b.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

This is what happens when I hand-code a patch...

Component:Provided Module Integrations» Rules Core
Status:Needs work» Needs review

Yes, test-bot... and fix category too. Don't even know.

Status:Needs review» Needs work

The last submitted patch, rules-token-evaluator-handles-empty-values-1559844-2b.patch, failed testing.

Well I just wanted to confirm I am experiencing the same issue.

I have an entity where I have optional fields, if the author doesn't enter a value the token is displayed within the email. This is just messy and not desired.

The template-based email actually involves three different entities.

1) User
2) Order
3) Line Item

I'm sending an email to a customer after purchase of the specific product (I'm using Drupal Commerce).

This issue was affecting me as well. @DRippstein's patch in #3 (applied manually) worked wonders for me.

Thanks!

Yep - #3 patch does the trick for me too.

Confirmed, applying the one line addition fixed the empty token being displayed in all emails and messages.

Status:Needs work» Reviewed & tested by the community

Patch on #3 also works for me. Can it be commited?

Playing devil's advocate, I could see having the tokens printed out within the email as useful for debugging a token and the evaluation of the token in a given display. That being said, I think it would be great if this behavior could be controlled, say on a configuration screen for tokens. "Enable display of empty token placeholders" defaulted to "off".

Then we'd just have to add this field value to the default settings for the token module. Add a check before the code that removes all empty tokens during evaluation.

Still, I second that this should be committed to the branch.

Status:Reviewed & tested by the community» Fixed

I've added a comment and committed #3. Feel free to work on a follow-up for making it configurable. Maybe as part of a general rules debug mode setting?

Status:Fixed» Needs work

Still getting this after the patch in #3:

[message:message-commerce-order:commerce-customer-shipping:commerce_customer_address]

Status:Needs work» Fixed

I just tested the latest dev which includes the patch
and it does not output the tokens when fields are empty

please brephraim try the dev instead of the patch and report back
(maybe your problem is commerce related ? try upgrading to latest commerce)

I confirmed with Commerce that problem exists without the patch and the problem is fixed with the patch. Both for Send system mail and Display a message. I think it's more likely that an error is happening based on the event and available variables.

So, bephraim, I recommend you open a Q&A on drupalcommerce.org for support and provide more details.

Status:Fixed» Closed (fixed)

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

I upgraded the rules module to the latest -dev (7.x.2.2+5-dev) and I still see raw tokens in the e-mails sent out by dc commerce. Anyone else still have this issue? Is there something else i should do after upgrading the module?

Status:Closed (fixed)» Needs review
StatusFileSize
new457 bytes
FAILED: [[SimpleTest]]: [MySQL] 276 pass(es), 1 fail(s), and 3 exception(s).
[ View ]

The patch in #3 can't be applied because the file identifiers are wrong, here is the fixed patch, it works nicely.

Status:Needs review» Needs work

The last submitted patch, rules-token-evaluator-handles-empty-values-1559844-2c.patch, failed testing.

The last patch didn't work for me :-( Empty tokens are still displayed in the mail.

Edit : i forgot the save button ;-) The patch is working fine.

I see #19 included in the latest dev
anybody else confirms ?

EDIT wrong thread text included

Status:Needs work» Needs review

Status:Needs review» Needs work

The last submitted patch, rules-token-evaluator-handles-empty-values-1559844-2c.patch, failed testing.

I installed rules 7.x-2.2 version and applied this patch. I configured rules to send mail with 'entity forms' submissions. But for empty values i am getting the token names. eg: Contact Number : [entityform:field-contact-number].

Any solution to fix this?

@josephanoop
try latest dev and report back

The latest dev version solved the issue, thanks!

Status:Needs work» Needs review

I tried the latest stable (7.x-2.3) and dev (7.x-2.x-dev) versions but I do not see change in this - so I still see tokens in the email.
I use commerce_email to sending emails.

same for me, using commerce_email. empty fields still showing the token.

Hi,
May be this helps using commerce_email: http://drupal.org/node/1440236#comment-6057672
Greetings, Martijn

no i dont get helped there. by the way the patch from #19 is already applied to latest dev with no effect:

system.eval.inc, line 183:

// Remove tokens if no replacement value is found. As token_replace() does
// if 'clear' is set.
$replacements += array_fill_keys($tokens, '');

by the way the patch from #19 is already applied to latest dev with no effect

I'm sorry, but this is wrong. The reason you're seeing no effect is likely because you're sending mails e.g. via variable_email. I've added the line to _variable_email_evaluate_token() and it does in fact replace empty tokens.

In my opinion the deeper issue here is that I've already come across two modules (variable_email and rules_mail_edit) that copied portions of RulesTokenEvaluator::evaluate() into their own code.

RulesTokenEvaluator::evaluate() could easily be declared static, allowing other modules to call it for token replacement.

@ciss, for your info: The 1.x-dev version of variable_email already uses the RulesTokenEvaluator class for token evaluation. See #1682730: Use standard rules token evaluation.

@MickL, are you using variable_email and does updating it to 1.x-dev fix your problem? Or, if not, what other modules do you have that are involved in sending email?

@xatoo Yes, i noticed it after posting the above response. I replaced variable_email alpha with the dev version, with the result that tokens no longer got replaced. Digging further I noticed that $element->settings['variable:process'] was undefined (I tested with an already existing rule, using rules-7.x-2.3+3-dev).

Where can I find documentation on how RulesTokenEvaluator is supposed to be implemented?

Hi, still having commerce-customer-billing tokens not replaced. Using variable_email 1. Alpha. Does this post on #34 has to do with it?
Greetings, Martijn

Upgrading to variable_email-7.x-1.x-dev as recommended by #34 breaks ALL tokens used with commerce_email -- kinda the opposite of what we need to accomplish! I know it's not really relevant here, but wanted to point any other variable/commerce users over to #1759140: Token API change in dev needs documentation and maybe an own branch.

Hi,
Yes latest variable_email breaks all tokens! Still no solution!
Also no solution for empty tokens... anyone please?

EDIT: Sory didn't want to hijack this rules issue..only responding to latest remark.
greetings, Martijn

Issue summary:View changes

Grammar change.

Many thx @ciss - post #33 - I added
$replacements += array_fill_keys($tokens, '');
to variable_email.rules.inc after line 136 and that fixed my issue with tokens printing out if they are blank.

using variable_email alpha1 (not latest dev)