You are here

function form_builder_examples_export_recurse in Form Builder 7

Same name and namespace in other branches
  1. 6 examples/form_builder_examples.module \form_builder_examples_export_recurse()
  2. 7.2 examples/form_builder_examples.module \form_builder_examples_export_recurse()

Recursive function for pretty-printing of FAPI arrays.

1 call to form_builder_examples_export_recurse()
form_builder_examples_export in examples/form_builder_examples.module

File

examples/form_builder_examples.module, line 436
form_builder_examples.module Sample implementations of form_builder.

Code

function form_builder_examples_export_recurse($form, $parents = array()) {
  $output = '';

  // Sort this level of the array according to weight.
  uasort($form, 'element_sort');

  // Print out this parent element and it's properties.
  $properties = element_properties($form);
  $omit = array(
    '#form_builder',
    '#key',
  );
  if (count($properties)) {
    $output .= form_builder_examples_export_variable_name($parents) . " = array(\n";
    foreach (element_properties($form) as $property) {
      if (!in_array($property, $omit)) {
        if (is_array($form[$property])) {
          $output .= "  '" . $property . "' => array(\n";
          foreach ($form[$property] as $key => $value) {
            if ($property == '#options') {
              $output .= "    '" . $key . "' => t('" . $value . "'),\n";
            }
            else {
              $output .= "    '" . $key . "' => '" . $value . "',\n";
            }
          }
          $output .= "  ),\n";
        }
        else {
          if ($property == '#title' || $property == '#description') {
            $output .= "  '" . $property . "' => t('" . str_replace("'", "\\'", $form[$property]) . "'),\n";
          }
          else {
            $output .= "  '" . $property . "' => '" . str_replace("'", "\\'", $form[$property]) . "',\n";
          }
        }
      }
    }
    $output .= ");\n";
  }
  else {
    $output .= form_builder_examples_export_variable_name($parents) . " = array();\n";
  }
  foreach (element_children($form) as $key) {
    $parents[] = $key;
    $output .= form_builder_examples_export_recurse($form[$key], $parents);
    array_pop($parents);
  }
  return $output;
}