You are here

webform_references.module in Webform References 7

Webform module node reference component.

File

webform_references.module
View source
<?php

/**
 * @file
 * Webform module node reference component.
 */

/**
 * Implements hook_menu().
 */
function webform_references_menu() {
  $items['webform_references/node/autocomplete/%'] = array(
    'page callback' => 'webform_references_node_autocomplete',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'webform_references.node.inc',
    'file path' => drupal_get_path('module', 'webform_references'),
  );
  $items['webform_references/term/autocomplete/%'] = array(
    'page callback' => 'webform_references_term_autocomplete',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'webform_references.term.inc',
    'file path' => drupal_get_path('module', 'webform_references'),
  );
  $items['webform_references/user/autocomplete/%'] = array(
    'page callback' => 'webform_references_user_autocomplete',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'webform_references.user.inc',
    'file path' => drupal_get_path('module', 'webform_references'),
  );
  $items['webform_references/view/autocomplete/%/%/%'] = array(
    'page callback' => 'webform_references_autocomplete_view',
    'page arguments' => array(
      3,
      4,
      5,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_webform_component_info().
 */
function webform_references_webform_component_info() {
  $components = array();
  $features = array(
    // Add content to CSV downloads. Defaults to TRUE.
    'csv' => TRUE,
    // Show this component in e-mailed submissions. Defaults to TRUE.
    'email' => TRUE,
    // Allow this component to be used as an e-mail FROM or TO address.
    // Defaults to FALSE.
    'email_address' => FALSE,
    // Allow this component to be used as an e-mail SUBJECT or FROM name.
    // Defaults to FALSE.
    'email_name' => FALSE,
    // This component may be toggled as required or not. Defaults to TRUE.
    'required' => TRUE,
    // This component has a title that can be toggled as displayed or not.
    'title_display' => TRUE,
    // This component has a title that can be displayed inline.
    'title_inline' => TRUE,
    // If this component can be used as a conditional SOURCE. All components
    // may always be displayed conditionally, regardless of this setting.
    // Defaults to TRUE.
    'conditional' => TRUE,
    // If this component allows other components to be grouped within it
    // (like a fieldset or tabs). Defaults to FALSE.
    'group' => FALSE,
    // If this component can be used for SPAM analysis, usually with Mollom.
    'spam_analysis' => FALSE,
    // If this component saves a file that can be used as an e-mail
    // attachment. Defaults to FALSE.
    'attachment' => FALSE,
  );
  $components['node_reference'] = array(
    'label' => t('Node Reference'),
    'description' => t('Node Reference field.'),
    'features' => $features,
    'file' => 'webform_references.node.inc',
  );
  $components['term_reference'] = array(
    'label' => t('Term Reference'),
    'description' => t('Term Reference field.'),
    'features' => $features,
    'file' => 'webform_references.term.inc',
  );
  $components['user_reference'] = array(
    'label' => t('User Reference'),
    'description' => t('User Reference field.'),
    'features' => $features,
    'file' => 'webform_references.user.inc',
  );
  return $components;
}

/**
 * Implements hook_system_info_alter().
 *
 * Add warning message to module info description if any component
 * of this module is added in any webform.
 */
function webform_references_system_info_alter(&$info, $file, $type) {
  if ($type == 'module' && $file->name == 'webform_references') {
    $result = db_select('webform_component', 'wc')
      ->fields('wc', array(
      'name',
    ))
      ->condition('wc.type', array(
      'node_reference',
      'term_reference',
      'user_reference',
    ), 'IN')
      ->execute()
      ->fetchAll();
    if (!empty($result)) {
      $info['description'] = t("Defines a webform component type for referencing the node/term/user. <span class='error'>(Warning: Don't disable/uninstall the module because some components of this module are in used.)</span>");
    }
  }
}

/**
 * Implements hook_webform_validator_alter().
 */
function webform_references_webform_validator_alter(&$validators) {
  $allowed_validators = array(
    'specific_value',
    'unique',
    'equal',
    'comparison',
  );
  foreach ($allowed_validators as $validator) {
    $validators[$validator]['component_types'][] = 'node_reference';
    $validators[$validator]['component_types'][] = 'term_reference';
    $validators[$validator]['component_types'][] = 'user_reference';
  }
}

/*
 * Helper function to retrieve options from a view
 */
function webform_references_execute_view($view_name, $display_name, $entity_type, $args = array(), $match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
  $result = array();

  // Check that the view is valid and the display still exists.
  $view = views_get_view($view_name);
  if (!$view || !isset($view->display[$display_name]) || !$view
    ->access($display_name)) {
    watchdog('entityreference', 'The view %view_name is no longer eligible.', array(
      '%view_name' => $view_name,
    ), WATCHDOG_WARNING);
    return array();
  }
  $view
    ->set_display($display_name);

  // Make sure the query is not cached.
  $view->is_cacheable = FALSE;

  // Set the options for the display handler.
  $entityreference_options = array(
    'match' => $match,
    'match_operator' => $match_operator,
    'limit' => $limit,
    'ids' => $ids,
  );
  $view->display_handler
    ->set_option('entityreference_options', $entityreference_options);
  $result = $view
    ->execute_display($display_name, $args);
  return $result;
}

/*
 * Callback function to find autocomplete matches from a view.
 */
function webform_references_autocomplete_view($type, $view_name, $display_name, $args = NULL, $string = '') {

  // need to gather the arguments to figure out what needs to be sent to the view
  $args = func_get_args();

  // Shift off the $type, $view_name, $display_name
  $args = array_slice($args, 3);

  // Get the string and pass the remainder as view arguments
  $string = array_pop($args);
  $result = webform_references_execute_view($view_name, $display_name, $type, $args, $string);
  $matches = array();
  foreach ($result as $id => $value) {
    $name = trim(strip_tags($value));
    $matches[$name . " [id:{$id}]"] = $name;
  }
  drupal_json_output($matches);
}

/**
 * Helper function to get node list from a view.
 */
function _webform_references_list_from_view($reference_view, $type, $view_args = '') {
  list($view_name, $display_name) = explode(':', $reference_view);
  $args = !empty($view_args) ? explode('/', token_replace($view_args, array())) : NULL;
  $result = webform_references_execute_view($view_name, $display_name, $type, $args);
  $list = array();
  foreach ($result as $id => $value) {
    $list[$id] = decode_entities(trim(strip_tags($value)));
  }
  return $list;
}