You are here

function multiform_sharedfields_set_post_values in Multiple forms 7.2

Set values for sharedfields based on $_POST array.

1 call to multiform_sharedfields_set_post_values()
multiform_sharedfields_process in ./multiform.module
Process sharedfields. Hide dependent sharedfields at form rendering and set their values in _POST after form submission.

File

./multiform.sharedfields.inc, line 37

Code

function multiform_sharedfields_set_post_values($multiform, $sharedfields_info) {
  if (!empty($_POST) && isset($multiform['#multiform_id']) && isset($sharedfields_info[$multiform['#multiform_id']])) {
    $multiform_id = $multiform['#multiform_id'];
    foreach ($sharedfields_info[$multiform_id] as $item) {

      // Get array path to the element in $_POST.
      $parents_base = $item['#base']['#parents'];
      array_unshift($parents_base, 'multiform', $item['#base']['#index']);

      // Get base value
      $key_exists = NULL;
      $value = drupal_array_get_nested_value($_POST, $parents_base, $key_exists);

      // If base key exist in $_POST array, set dependent value.
      if ($key_exists) {
        $parents_dependent = $item['#dependent']['#parents'];
        array_unshift($parents_dependent, 'multiform', $item['#dependent']['#index']);
        drupal_array_set_nested_value($_POST, $parents_dependent, $value, TRUE);
      }
    }
  }
}