You are here

function webform_node_view in Webform 7.4

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

Implements hook_node_view().

2 calls to webform_node_view()
webform_block_view in ./webform.module
Implements hook_block_view().
webform_handler_field_form_body::render in views/webform_handler_field_form_body.inc
Render the field.

File

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

Code

function webform_node_view($node, $view_mode) {
  global $user;
  if (!variable_get('webform_node_' . $node->type, FALSE)) {
    return;
  }

  // If empty or a new node (during preview) do not display.
  if (empty($node->webform['components']) || empty($node->nid)) {
    return;
  }

  // If the webform is not set to display in this view mode, return early.
  // View mode of 'form' is exempted to allow blocks and views to force display.
  $extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
  if ($view_mode != 'form' && empty($extra_fields['webform']['visible'])) {
    return;
  }
  $submission = FALSE;
  $submission_count = 0;
  $page = node_is_page($node);
  $logging_in = FALSE;
  $total_limit_exceeded = FALSE;
  $user_limit_exceeded = FALSE;
  $closed = FALSE;

  // If a teaser, tell the form to load subsequent pages on the node page. A
  // special exception is made for this view mode only.
  if ($view_mode == 'teaser' && !isset($node->webform['action'])) {
    $query = array_diff_key($_GET, array(
      'q' => '',
    ));
    $node->webform['action'] = url('node/' . $node->nid, array(
      'query' => $query,
    ));
  }

  // When logging in using a form on the same page as a webform node, suppress
  // output messages so that they don't show up after the user has logged in.
  // See http://drupal.org/node/239343.
  if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
    $logging_in = TRUE;
  }
  if ($node->webform['status'] == 0) {
    $closed = TRUE;
    $enabled = FALSE;
    $allowed_roles = array();
  }
  else {

    // $enabled set by reference.
    $allowed_roles = _webform_allowed_roles($node, $enabled);
  }

  // Get a count of previous submissions by this user. Note that the
  // webform_submission_access() function may disable the page cache for
  // anonymous users if they are allowed to edit their own submissions!
  if ($page && webform_submission_access($node, NULL, 'list')) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $submission_count = webform_get_submission_count($node->nid, $user->uid);
  }

  // Check if this page is cached or not.
  $cached = drupal_page_is_cacheable();

  // Check if the user can add another submission based on the individual
  // submission limit.
  // -1: Submissions are never throttled.
  if ($node->webform['submit_limit'] != -1) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded and page cache is not active.
    // This prevents one anonymous user from generated a disabled webform page
    // for the cache, which would be shown to other anonymous users who have not
    // exceeded the limit.
    // Cached should be checked first to avoid the expensive limit check on
    // cached requests.
    if (!$cached && ($user_limit_exceeded = webform_submission_user_limit_check($node))) {
      $enabled = FALSE;
    }
  }

  // Check if the user can add another submission if there is a limit on total
  // submissions.
  // -1: Submissions are never throttled.
  if ($node->webform['total_submit_limit'] != -1) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded. The cache is irrelevant for
    // the total submission limit; when it is exceeded for one user, it is
    // exceeded for any other user.
    if ($total_limit_exceeded = webform_submission_total_limit_check($node)) {
      $enabled = FALSE;
    }
  }

  // Check if this user has a draft for this webform.
  $resume_draft = FALSE;
  if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {

    // Draft found - display form with draft data for further editing.
    if ($draft_sid = _webform_fetch_draft_sid($node->nid, $user->uid)) {
      module_load_include('inc', 'webform', 'includes/webform.submissions');
      $submission = webform_get_submission($node->nid, $draft_sid);
      $enabled = TRUE;
      $resume_draft = TRUE;
    }
  }

  // Avoid building the same form twice on the same page request (which can
  // happen if the webform is displayed in a panel or block) because this
  // causes multistep forms to build incorrectly the second time.
  $cached_forms =& drupal_static(__FUNCTION__, array());
  if (isset($cached_forms[$node->nid])) {
    $form = $cached_forms[$node->nid];
  }
  else {
    $form = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $resume_draft);
    $cached_forms[$node->nid] = $form;
  }

  // Remove the surrounding <form> tag if this is a preview.
  if (!empty($node->in_preview)) {
    $form['#type'] = 'markup';
  }

  // Print out messages for the webform.
  if (empty($node->in_preview) && !isset($node->webform_block) && !$logging_in) {
    theme('webform_view_messages', array(
      'node' => $node,
      'page' => $page,
      'submission_count' => $submission_count,
      'user_limit_exceeded' => $user_limit_exceeded,
      'total_limit_exceeded' => $total_limit_exceeded,
      'allowed_roles' => $allowed_roles,
      'closed' => $closed,
      'cached' => $cached,
    ));
  }

  // Add the output to the node.
  $node->content['webform'] = array(
    '#theme' => 'webform_view',
    '#node' => $node,
    '#page' => $page,
    '#form' => $form,
    '#enabled' => $enabled,
    '#visible' => $extra_fields['webform']['visible'],
    '#weight' => 10,
  );
}