1

I'm looking to show and hide form fields based on the current progress of a node using contributed modules like Workflow, Maestro or rules.

For example, I would like to show certain fields if a post is published and hide those same fields if it is in draft. I would like to avoid writing a custom module if possible.

Is there a simple way to interact with the Drupal 7 FormAPI to show and hide fields based on the status of a node using Rules, Workflow, Maestro or other contributed modules?

Also, is there an easy way to modify 'select lists' produced with the FormAPI using any of the noted plugins?

2

You might be able to piece something together using Conditional Fields, but a custom module (using hook_form_alter()) would definitely be the recommended path here, because it sounds relatively custom.

Conditional fields are simply hidden using JS, while it sounds like you might need to make it so people can't access the fields (so, #access property = FALSE) on the edit form in certain situations.

Rules doesn't have much integration with content editing forms; but you could probably set some field to a different value if the status of a node is changed from draft to published, or something like that, which could trigger something with Conditional Fields... but I think doing it in a custom module would be better in this case, and would definitely be more robust.

2
  • I don't think hook_form_alter is useful here. The question is about (not) viewing fields, not about editing. hook_form_alter will not be called in this case. – BetaRide Jan 24 '12 at 6:59
  • I would assume the OP would want the fields to not appear in either editing or viewing. Maybe implementing hook_field_access() would be the best bet to cover both situations. – geerlingguy Jan 25 '12 at 5:51
2

+1 for a new module, but hook_node_view_alter() makes more sense.

In fact I thought I'd try it out. This will hide a field called field_hide_me if the node is published:

/**
 * Implements hook_node_view_alter().
 *
 * If the node is published then we hide a field
 * called 'field_hide_me'.
 */
function if_published_node_view_alter(&$build) {
  // only for full view mode
  if ($build['#view_mode'] == 'full') {
    // check whether our field exists
    if (isset($build['field_hide_me'])) {
      // grab the node
      $node = $build['#node'];
      // status is 1 for published, 0 unpublished
      if ($node->status) {
        unset($build['field_hide_me']);
      }
    }
  }
}

Sandbox here: http://drupal.org/sandbox/Mile23/1415030

0

I don't know about aGUI only solution. You can of course implement hook_field_access. It allows to implement fine grained access control for fields.

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.