Posted by eärendil on February 27, 2013 at 8:45pm
I have been trying to create a trigger so that when someone updates a page node by inserting an image into the body it will send me an email. So, in other words, I want the trigger to act (send an email) if it finds an img tag in the body of the updated node.
Event:
After updating existing content
ON even After updating existing content
If
Two texts to compare:
Text1:
<?php
print $body;
?>Text2: /<img/gi
**I have evaluate text2 as regular expression checked.
I have tried so many different ways, but can't get it to work. It will either never send an email or regardless of what I put in the body it will always send an email.
What am I doing wrong?
Comments
Found a solution
I found a solution that worked for my purposes. Not saying this is the best way to do it, but I used the "textual comparison" option and only used the $text1 output. The value for $text1 was
<?phpprint $body->content
?>
So, instead of trying to match the $text2 content, I modified the text_compare function in the rules.rules.inc file to search for a specific string in $text1.
function rules_condition_text_compare($text1, $text2, $settings) {
if ($settings['regex']) {
return (bool) preg_match('/'. str_replace('/', '\/', $text2) .'/', $text1);
}
if(strstr($text1, '<img src=')) {
return TRUE;
}
return FALSE;
}
Any and all feedback is welcome. Thanks.
Doesn't work with a regular text comparison action?
Hi Chad_Whitman,
Thanks very much for letting us know about this requirement.
I found it particularly strange that this couldn't be achieved with normal/simple Rules.
So I went in and tried to do the test on a local version.
I came up with the following Rules export:
{ "rules_notify_image_in_body_content_update" : {"LABEL" : "Notify if image in body on update",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "node_update" ],
"IF" : [
{ "text_matches" : {
"text" : [ "node:body:value" ],
"match" : "\u003Cimg[^\u003E]\u003E",
"operation" : "regex"
}
}
],
"DO" : [
{ "drupal_message" : { "message" : "There is an image in content body." } }
]
}
}
You should be able to import this rule, modify it and test it further on your development enviroment.
Pretty much what needed to be tuned carefully was the regex and making sure that regular expression was selected in the text comparison action.
I used the regex:
<img[^>]>instead of the one you indicated.Honestly, I would be really surprised if any of the code in Rules had to be modified to achieve this feature.
I would greatly appreciate your feedback on the views export and let me know if I overlooked or missed anything in your requirements.
Please let me know if you would have any questions, comments, recommendations or issues on any aspects discussed in this post, or on the provided rules export, I would certainly be glad to provide more information or explain in more details.
Thanks very much in advance for your comments, feedbacks and testing.
Cheers!