You are here

function context_ui_default_values in Context 6.2

Same name and namespace in other branches
  1. 6 context_ui/context_ui.admin.inc \context_ui_default_values()

Recursive function that intelligently populates default values in a form from a provided array of data.

Parameters

$form: A form API element to populate with default values.

$data: A keyed array of data that matches the tree structure of the form API branch it should populate.

Return value

A form API element populated with default values.

1 call to context_ui_default_values()
context_ui_form in context_ui/context_ui.admin.inc
Generates the omnibus context definition editing form. Note: submission and validation handlers are in context_ui.admin.inc

File

context_ui/context_ui.admin.inc, line 114

Code

function context_ui_default_values($form, $data) {
  if (!empty($form['#tree'])) {
    foreach (element_children($form) as $id) {
      if (isset($data[$id])) {
        $form[$id] = context_ui_default_values($form[$id], $data[$id]);
      }
    }
  }
  else {
    if (is_array($data) && $form['#type'] == 'checkboxes') {
      $form['#default_value'] = array();
      foreach ($data as $value) {
        $form['#default_value'][$value] = $value;
      }
    }
    else {
      if (is_array($data) && $form['#type'] == 'select' && $form['#multiple'] == TRUE) {
        $form['#default_value'] = array();
        foreach ($data as $value) {
          $form['#default_value'][] = $value;
        }
      }
      else {
        if (is_array($data) && $form['#type'] == 'textarea') {
          $form['#default_value'] = implode("\n", $data);
        }
        else {
          if (is_array($data) && $form['#type'] == 'textfield') {
            $form['#default_value'] = implode(",", $data);
          }
          else {
            $form['#default_value'] = is_array($data) ? current($data) : $data;
          }
        }
      }
    }
  }
  return $form;
}