2

Is there a way to unpublish a node if a user selects an option from a select list in field?

I have a field that has a list of cities. What I would like is if the user selects a certain city like London for example, the node is automatically unpublished so it can be moderated.

I have been trying with rules but every time I test my rule nothing happens so I'm sure there is something wrong with my rule or maybe there is an easier approach?

3
  • Have you turned on the verbose logging option for Rules? Apr 4, 2013 at 14:08
  • Do you mean debugging information?
    – ipwa
    Apr 4, 2013 at 14:23
  • Yes, that's what I mean. Apr 4, 2013 at 14:25

3 Answers 3

7

You could also hook on hook_node_presave() and check here the value of the field... No ?

mymodule_node_presave($node) {
if($node->yourfield[.....]['value'] == 'London') {
$node->state = '0';
}
}
2
  • This is a great solution, thank you, I think this might be the best way to solve the issue.
    – ipwa
    Apr 16, 2013 at 0:14
  • 1
    Use field_get_items to access values of fields.
    – Sam Becker
    Apr 28, 2013 at 5:37
7
+50

Did you test first to see if the "Entity has field" before doing the "Data comparison"?

When comparing fields it is necessary to first check that the entity has the field for Rules to pick them up. When comparing basic node attributes like Content Type you can omit this step.

Here's an example...

enter image description here

2
  • Voting for this answer, and I'd just mention that this would be in a rule you create for responding to the event 'Node: Before saving content,' and 'Node: After saving existing content.'
    – paul-m
    Apr 15, 2013 at 0:40
  • Since this is the solution I used, I will award the bounty.
    – ipwa
    Apr 16, 2013 at 0:14
-1

That can be done with some code in a custom module...

In the example below I'm assuming your content type is called article, and articles have a field called category, and when the category is Javascript then we unpublish the node.

First we need to modify the node form and add a new submit handler that fire after the one that saves the node (the order here is important):

function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
    if($form_id=='article_node_form') {
        $form['actions']['submit']['#submit'][] = 'article_node_form_custom_submit';
    }
}

Now we need to define our custom form submit handler:

function article_node_form_custom_submit($form, &$form_state) {
    // check if the category is Javascript
    if($form_state['values']['field_category']['und'][0]['value']=='Javascript') {
        // load the node
        $node = node_load($form_state['values']['nid']);
        // set the status to 0 to unpublish it
        $node->status = 0;
        // save the node
        node_save($node);
        // set a message to say what we've done
        drupal_set_message($node->title.' has been unpublished as the category was set to Javascript.');
    }
}

And that's all there is to it :)

2
  • Please don't edit your answer to copy another - it doesn't add any value to the site. Thanks :)
    – Clive
    Apr 12, 2013 at 11:39
  • Fair enough - I was just acknowledging that this is a better solution to the question that my initial one and just wanted to show a full example...
    – Felix Eve
    Apr 14, 2013 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.