Active
Project:
Rules
Version:
8.x-3.x-dev
Component:
Rules Core
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
19 May 2009 at 14:56 UTC
Updated:
2 Feb 2015 at 17:27 UTC
Jump to comment: Most recent
I want to run javascript with rules so I can do some client side things.
Comments
Comment #1
fagoHm, we could add an "Add javascript" action, that just relies on drupal_add_js().
Comment #2
burningdog commentedThat sounds good - I'd love this feature :) There's an interesting problem that would have to be solved in getting this right, though. Since rules are executed when the page is saved, adding drupal_add_js isn't going to do anything, since it's got to be added only once the rule has processed and drupal is bootstrapping again.
So there'd have to be something in Rules which picks up that javascript must be added, but only adds it via drupal_add_js when the new page (or whatever) is loading. Do I have that right?
Comment #3
mitchell commentedComment #4
rv0 commentedI stumbled upon this feature request
Any chance this would be possible to do in a "Custom PHP code" action?
Comment #5
dwangba commentedI am trying to get JavaScript code executed when a user clicks on a rules link.
I think Roger is right in #2 because the JavaScript won't execute using drupal_add_js in a custom rules action.
Can anyone point me in the right direction to achieve this. Thanks!
Comment #6
dwangba commentedI found a solution using behaviors.
Comment #7
Anonymous (not verified) commentedHello. Has this issue been followed up? I would find it very very helpful to be able to execute some client side javascript using the rules module. jakob
Comment #8
SilviaT commentedSame request for me, it would be nice to have it as an action
Comment #9
mitchell commented#2 & #5: No, that's not right. 'Drupal is initializing' would definitely work but is expensive, and 'Content is viewed' would probably work because Rules Forms Support can modify the served html, afair. I haven't looked at the code personally, but if fago suggests drupal_add_js() support will make this work, I'm gonna believe that.
#6: What did you do?
#m: The maintainers are waiting on a patch from you.
Comment #10
nocean commentedNot at all to take away from adding this feature, but I wanted to share how I managed to do this without a new action by using the drupal_set_message(); function and a dollop of CSS.
I wanted to fire off a bit of javascript when a node was created/edited. So, I simply setup my Event and Conditions, and then for Action I used "Execute Custom PHP Code" with the following PHP:
drupal_set_message('<script type="text/javascript">MY JAVASCRIPT CODE</script>','js-message');That prints the script in a message box after the node is saved/edited and thus fires the JS. The one problem was it generated a message box, which was a bit unsightly. This is where the "js-message" comes in. By defining the message type as "js-message" I can then target the message box in CSS by adding the following to my stylesheet:
.js-message {display:none;}Anyway, maybe not a fully-integrated solution, but it got the job done. Wanted to share in case anyone else wandered here looking for a solution as I did.
Comment #11
nambio commentedPerfect implementation, thanks daobydesign.
I implemented it as a custom action (rather than the custom php code thing) which is the only improvement I can think of before we get into *actual* best practice.
Comment #12
mitchell commenteddrupal_add_js ('inline'): Executes a piece of JavaScript code on the current page by placing the code directly in the page. -- The example seems to reflect the intentions here, so changing title for that, specifically.
#10 lead me to read about drupal_add_css too. The two functions seem to work hand in hand and it similarly supports inline code, so that'll be an interesting issue, too. Also, Rules Forms Support might come in handy for "pre-post-processing" form elements, as shown in drupal_add_css/7#comment-23333
---
There's a support request for this in #1678364: Is Rules module designed to work on the backend or can it be used to display UI elements, so I left it open, for when a solution is implemented, to document an example.
Comment #13
luksakWould be really nice to have this especially in cases where contrib modules like commerce use rules hooks like this one:
http://api.drupalcommerce.org/api/Drupal%20Commerce/sites!all!modules!co...
Comment #14
giorgio79 commentedMaybe 8x?
Comment #15
giorgoskAnother way is outlined here
and it used php session variables to trigger html code in template
https://www.drupal.org/node/1243544#comment-4841992
Comment #16
anybodyI've created a sandbox module based on the idea in #10.
I think it's a general requirement to have a way (action) to execute JavaScript in Rules. It's helpful in several situations like for tracking tools and other stuff.
Please help me to check the security of the Proof-Of-Concept sandbox module and discuss it here: https://www.drupal.org/node/2418735
It would be great if a member of the Drupal Security Team could have a look at it.
The Sandbox Module can be found here: https://www.drupal.org/sandbox/anybody/2418721
Thanks a lot!