You are here

function webform_node_view in Webform 7.3

Same name and namespace in other branches
  1. 6.3 webform.module \webform_node_view()
  2. 7.4 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 1441
This module provides a simple way to create forms and questionnaires.

Code

function webform_node_view($node, $view_mode) {
  global $user;
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
    return;
  }

  // Set teaser and page variables a la Drupal 6.
  $teaser = $view_mode == 'teaser';
  $page = arg(0) == 'node' && arg(1) == $node->nid;

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

  // Do not include the form in the search index if indexing is disabled.
  if (module_exists('search') && $view_mode == 'search_index' && !variable_get('webform_search_index', 1)) {
    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;
  }
  $info = array();
  $submission = array();
  $submission_count = 0;
  $enabled = TRUE;
  $logging_in = FALSE;
  $total_limit_exceeded = FALSE;
  $user_limit_exceeded = FALSE;
  $closed = FALSE;
  $allowed_roles = array();

  // If a teaser, tell the form to load subsequent pages on the node page.
  if ($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;
  }
  else {

    // Check if the user's role can submit this webform.
    if (variable_get('webform_submission_access_control', 1)) {
      foreach ($node->webform['roles'] as $rid) {
        $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
      }
      if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
        $enabled = FALSE;
      }
    }
    else {

      // If not using Webform submission access control, allow for all roles.
      $allowed_roles = array_keys(user_roles());
    }
  }

  // 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 = $user->uid == 0 && (variable_get('cache', 0) || drupal_page_is_cacheable() === FALSE);

  // Check if the user can add another submission.
  // -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.
    if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
      $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 and page cache is not active.
    if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
      $enabled = FALSE;
    }
  }

  // Check if this user has a draft for this webform.
  $is_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;
      $is_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, $is_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,
      'teaser' => $teaser,
      '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,
    '#teaser' => $teaser,
    '#page' => $page,
    '#form' => $form,
    '#enabled' => $enabled,
    '#weight' => 10,
  );
}