You are here

function webform_node_insert in Webform 7.3

Same name and namespace in other branches
  1. 6.3 webform.module \webform_node_insert()
  2. 7.4 webform.module \webform_node_insert()

Implements hook_node_insert().

2 calls to webform_node_insert()
webform_ensure_record in ./webform.module
Utility function to ensure that a webform record exists in the database.
webform_node_update in ./webform.module
Implements hook_node_update().

File

./webform.module, line 1120
This module provides a simple way to create forms and questionnaires.

Code

function webform_node_insert($node) {
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
    return;
  }

  // If added directly through node_save(), set defaults for the node.
  if (!isset($node->webform)) {
    $node->webform = webform_node_defaults();
  }

  // Do not make an entry if this node does not have any Webform settings.
  if ($node->webform == webform_node_defaults() && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
    return;
  }
  module_load_include('inc', 'webform', 'includes/webform.components');
  module_load_include('inc', 'webform', 'includes/webform.emails');

  // Insert the webform.
  $node->webform['nid'] = $node->nid;
  $node->webform['record_exists'] = (bool) drupal_write_record('webform', $node->webform);

  // Insert the components into the database. Used with clone.module.
  if (isset($node->webform['components']) && !empty($node->webform['components'])) {
    foreach ($node->webform['components'] as $cid => $component) {

      // Required for clone.module.
      $component['nid'] = $node->nid;
      webform_component_insert($component);
    }
  }

  // Insert emails. Also used with clone.module.
  if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
    foreach ($node->webform['emails'] as $eid => $email) {
      $email['nid'] = $node->nid;
      webform_email_insert($email);
    }
  }

  // Set the per-role submission access control.
  foreach (array_filter($node->webform['roles']) as $rid) {
    db_insert('webform_roles')
      ->fields(array(
      'nid' => $node->nid,
      'rid' => $rid,
    ))
      ->execute();
  }

  // Flush the block cache if creating a block.
  if ($node->webform['block']) {
    block_flush_caches();
  }
}