You are here

function form_builder_webform_save_node in Form Builder 6

Save the current Form Builder interface changes for a webform node.

1 call to form_builder_webform_save_node()
form_builder_webform_save_form_submit in modules/webform/form_builder_webform.module
Submit handler; save the current Form Builder interface changes.

File

modules/webform/form_builder_webform.module, line 84
Form Builder integration for the Webform module.

Code

function form_builder_webform_save_node($node) {
  $form_cache = form_builder_cache_load('webform', $node->nid);
  $element_ids = form_builder_preview_prepare($form_cache, 'webform', $node->nid);

  // Remove components if deleted and calculate the highest in-use CID.
  $max_cid = 0;
  foreach ($node->webform['components'] as $component) {
    $element_id = 'cid_' . $component['cid'];
    $cid = $component['cid'];

    // Max CID is used in the creation of new components, preventing conflicts.
    $max_cid = max($max_cid, $cid);

    // Remove components from the $node that have been removed in the UI.
    if (!in_array($element_id, $element_ids)) {
      if (isset($node->webform['components'][$cid])) {
        unset($node->webform['components'][$cid]);
      }
    }
  }

  // Update any new/updated components in the node record.
  foreach ($element_ids as $element_id) {
    $component = form_builder_webform_get_component($node, $element_id, $form_cache);
    if ($component) {
      if (empty($component['cid'])) {
        $cid = ++$max_cid;
        $component['cid'] = $cid;

        // Reassign the component ID to the form so that future elements that
        // may depend on this one set their parent ID (pid) properly.
        $element = form_builder_get_element($form_cache, $element_id);
        $element['#webform_component']['cid'] = $cid;
        form_builder_set_element($form_cache, $element);
      }
      else {
        $cid = $component['cid'];
      }
      $node->webform['components'][$cid] = $component;
    }
  }

  // Save the node itself to update components and allow other modules to
  // respond to any changes. The Form Builder cache is intentionally left in
  // place so other modules can check it for changes also.
  node_save($node);

  // Remove the cached form_builder form.
  form_builder_cache_delete('webform', $node->nid);
}