You are here

webform_views_select.module in Webform Views Select 7

Module file for Webform Views Select.

File

webform_views_select.module
View source
<?php

/**
 * @file
 * Module file for Webform Views Select.
 */

/**
 * Implements hook_views_api().
 */
function webform_views_select_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_webform_select_options_info().
 */
function webform_views_select_webform_select_options_info() {
  $views = views_get_enabled_views();
  $items = array();
  foreach ($views as $view) {
    if (!empty($view->display)) {
      foreach ($view->display as $id => $display) {
        if ($view->display[$id]->display_plugin == 'webform_select') {
          if (!empty($view->human_name)) {
            $viewname = $view->human_name;
          }
          else {
            $viewname = $view->name;
          }
          $items[$view->name . '-' . $id] = array(
            'title' => t('Views: @view - @display', array(
              '@view' => $viewname,
              '@display' => $display->display_title,
            )),
            'options callback' => 'webform_views_select_options',
            'options arguments' => array(
              'view' => $view->name,
              'display_id' => $id,
            ),
          );
        }
      }
    }
  }
  return $items;
}

/**
 * Callback for retrieving webform select options.
 *
 * @see webform_options_example()
 */
function webform_views_select_options($component, $flat, $filter, $args = array()) {

  // In Webform 4.x, $filter has been removed and the value $args is really the
  // third parameter. This means that when $args is empty, we need to get the
  // arguments from the $filter argument.
  if (empty($args)) {
    $args = $filter;
  }

  // Get the view and display.
  $view = views_get_view($args['view']);
  $view
    ->set_display($args['display_id']);

  // Set view arguments if present (Might be useful when used in a form alter
  // function, seee README.txt for an example).
  if (isset($args['view_args'])) {
    $view
      ->set_arguments($args['view_args']);
  }
  return webform_views_select_options_from_view($view, $flat);
}

/**
 * Internal function to get the results.
 */
function webform_views_select_options_from_view($view, $flat = FALSE) {
  $view
    ->pre_execute();
  $view
    ->render();

  // Check if the view was built correctly.
  if (!$view->built) {
    watchdog('webform_views_select', 'There was an error while building display %display of view %view', array(
      '%view' => $view->name,
      '%display' => $view->current_display,
    ), WATCHDOG_ERROR);
    return array();
  }
  return $view->display_handler
    ->execute($flat);
}

/**
 * Implements hook_help().
 */
function webform_views_select_help($path, $arg) {
  switch ($path) {

    // Main module help for this module:
    case 'admin/help#webform_views_select':
      return '<p>' . t('Instructions on how to use this module are provided in the README.txt file included with the module.') . '</p>';
  }
}

Functions