2

I am using the tokens module to print personalized email notifications. Sometimes a webform component is not filled out and the email notification leaves an empty space before the token. I am using the Rules module to send a mail.

E.g.

[data:title-value-raw] [data:name-value-raw]

Default output

Mr. Example

Expected result

Example

Actual result

 Example

How can I hide the space if the first token is empty?

Drupal rule to send message.

{ "rules_mail_confirmation" : {
"LABEL" : "Mail Confirmation",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "webform_rules" ],
"ON" : { "webform_rules_submit" : [] },
"IF" : [
  { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
  { "node_is_webform" : { "node" : [ "node" ] } }
],
"DO" : [
  { "mail" : {
      "to" : "[data:email-value]",
      "subject" : "Confirmation",
      "message" : "[data:title-value-raw] [data:name-value-raw],\r\n\r\n",
      "from" : "[site:mail]"
    }
  }
]

} }

  • 1
    I suppose you are asking the wrong question. Empty tokens are 100% hidden by default. The space you are seeing is not coming from the token, but is the same space you used between the tokens. [data:sg_title-value-raw]@[data:sg_name-value-raw] will likely render @Example. So you want to know how to conditionally hide the space if the first token is empty. – Neograph734 Jun 25 '17 at 10:53
  • @Neograph734: Thanks, you're right. I am asking the wrong question. I've edited my original question. How to conditionally hide the space if the first token is empty? – howdytom Jun 25 '17 at 14:15
  • No problem, but a false assumption can guide (yourself) and other people in a wrong direction. I hope something comes to my mind anytime soon, but otherwise someone else might have an idea. – Neograph734 Jun 25 '17 at 14:19
2

If I was to implement this, I'd create a special (tiny) Rules Component as a kind of helper function, and use that in your existing rule also. The clue of that Rules Component is the (not often used) "Provided" facility for one of its parameters. More details below ...

Part 1: Create a Rules Component

Create a new Rules Component that looks like so (in Rules Export format):

{ "rules_create_email_body_prefix" : {
    "LABEL" : "Create eMail body prefix",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "title_value" : { "label" : "Title value", "type" : "text" },
      "prefix_value" : { "label" : "Prefix value", "type" : "text", "parameter" : false }
    },
    "IF" : [ { "NOT data_is_empty" : { "data" : [ "title-value" ] } } ],
    "DO" : [
      { "data_set" : { "data" : [ "prefix-value" ], "value" : "[title-value:value] " } }
    ],
    "PROVIDES VARIABLES" : [ "prefix_value" ]
  }
}

Some details about the above Rules Component:

  • It uses 2 parameters:
    • it accepts title_value as an input parameter (Usage = Parameter).
    • it provides prefix_value as output of it (Usage = Provided).
  • The logic of this Rules Component is like so:
    • Rules Condition: verify that the value of title_value is NOT empty.
    • Rules Action: set the value of prefix_value equal to the value provided via title_value, and follow it by a space (note!).

Part 2: Integrate the Rules Component in your existing rule

Here is an updated version of your rule (also in rules export format).

{ "rules_mail_confirmation_with_optional_prefix" : {
    "LABEL" : "Mail Confirmation with optional prefix",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "webform_rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
      { "node_is_webform" : { "node" : [ "node" ] } }
    ],
    "DO" : [
      { "component_rules_create_email_body_prefix" : {
          "USING" : { "title_value" : "[data:title-value-raw]" },
          "PROVIDE" : { "prefix_value" : { "prefix_value" : "Prefix value" } }
        }
      },
      { "mail" : {
          "to" : "[data:email-value]",
          "subject" : "Confirmation",
          "message" : "[prefix-value][data:name-value-raw],\r\n\r\n",
          "from" : "[site:mail]",
          "language" : [ "" ]
        }
      }
    ]
  }
}

These are the changes I applied (as compared to your rule):

  • Rename the rule (to not override your own rule).
  • Add a Rules Action (as the first one) to invoke the newly created Rules Component (whereas the value [data:title-value-raw] is passed to the title_value parameter).
  • Adapt the message body of the eMail, to now use the prefix_value as provided by the Rules Component. To be complete: if the Rules Condition in the Rules Component is NOT satisfied, the value of prefix_value is empty, otherwise the Rules Component will assign a value is equal to title_value followed by a space.

Notes:

The exported rule as included in the 1st revision of your question, contains some errors/challenges, i.e:

  1. It contains a syntax error, the comma at the end of your 2nd Rules Condition should not be there (only after I removed that comma I was able to import your rule ...).

  2. Something doesn't seem right in the [data:[data:title-value-raw]] token you included, pretty sure it should be [data:title-value-raw], as confirmed somehow also in the edit you applied in revision 6 of your question.

  3. IMO there is no need for "PHP" (as shown within "REQUIRES" in your exported rule ...), as confirmed somehow also in the edit you applied in revision 6 of your question.

  • PS1 / PS 2: yeah, thx I've edited the rules actions afterwards. I've updated my original question. – howdytom Jun 26 '17 at 23:22
  • wow. Thank you for your in-depth explanation. Excellent work. Unfortunately this doesn't work for me. My email output looks like this [data:Gates,. I guess there's a typing error in rules data token? When I add Mr. to the title field, it doesn't show up either. – howdytom Jun 26 '17 at 23:29
  • @howdytom please note that I adapted (improved my answer) to make it fit with revision 6 of your question. Not sure if you realize, but because of those edits in revision 6, you invalidated my previous answer (which is actually not allowed). While working on this improved version of my answer, I also noticed a typo in the "message" included in the rule within part 2: it should be "[prefix-value]" instead of "[prefix-value:value]". And with that, I'd be curious to hear if with this I did address your prior comment. Can you retest it again and let me know the result please? – Pierre.Vriens Jun 28 '17 at 13:47
  • Pierre, thanks again for your reply and time. Sorry, it wasn't my intention to invalidate your comment. I would love to use your solution instead of PHP filter. I have tested your helper function in a fresh D7 installation. However I am really sorry to say that it still doesn’t work. My email output always looks like this [prefix-value]tom. I have set up the following webform components: title, name, email address. I am pretty sure all tokens and naming conventions are correct.Both rules are looking good to me. Besides selecting a valid content type, do I have to make additional modifications? – howdytom Jul 2 '17 at 22:32
