2

My client has a system in which they allow nodes of certain content types to be created by authenticated users. The nodes are unpublished until they are reviewed by someone with the proper role and this all works correctly.

One of the requirements was allowing users to save a node as draft. I initially used the Save Draft module but found that was defaulting nodes to be published which caused problems. I worked around this by adding a hidden field called status that would change values based on which submit button was clicked via hooks.

Now, the issue comes when the node is reviewed. At that point, an admin will change the node to published. They would also like automatically like this hidden status field to change from "submitted" to "published". Normally, I would use a hook for this but through my testing, I can't seem to get form alter hooks to work in the admin section.

I suppose I have two questions here:

  1. Is there an easier way to achieve my end result here?
  2. Failing that, is there a way to automatically change the value of one field when a node is updated via the admin section?
2

Piece of cake, if you are willing to use the Rules module for this ... like so:

  • Rules Event: before saving content, which covers all variations of updates of nodes, and creations of new nodes (and does not require an explicit "entity save" action).
  • Rules Conditions:

    • Entity has field, for your hidden status field, that way the logic implemented via this rules only applies for nodes that use this field.
    • Node-unchanged has status unpublished.
    • Node-changed has status published.
  • Rules Action: Set a data value, for your hidden status field, to assign the value 'published'.

With the above in mind, have a look at this rule (in Rules export format), in which I'm using a status field with machine name field_status (and for which a value of "P" reflects "Published" status:

 "rules_change_value_of_hidden_field_on_node_publish" : {
    "LABEL" : "Change value of hidden field on node publish",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_status" } },
      { "NOT node_is_published" : { "node" : [ "node-unchanged" ] } },
      { "node_is_published" : { "node" : [ "node" ] } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-status" ], "value" : "P" } }
    ]
  }
}

If you adapt the machine name field_status to match your own field's machine name, together with the value used to reflect "Published" status, you should be able to import the above rule in your own site. After doing so ... you should be done!

  • This is perfect. I'm already using Rules for other stuff and completely forgot about it. Thanks! – lokisapocalypse Oct 1 '17 at 18:49
  • @lokisapocalypse merci for the feedback, and the accept! No other questions where possibly the Rules module could also be a solution? – Pierre.Vriens Oct 1 '17 at 18:53
1

You should be able to do this with a hook.

If you get frustrated with hooks, you can try the Rules module.

I would set it up something like this:

Event: When publishing a node

Conditions:

  • Node is of content type ABC
  • Status field is "Submitted"

Actions:

  • Change status field to "Published"
  • Entity save

These names are from memory, so the actual names will be a little different. Rules is not faster than writing hooks, but if you are having trouble with hooks, it can sometimes be an easier solution that doesn't require writing code.

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.