You are here

function webform_prepare in Webform 5

Implementation of hook_prepare(). This function is called before the display of webform_form(). Rather than a typical usage of hook_prepare, in webform it is used to update the contents of the $node object after changes have been made to the node, such as adding a new component or deleting an existing component. The node is altered as necessary, then the user is returned to the display of webform_form() with the changes visible. The changes to the node are not permanent until the user submits the form.

1 call to webform_prepare()
webform_view in ./webform.module
Implementation of hook_view().

File

./webform.module, line 414

Code

function webform_prepare(&$node) {
  $op = $_POST['op'];
  switch ($op) {
    case t('Delete Selected'):

      // Re-add existing components.
      if (isset($_POST['webformcomponents'])) {
        $node->webformcomponents = _webform_components_decode($_POST['webformcomponents']);
      }

      // Delete an existing component.
      $delete_cid = $_POST['components']['selected_component'];
      if (is_array($node->webformcomponents)) {
        $new_parent = $node->webformcomponents[$delete_cid]['parent'];

        // Fix-up any children of the deleted component.
        foreach ($node->webformcomponents as $cid => $component) {
          if ($component['parent'] == $delete_cid) {
            $node->webformcomponents[$cid]['parent'] = $new_parent;
          }
        }
        unset($node->webformcomponents[$delete_cid]);
      }
      break;
    case t('Done'):

      // Overwrite the database components with any saved in the POST array.
      $node_components = array();
      if (isset($_POST['node']['webformcomponents'])) {
        $node_components = _webform_components_decode($_POST['node']['webformcomponents']);
      }
      $node_components[$_POST['field']['cid']] = $_POST['field'];

      // Add saved values of the node.
      $edit = array_merge((array) $node, (array) $_POST['node']);
      $node = (object) $edit;
      $node->webformcomponents = $node_components;
      $node->selected_component = $_POST['field']['cid'];

      // Validate the field form.
      webform_edit_field_form_prepare_validate($_POST['field'], $node);
      $errors = form_get_errors();
      if (!empty($errors)) {
        $_POST['op'] = t('Edit Selected');
        $output = drupal_get_form('webform_edit_field_form', $node);
        print theme('page', $output);
        exit;
      }

      // Display editted message.
      drupal_set_message(t('The form component has been changed. Remember to press Submit on the bottom of this form to save your changes.'));
      break;
    case t('Preview'):
      if (isset($_POST['webformcomponents'])) {
        $node->webformcomponents = _webform_components_decode($_POST['webformcomponents']);
      }
      break;
  }

  // Make sure the submission limiter is correctly set.
  if ($_POST['enforce_limit'] === 'no') {
    $node->submit_limit = '-1';
    $node->submit_interval = '157784630';

    // 5 years, close enough to 'ever'.
  }
}