0

I have a node set up with a few workbench states. The first state, "Draft", should be used to allow someone to save the node without any required fields. The second state, "Submitted", should make the required fields active. Essentially, I'd like to use default required field behavior for all states except the first "Draft". Is this something I can do programmatically with a module?

0

Yes, this should be fairly easy to do with hook_form_<form-id>_alter():

<?php

function MODULENAME_form_page_node_form_alter(&$form, &$form_state) {   
  $target_field = "FIELDNAME";
  $workbench_state_required = "draft";

  $form[$target_field]['#states'] = array(
    'required' => array(
      ':input[name="workbench_moderation_state_new"]' => array(
        '!value' => $workbench_state_required
      ),
    )
  );
}

Just replace MODULENAME with the name of your custom module, and FIELDNAME with the field in question to toggle required or not (you can print the $form array in this function to work out the exact field name if you like).

For more information on the Form States API, visit: api.drupal.org.

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.