1
+50

use rules conditional module, first action: add a conditional, add an IF, "data value is empty", field:[data:[data:title-value-raw], here you have only content with token empty, avoid in message token value, add an action:

"DO" : [
{ "mail" : {
  "to" : "[data:email-value]",
  "subject" : "Confirmation",
  "message" : "[data:name-value-raw],\r\n\r\n",
  "from" : "[site:mail]"
}
}
]

add an ELSE "under conditional", here you are with content that token value is not empty, so, include token value in message, add an action under ELSE:

"DO" : [
{ "mail" : {
  "to" : "[data:email-value]",
  "subject" : "Confirmation",
  "message" : "[data:[data:title-value-raw] [data:name-value-raw],\r\n\r\n",
  "from" : "[site:mail]"
}
}
]

Done, i hope it helps.

EDIT: if do not want to install another module, then make two rules, on first rule, add a condition: "data value is empty", field:[data:[data:title-value-raw], set message avoiding in message empty token value. Second rule: "data value is empty", NEGATE, field:[data:[data:title-value-raw], the complete your message including token, this time token wont be empty.

  • Excellent point. rules conditional module has been my last chance. However it hasn't been updated since 2012. For now I didn't want to install another module. I'll definitely take a look at rules conditional module for upcoming projects. Your last comment pointed me in the right direction. Take a look at my answer. – howdytom Jun 26 '17 at 23:08
  • i have used conditional rules module in many D7 sites with no problem. – pinueve Jun 26 '17 at 23:28
0

The action you are looking for is called trimming; stripping whitespace from the beginning (and end) of a string.

However to do that, we first need to merge your two fields into one. Before the mail add the action of Add a variable, give it the value [data:title-value-raw] [data:name-value-raw] (No data selector, just direct input mode). Also give it a sensible name for later.

Now we have one token that contains Mr. Example or [whitespace]Example. By removing the whitespace from this one token we can get the desired result. (Removing the white space from individual tokens would not have made any difference.) Rules by default does not offer a Trim action , but Rules Data Transforms does. Add the trim action for your previously created field and use the trimmed output in your mail.

  • Thank you for your reply. I was thinking about trimming as well, however I didn't want to install another dev module for such basic task. Take a look at my answer. – howdytom Jun 26 '17 at 23:07
0

Many thanks to your replies. Your answer has been really useful to me. I didn't expect that hiding a space with a simple rules action requires so much effort. I've ended up using 2 rules (yeah, not perfect) with a custom php code that checks for value none. Now, instead of a text field I am using select options as a webform component. If you find a more elegant solution, feel free to share it.

Here's what works for me.

  1. Within the Webform select options I've added a key-value pair none|
  2. Create a new rules condition using Execute custom PHP code and add the following code as value:

if ($data['components']['title']['value'][0]=='none') {
return TRUE;
} else {
return FALSE;
}

Rule 1: Without title

{ "rules_mail_confirmation_without_title" : {
    "LABEL" : "Mail Confirmation without Title",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "webform_rules", "php" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
      { "node_is_webform" : { "node" : [ "node" ] } },
      { "php_eval" : { "  if/else code from above " } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[data:email-value]",
          "subject" : "Confirmation",
          "message" : "Hello [data:name-value-raw],\r\n\r\n",
          "from" : "[site:mail]"
        }
      }
    ]
  }
}

Rule 2: With title

{ "rules_mail_confirmation_with_title" : {
    "LABEL" : "Mail Confirmation with Title",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "webform_rules", "php" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
      { "node_is_webform" : { "node" : [ "node" ] } },
      { "NOT php_eval" : { " if/else code from above " } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[data:email-value]",
          "subject" : "Confirmation",
          "message" : "Hello [data:title-value-html] [data:name-value-raw],\r\n\r\n",
          "from" : "[site:mail]"
        }
      }
    ]
  }
}
  • The beauty of Drupal is that there are often multiple ways to achieve the same results. However the solution included in this answer has a major challenge: it requires the PHP filter to be enabled (ie: store PHP code in your Drupal database). That is commonly considered as something you should try to avoid whenever possible. And since my own answer uses a Rules Component to implement that helper-like function that does the equivalent of your PHP-code, my recommendation (not more then that ...) is to give it a 2nd thought to try to get your job done without using the PHP filter. – Pierre.Vriens Jun 28 '17 at 13:52
  • Pierre, Totally agree, I would rather not enable PHP Filter, however Rules module makes it so time-consuming to implement such a simple tasks. Moreover I’ve added additional php field checks. I’ve spend several hours researching for a better solution. Creating a simple condition with Rules is a pain. Anyway, I definitely have to dive more into Rules workflows. – howdytom Jul 2 '17 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.