0

I am trying to use the states to change another field depending on the moderation state, I tried this, but didn't catch it.

$form['schedule_publish_date'] ['#states'] =  array(
 'invisible' => array(
  ':input[name="moderation_state"]' => array('value' =>"published"),
 ),

I'm not sure what the correct field name of the moderation state is, this is the source code <div class="js-form-item form-item js-form-type-select form-type-select js-form-item-moderation-state-0 form-item-moderation-state-0"> <label for="edit-moderation-state-0">Moderation state</label> <select data-drupal-selector="edit-moderation-state-0" aria-describedby="edit-moderation-state-0--description" id="edit-moderation-state-0" name="moderation_state[0]" class="form-select"><option value="draft" selected="selected">Draft</option><option value="needs_review">Needs Review</option><option value="approved_to_be_published">Approved to be published</option><option value="published">Published</option></select> <div id="edit-moderation-state-0--description" class="description"> The moderation state of this piece of content. </div> </div>

1

Drupal API: function drupal_process_states

Example A where the source fieldtype is a textfield

$form['moderation_state'] = [
  '#type'  => 'textfield',
  '#title' => ('Some title'),
];

$form['schedule_publish_date'] = [
  '#type'   => 'textfield',
  '#title'  => ('Invisible if ...'),
  '#states' => ['invisible' => [':input[name="moderation_state"]' => ['value' => "published"],],],
];

Example B where the source fieldtype is a select-field

$form['moderation_state'] = array(
  '#type'          => 'select',
  '#title'         => t('Some title'),
  '#options'       => array('unpublished'=>'unpublished', 'published'=>'published'),
  '#default_value' => 0,
);

$form['schedule_publish_date'] = [
  '#type'   => 'textfield',
  '#title'  => ('Invisible if ...'),
  '#states' => ['invisible' => [':input[name="moderation_state"]' => ['value' => "published"],],],
];
| improve this answer | |
  • thanks but the moderation state comes from the work flow module, so the code for it is different , I can't really tell what the input name for it is. – Diana Jul 6 '17 at 17:30
  • Should be "moderation_state[0]" – Calar Jul 6 '17 at 17:36
  • 1
    $form['schedule_publish_date'] = [ '#type' => 'textfield', '#title' => ('Invisible if ...'), '#states' => ['invisible' => [':input[name="moderation_state[0]"]' => ['value' => "published"],],], ]; – Calar Jul 6 '17 at 17:37

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.