In my project, i created a node programmatically using the following code

    function create_my_node() {

$node = new stdClass();
  $node->type = 'custom_content';
  $node->uid = 1;
  $node->title    = 'new node';
  $node->language = LANGUAGE_NONE;
  $node->field_period [$node->language][0]['value'] = $period;
  $node->field_numbers[$node->language][0]['value'] = $number;
  $node->field_numbers[$node->language][0]['format'] = NULL;
  $node= node_submit($node);

  node_save($node); }

This works fine.

I then added a rule in the Rules module that says: "After saving new content" and content is "custom content", display a message on the site.

This works fine when I create content from the drupal admin interface, but it doesn't trigger the rule when the node is created through my code.

Please what could i be missing?

  • What is the condition of the rule? – Abi أب Sep 26 '14 at 13:26

It might be a permission related issue, one post I found on drupal site https://drupal.org/node/929936 contains the discussion about the same.

  • Thanks. But I believe my permissions are set right. Also, "user 1/admin" is the author of the content type and not anonymous user. – seyi Sep 28 '13 at 19:07
  • Tried it but it does not work. Something i noticed... content created programmatically does not appear in www.example.com/admin/reports/dblog. Also there is a "new" written in red in front of content created programmatically at www.example.com/node/#overlay=admin/content. Am not sure if its a pointer or clue. – seyi Sep 28 '13 at 19:33
  • The "new" written next to the created content signifies that the node is getting created successfully, so the problem doesn't seems to be with the node creation functionality as code seems to be fine. Can you once check the rule which is supposed to be triggered while node gets created, – Ankit Agrawal Sep 28 '13 at 22:01

@seyi I just checked your codes on my local installation with some changes as few we do not required to make this test,so code with which I test is.

function save_my_node() {
  $node = new stdClass();
  $node->type = 'page';
  $node->uid = 1;
  $node->title    = 'new node';
  $node->language = LANGUAGE_NONE;
  $node->body = "test";
  $node= node_submit($node);

  // setting message using drupal_set_message();
  drupal_set_message("your node is created");
  • As you see use of dupal_Set_message();
  • it is working in my case.
  • Also I tried creating a rule with

    • Event as "After saving new content of type Basic page"
    • Condition as none.
    • Action as "Show a message on the site" the message provided in the actions is alsomworking fine in my case.

    I'll recommend debugg codes for node saving,try using jook_node_insert(); to custom track of node save process and debug it.

  • Thanks. Still does not work. Also in my case, there are some other actions I want to trigger upon the event so cant use drupal_set_message. – seyi Sep 29 '13 at 5:58
  • 1
    Are you able to see drupal messages at other places, theirs might be a reason that you do not $messages, variable in your page template. In order to check rules are working or not change action of rule to add some simple action and if it's working or not. – arpitr Sep 29 '13 at 6:21
  • Thanks! Rule to send email upon creation of node works. So that means Rules is actually being triggered. – seyi Sep 29 '13 at 8:06
  • You are half done solving it, according to me your next step should be checking if drupal_set_message() is working, as a quick way to check it try violating any validation rule and see if you getting the message – arpitr Sep 29 '13 at 8:18
  • Try to add $node->is_new = 1 – pico34 Sep 30 '13 at 14:55

Did you try to enable debug mode in Rules. ? It might help in debugging if there is any issue on Rules side.

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.