Search code examples
javascriptdrupaldrupal-7drupal-rules

How to trigger a drupal rule from a javascript code


I don't know how to trigger drupal rule from JavaScript code.

      lowerLayer[image.feature_nid].on("dragend", function() {
               var position = kineticImage.getPosition();
               var layerPosition = this.getPosition();
               var slotNid = kineticImage.slot_nid;
               positionX = position.x + layerPosition.x;
               positionY = position.y + layerPosition.y;
               //Here I want to call drupal rule
        });  

I commented the line where I need to trigger drupal rule. Can I call a drupal rule with these parameters (slotNid,positionX,positionY) like I can call some function (someFunction(slotNid,positionX,positionY).

Any solutions are welcome.


Solution

  • As mentioned by Clive, You can achieve this by ajax call.
    You need to setup page callback under menu hook in your custom module & there you can mention the function to call on page request.

    Example:
    // this code will go under menu hook
    $items['ajax/rule1.1'] = array(
        'title' => 'Rule 1.1', 
        'page callback' => 'custom_rule_trigger', 
        'access arguments' => array('access content'), 
        'file' => 'custom_rules.inc',
      );
    
    // then you can write php code which you want to execute under custom_rule_trigger function in custom_rules.inc file.
    function custom_rule_trigger(){
    // write your rules here.. 
    }
    

    In javascript, you can call www.example.com/ajax/rule1.1 url to trigger your rule.

    Hope this will help.