You are here

function variable_form_element_array in Variable 7

Same name and namespace in other branches
  1. 7.2 variable.form.inc \variable_form_element_array()

Build array form element

1 string reference to 'variable_form_element_array'
variable_variable_type_info in ./variable.variable.inc
Implements hook_variable_type_info().

File

./variable.form.inc, line 39
Variable API module. Form library.

Code

function variable_form_element_array($variable, $options = array()) {

  // This will be possibly a fieldset with tree value
  $element = variable_form_element_default($variable, $options);

  // We may have a multiple element base that will default to plain textfield
  $item = $variable['repeat'];
  $value = variable_get_value($variable, $options);

  // Compile names and defaults for all elements
  $names = $defaults = array();
  if (!empty($variable['multiple'])) {

    // If we've got a multiple value, it will be an array with known elements
    $names = $variable['multiple'];
  }
  else {

    // Array with unknown elements, we add an element for each existing one
    $names = $value ? array_combine(array_keys($value), array_keys($value)) : array();
  }

  // Now build elements with the right names
  foreach ($names as $key => $title) {
    if (isset($value[$key]) && is_array($value[$key])) {

      // This element is an array, we cannot edit it but we need to add it to the form
      $element[$key] = array(
        '#type' => 'value',
        '#value' => $value[$key],
      );
      $element['variable_element_array_' . $key] = array(
        '#type' => 'item',
        '#title' => $title,
        '#markup' => variable_format_array($value[$key]),
      );
    }
    else {
      $element[$key] = $item['element'] + array(
        '#title' => $title,
        '#default_value' => isset($value[$key]) ? $value[$key] : '',
      );
    }
  }
  return $element;
}