You are here

function webform_block_view in Webform 7.4

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

Implements hook_block_view().

File

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

Code

function webform_block_view($delta = '') {

  // Load the block-specific configuration settings.
  $webform_blocks = webform_variable_get('webform_blocks');
  $settings = isset($webform_blocks[$delta]) ? $webform_blocks[$delta] : array();
  $settings += array(
    'display' => 'form',
    'pages_block' => 1,
    'confirmation_block' => 0,
  );

  // Get the node ID from delta.
  $nid = drupal_substr($delta, strrpos($delta, '-') + 1);

  // Load node in current language.
  if (module_exists('translation')) {
    global $language;
    if (($translations = translation_node_get_translations($nid)) && isset($translations[$language->language])) {
      $nid = $translations[$language->language]->nid;
    }
  }

  // The webform node to display in the block.
  $node = node_load($nid);

  // Return if user has no access to the webform node.
  if (!node_access('view', $node)) {
    return;
  }

  // This is a webform node block.
  $node->webform_block = TRUE;
  $node->webform['confirmation_block'] = $settings['confirmation_block'];

  // If not displaying pages in the block, set the #action property on the form.
  if ($settings['pages_block']) {
    $node->webform['action'] = FALSE;
  }
  else {
    $query = array_diff_key($_GET, array(
      'q' => '',
    ));
    $node->webform['action'] = url('node/' . $node->nid, array(
      'query' => $query,
    ));
  }

  // Generate the content of the block based on display settings.
  $content = array();
  if ($settings['display'] == 'form') {
    webform_node_view($node, 'form');
    if (isset($node->content['webform'])) {
      $content = $node->content['webform'];
      if (!$node->content['webform']['#visible']) {

        // If the webform form is only shown in a block and not as within the
        // node, remove the content from the node.
        unset($node->content['webform']);
      }
    }
  }
  else {
    $content = node_view($node, $settings['display']);
  }

  // Check for an in-block confirmation message.
  if (isset($_SESSION['webform_confirmation'][$nid])) {
    if ($_SESSION['webform_confirmation'][$nid]['confirmation_page']) {

      // Replace form with confirmation page.
      $content = array(
        '#theme' => array(
          'webform_confirmation_' . $node->nid,
          'webform_confirmation',
        ),
        '#node' => $node,
        '#sid' => $_SESSION['webform_confirmation'][$nid]['sid'],
      );
    }
    elseif (strlen(trim(strip_tags($node->webform['confirmation'])))) {

      // Display confirmation link drupal status messages, but in the block.
      $message = webform_replace_tokens($node->webform['confirmation'], $node, webform_get_submission($nid, $_SESSION['webform_confirmation'][$nid]['confirmation_page']), NULL, $node->webform['confirmation_format']);
      $content = array(
        'confirmation_message' => array(
          '#markup' => "<div class=\"messages status webform-confirmation\">\n" . '<h2 class="element-invisible">' . t('Status message') . "</h2>\n" . $message . "</div>\n",
          '#weight' => -1,
        ),
        'webform_view' => $content,
      );
    }
    unset($_SESSION['webform_confirmation'][$nid]);
    if (empty($_SESSION['webform_confirmation'])) {
      unset($_SESSION['webform_confirmation']);
    }
  }

  // Add contextual links for the webform node if they aren't already there.
  if (!isset($content['#contextual_links']['node'])) {
    $content['#contextual_links']['node'] = array(
      'node',
      array(
        $node->nid,
      ),
    );
  }

  // Create the block, using the node title for the block title.
  // Note that we render the content immediately here rather than passing back
  // a renderable so that if the block is empty it is hidden.
  $block = array(
    'subject' => check_plain($node->title),
    'content' => $content,
  );
  return $block;
}