Jetson John
Jetson John

Reputation: 3829

How to prevent creating a new node if the referenced node is closed?

I have two content type "Idea" and "Challenge". If I create an idea with a reference to a closed challenge then I need to prevent the idea from saving. Is it possible to do this using Rules?

Upvotes: 0

Views: 494

Answers (1)

user3563097
user3563097

Reputation: 179

You can do it with rules however I would do it programmatically.

In rules you need to set up a rule st like 'pre save' where you set up your conditions than drop a form error.

Programmatically: Implement a hook_form_alter() in which you should have a form validation that calls a function in which you validate.

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7

EXAMPLE

function YOURMODULE_form_alter (&$form, &$form_state, $form_id) {
  if ($form_id == 'YOURCHALLANGENODETYPENAME_node_form') {
    $form['#validate'][] = 'YOURMODULE_form_validate';

    }
}

function YOURMODULE_form_validate ($form, &$form_state) {
  // load your idea here st like:
  $idea = entity_load_single( 'node', $form[YOURNODEREFERENCEID]);
  if ($idea->status == 0) {
    form_set_error ('YOURNODEREFERENCEFIELDNAME', t('ERROR_MESSAGE_TEXT'));
  }
}

Upvotes: 0

Related Questions