You are here

function form_builder_cache_difference in Form Builder 7

Same name and namespace in other branches
  1. 6 includes/form_builder.cache.inc \form_builder_cache_difference()
  2. 7.2 includes/form_builder.cache.inc \form_builder_cache_difference()

Compare the cached form with the original and return all changed elements.

@todo Implement this as a form object method.

Return value

An array of elements keyed by the element_id of the changed element. Each value contains an array of the 'original' and 'modified' elements.

File

includes/form_builder.cache.inc, line 58
form_builder.cache.inc Functions for temporary storage of form builder structures while editing.

Code

function form_builder_cache_difference($form_type, $form_id, $exclude_weight = TRUE) {
  $loader = FormBuilderLoader::instance();
  $modified_form = $loader
    ->fromCache($form_type, $form_id);
  if ($modified_form) {
    $original_form = $loader
      ->fromStorage($form_type, $form_id);
    $original_ids = $original_form
      ->getElementIds();
    $modified_ids = $modified_form
      ->getElementIds();

    // Find the union of IDs in both arrays.
    $element_ids = array_keys(array_flip($original_ids) + array_flip($modified_ids));

    // Build a list of all elements that have changed.
    $differences = array();
    foreach ($element_ids as $element_id) {
      $original = $original_form
        ->getElementArray($element_id);
      $modified = $modified_form
        ->getElementArray($element_id);
      if ($exclude_weight) {
        if (isset($original['#weight'])) {
          unset($original['#weight']);
        }
        if (isset($modified['#weight'])) {
          unset($modified['#weight']);
        }
      }
      if ($original != $modified) {
        $differences[$element_id] = array(
          'original' => $original,
          'modified' => $modified,
        );
      }
    }
    return $differences;
  }
  return FALSE;
}