You are here

webform_view.module in Webform view 7

Same filename and directory in other branches
  1. 7.4 webform_view.module

Allows views to be embedded in webforms and used as submission data.

This does NOT save all that data in a fully webform-compatible way for reports, it just allows elements that were added on the fly to be processed as data for emails. Data is not available for later reporting.

USAGE.

Add a field 'can order' as a boolean flag on nodes that can be ordered.

Set its 'display' to be 'webform placeholder'

Make a view for the nodes you want to show on a webform. This view should display the 'webform placeholder' field.

Edit the webform and add this view as a webform field.

The placeholder on each node in the view will be replaced with a webform submission field.

File

webform_view.module
View source
<?php

/**
 * @file
 * Allows views to be embedded in webforms and used as submission data.
 *
 *
 * This does NOT save all that data in a fully webform-compatible way for
 * reports, it just allows elements that were added on the fly to be processed
 * as data for emails. Data is not available for later reporting.
 *
 * USAGE.
 *
 * Add a field 'can order' as a boolean flag on nodes that can be ordered.
 *
 * Set its 'display' to be 'webform placeholder'
 *
 * Make a view for the nodes you want to show on a webform.
 * This view should display the 'webform placeholder' field.
 *
 * Edit the webform and add this view as a webform field.
 *
 * The placeholder on each node in the view will be replaced with a webform
 * submission field.
 */

/**
 * Declare our new component type for webform - an embedded view.
 *
 * Implements hook_webform_component_info().
 */
function webform_view_webform_component_info() {
  $component_info = array(
    'view' => array(
      'label' => t('Embedded view'),
      'description' => t('Allows a view to be embedded and each row in the view to be a selectable option.'),
      'file' => 'webform_view.inc',
      'features' => array(
        'default_value' => FALSE,
        // Support nested fields here.
        'group' => TRUE,
      ),
    ),
  );
  return $component_info;
}

/**
 * Declare our custom theme.
 *
 * Catch the rendering of an embedded view at the end.
 *
 * Implements hook_theme().
 */
function webform_view_theme() {
  return array(
    'webform_view_embedded' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Implements hook_help().
 */
function webform_view_help($path, $arg) {
  $modulename = 'webform_view';
  switch ($path) {
    case 'admin/help#' . $modulename:
      $help_dir = drupal_get_path('module', $modulename) . '/help';
      $text = file_get_contents("{$help_dir}/index.html");
      $text = preg_replace('/(src|href)="([^\\/][^"]+)"/', '$1="' . url($help_dir) . '/$2"', $text);
      return $text;
  }
  return FALSE;
}

/**
 * Adjusts a webform_client_form.
 *
 * Calculates the unique key which is required later.
 *
 * Implements hook_form_alter().
 */
function webform_view_form_alter(&$form, $form_state, $form_id) {
  if (substr($form_id, 0, 19) != 'webform_client_form') {
    return;
  }

  // Find form_key from cid.
  $form_key = array();
  $settings = variable_get('webform_view_' . $form['#node']->nid, array());
  foreach ($settings as $cid => $enabled) {
    if (is_int($cid) && $enabled) {
      $form_key[] = 'webform-component-' . $form['#node']->webform['components'][$cid]['form_key'];
    }
  }
}

/**
 * A special formatter to use as a placeholder to insert our view into a form.
 *
 * By declaring an alternative field formatter - to be used in place of the
 * 'can order' flag, we can insert form elements into the view display
 * and therefore into the webform.
 *
 * Field used on the content type must be a Boolean.
 * If so, then we can replace that item with a custom display renderer.
 *
 * Implements hook_field_formatter_info().
 */
function webform_view_field_formatter_info() {
  return array(
    'webform_view_placeholder' => array(
      'label' => t('A webform placeholder'),
      'field types' => array(
        'list_boolean',
      ),
    ),
  );
}

/**
 * Insert a PLACEHOLDER value as the text rendering of an item.
 *
 * The webform postprocess should replace this placeholder with the form element
 *
 * Implements hook_field_formatter_view().
 *
 * @see theme_webform_view_embedded()
 */
function webform_view_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'webform_view_placeholder':
      foreach ($items as $delta => $item) {

        // Don't even insert the placeholder if there is no value in the trigger
        // element.
        if (!empty($item['value'])) {
          $key = $entity_type == 'node' ? $entity->nid : $entity->id;
          $replace_pattern = "[webform_view_" . $key . "_placeholder]";
          $element[$delta] = array(
            '#type' => 'markup',
            '#markup' => $replace_pattern,
          );
        }
        else {

          // Cannot order, return empty string.
          $element[$delta] = array(
            '#type' => 'markup',
            '#markup' => '',
          );
        }
      }
      break;
  }
  return $element;
}

/**
 * Respond to the loading of Webform submissions.
 *
 * Unpack our data so it can be retrieved and displayed in reports.
 *
 * @param array $submissions
 *   An array of Webform submissions that are being loaded, keyed by the
 *   submission ID. Modifications to the submissions are done by reference.
 */
function webform_view_webform_submission_load(&$submissions) {

  // Unused - this is too high-level
}

Functions

Namesort descending Description
webform_view_field_formatter_info A special formatter to use as a placeholder to insert our view into a form.
webform_view_field_formatter_view Insert a PLACEHOLDER value as the text rendering of an item.
webform_view_form_alter Adjusts a webform_client_form.
webform_view_help Implements hook_help().
webform_view_theme Declare our custom theme.
webform_view_webform_component_info Declare our new component type for webform - an embedded view.
webform_view_webform_submission_load Respond to the loading of Webform submissions.