I need to do a simple thing. Write a rule that edits some elements of the node, as soon as it is created. Unfortunately Drupal doesn’t like that and crashes whenever you try to save the node within a rule that was triggered by "After saving new content".

Event: After saving new content

Conditions: ...

Actions: Set a data value, with Parameter: Data: [node:field-workflow...], Value: 1

The above results in:

PDOException: SQLSTATE[23000]: Integrity constraint violation:

So far I managed to walk-around that with hook_forms, changing the workflow in my portal (so I never have to edit the node just after it is created)

Things get really interesting when I try to use "Execute custom PHP code" action which will change the node value I want:

  1. node_save will result in drupal crushing
  2. db_update - seems to work most of the time...
  3. db_insert - sometimes works (works for superuser only)

Unfortunately all my hacks don't work for all my problems. Anyone has any idea how else I can address this situation?

  • Is there a reason you can't use the "Before saving content" event? – Clive Apr 24 '13 at 15:20
  • Clive, it works!!! I always thought that if you use Before saving content, then you don't have access to the information that was just entered into forms (why else having this event). Now I know I was wrong. Thanks a lot. There are some realy creazy ways of dealing with this problem on internet but no-one ever suggested just changing to "Before saving content". – Pawel G Apr 24 '13 at 16:03

Have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module works ...).

A possible compromise to avoid these kind of issues, is to transform what you're trying to do in your Rules Action (related to Set a data value) in a Rules Component, so that your Rule "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component. For example after only a few seconds or minutes later on (if your cron job runs frequent enough, otherwise it'll be next time cron runs). This compromise will ensure that, at the time the Rules Component is executed, the "Save new content" for sure completed, and your new content will have been created as expected.

PS: I'm assuming this question is about D7.

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.