1

I need this in Rules:

When a node is being created (before the node is saved), and certain conditions are true, the user can't create the node, and an error message appears instead.

I have a rule that is working, and the error message is displaying. But the node is still created.

Is there a way to prevent node creation in rules?

Note: I'm using Drupal 7. I selected the Action "Show error message" directly on rules.

  • Please provide which version of Drupal you're using. Also are you showing error messages using drupal_set_message or form_set_error? – Yogesh Nov 21 '16 at 11:01
  • I'm using Drupal 7. I selected the Action "Show error message" directly on rules... – Drii7 Nov 21 '16 at 11:48
0

I solved this by using the drupal_goto function.

0

if you are using a react on event "Before saving content" a node will be saved anyway. There is a way to use "After saving new content" option and node_delete($node->nid) ("Execute custom php code option" in action to add) if some conditions are met. But it is too overcoded way. So if you are using Drupal 7 you can try to use hook_form_FORM_ID_alter() function without creating any rules. Additional validation can prevent node creation.

function your_module_form_YUOR_NODE_TYPE_FORM_alter(&$form, &$form_state, $form_id) 
{
   array_unshift($form['#validate'], 'your_new_validation_function');
}
function your_new_validation_function($form, &$form_state) {

if ($some_condition == 'some_condition_variable') {
// Do some things here
}
else {
form_set_error('', 'Form Error, node was not created!');
}

if ($form_state['values']['some_field'] == 'some condition') {
//Do some staff here
}
else {
form_set_error('some_field', 'Form Error, node was not created!');
}

}
  • Part of the question is "Is there a way to prevent node creation in rules?". So IMO that implies (a) it must use the Rules module and (b) deleting a node again after it got created does not prevent the node creation. Moreover, do you really need custom PHP code (stored in your database ...) to delete a node when using Rules? – Pierre.Vriens Nov 22 '16 at 8:18
  • Yes, you are right, but i'm just trying to help to resolve this issue with my own experience as rule options are limited from the box. Maybe the best way is to create own custom rule for special conditions, but more details are needed. Otherwise one can use node or entity hooks. – Andrew Nim Nov 22 '16 at 8:45
  • Andrew, looks like we have a different opinion ... IMO Rules-out-of-the-box is not at all limited. Depending on which other modules you're working with, it's up to the other module to "integrate" that module with Rules. If the other module takes advantage of the entity API (such as "Message", "Group", "Goals", etc), then rules-out-of-the-box has more or less everything that's needed ... provided you have sufficient experience about what's possible in Rules just using its entity API facilities ... – Pierre.Vriens Nov 22 '16 at 9:33
0

Out-of-the Rules module-box, I think there is no solution to "prevent node creation in rules?" (as in your question). However, without using custom code and custom PHP in your Rules conditions or actions and without deleting the node after it got created, you may get it to work if you also use the Rules Forms Support module.

More details about Rules Forms Support (from its project page):

Rules Forms Support provides a Rules based method for controlling forms and their elements. The module can be used to alter any form provided by Drupal core or contributed modules. Change element titles, descriptions, weights, and more, or validate form data and set form errors. Rules forms saves time and cost by affording site builders the ability to monitor and alter forms without the need to implement a custom module.

For help getting started with Rules Forms Support, please see the module documentation.

Features

  • Activate events for form creation, validation, and submission on a form-by-form basis.
  • Target individual form elements or the entire form in conditions and actions.
  • Manipulate the attributes of a form element like title, description, weight, and more.
  • Validate the values of form elements during form validation.
  • Set form errors when rule-based validation fails.
  • Redirect users to a different page upon viewing or submitting a form.
  • Examine the attributes of elements in active forms with the element inspection tool.

Especially the "Set form errors when rule-based validation fails" should help to address your question.

More details about how to use this module can be found in the community documentation "Altering & validating forms".

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.