Adds a setting to hide a required controlled field when there is only one option in the select list. Only tested with a text field at this stage.

CommentFileSizeAuthor
dependent_fields-hide_single.patch5.34 KBimclean

Comments

smoothify’s picture

Thanks for the patch.

I like the idea of this option, I wonder, however, if it would be good to display a visual indication that this item has automatically been selected.

imclean’s picture

Good idea, that could be an option as well. For our current use we'd like to not show anything to the user if they don't have to choose. Although, simply disabling the option would show the option which has been selected.

imclean’s picture

Status: Needs review » Needs work

Ok this patch doesn't quite work, presumably because a select field can't be changed to a hidden (straight text) field and be expected to pass on the value in the correct format.

Will keep at it, any suggestions welcome.

inferno_msk’s picture

I can not properly configure the module. can not do so when you click a dropdown list with a choice of values.
Use the field - the category.

imclean’s picture

Hiding it via CSS is what we've gone with. I've done this using a custom module but it could potentially be included in Dependent Fields (taking #1 into account).

An additional option "Not sure" is offered when there's more than one option to present to the user.

/**
 * Implementation of hook_form_alter().
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case "MY_FORM_ID":   
      $form['#after_build'][] = 'MYMODULE_MYFORM_after_build';  
      break;
  }
}

/*
 * Modify CCK form elements on MYFORM
 */
function MYMODULE_MYFORM_after_build($form, &$form_state) {
  // Hide field when only one option
  $options = $form['field_controllerfield_ahah_wrapper']['field_controlledfield']['value']['#options'];
  if ((count($options) == 1) && end($options) != '- None -') {            
    $form['field_controllerfield_ahah_wrapper']['field_controlledfield']['#prefix'] .= '<div style="display: none;">';
    $form['field_controllerfield_ahah_wrapper']['field_controlledfield']['#suffix'] .= '</div>';
  } else {
    $form['field_controllerfield_ahah_wrapper']['field_controlledfield']['value']['#options']['Not sure'] = 'Not sure';       
  }
  return $form;
}
smoothify’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)

I've moved on from Drupal Development work now, and so unfortunately i need to abandon this module. If anyone is interested on reviving it, please let me know through my contact form.