You are here

function _msnf_hide_fields in Multistep Nodeform 7

Same name and namespace in other branches
  1. 6 msnf.module \_msnf_hide_fields()

Hide all fields that are not associated to the current step.

Parameters

<array> $form: Form to hide the fields from.

<array> $form_state: Current form state.

1 call to _msnf_hide_fields()
msnf_form_node_form_alter in ./msnf.module
Implements hook_form_FORM_ID_alter().

File

./msnf.module, line 1258
Main functions for module "Multistep Nodeform".

Code

function _msnf_hide_fields(&$form, &$form_state) {
  if (($current_step = _msnf_form_step_get_current($form, $form_state)) === FALSE) {

    // Step not found. Do nothing.
    return;
  }

  // Get the names of all form children.
  $form_elements = element_children($form);

  // Hide all elements that do not belong to the current step.
  foreach ($form_elements as $element_name) {
    if ('title' !== $element_name && (empty($form[$element_name]['#type']) || in_array($form[$element_name]['#type'], array(
      'value',
      'actions',
      'token',
      'hidden',
    )))) {

      // Value elements and actions needs to be present on each step.
      continue;
    }
    if (isset($form['#step_children'][$element_name]) && $form['#step_children'][$element_name] == $current_step->step_name) {

      // Element is a child of the current step.
      continue;
    }
    if (!empty($form[$element_name]['#group']) && in_array($form[$element_name]['#group'], $current_step->children)) {

      // Element is part of a group within this step. Display the element.
      continue;
    }

    // Maybe the element is in a field group?
    if (module_exists('field_group') && isset($form['#fieldgroups'])) {
      foreach ($form['#fieldgroups'] as $group_name => $group) {
        if (isset($form['#step_children'][$group_name]) && $form['#step_children'][$group_name] == $current_step->step_name && _msnf_fieldgroup_has_element($form['#fieldgroups'], $group, $element_name)) {

          // Element is a child of a fieldgroup which is a child of this step.
          continue 2;
        }
      }
    }

    // Hide the element.
    $form[$element_name]['#access'] = FALSE;

    // Make sure the element is not required at this time!
    _msnf_element_unset_required($form[$element_name]);
  }

  // Todo: hide preview on step change.
}