You are here

webform_autocomplete.module in Webform Autocomplete 7.2

Same filename and directory in other branches
  1. 7 webform_autocomplete.module

File

webform_autocomplete.module
View source
<?php

/**
 * This module provides an autocomplete element for webforms.
 *
 * @author Coleman Watts
 */
define('WEBFORM_AUTOCOMPLETE_MATCH_BEGIN', 0);
define('WEBFORM_AUTOCOMPLETE_MATCH_CONTAINS', 1);
define('WEBFORM_AUTOCOMPLETE_MATCH_END', 2);

/**
 * Implements hook_menu().
 *
 * @return array
 */
function webform_autocomplete_menu() {
  $items = array();
  $items['webform_autocomplete/js/%node/%'] = array(
    'title' => 'Webform Autocomplete',
    'page callback' => 'webform_autocomplete_js',
    'access callback' => 'webform_autocomplete_access',
    'access arguments' => array(
      2,
      3,
    ),
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
    'file' => 'autocomplete.inc',
  );
  return $items;
}

/**
 * Implements hook_webform_component_info().
 *
 * @return array
 */
function webform_autocomplete_webform_component_info() {
  return array(
    'autocomplete' => array(
      'label' => t('Autocomplete'),
      'description' => t('Autocomplete textfield.'),
      'features' => array(
        'email_name' => TRUE,
        'spam_analysis' => TRUE,
      ),
      'file' => 'autocomplete.inc',
    ),
  );
}

/**
 * Access callback. Check if user has permission to view autocomplete results.
 */
function webform_autocomplete_access($node, $cid) {
  global $user;
  if (!$cid || empty($node->webform['components'][$cid]) || !node_access('view', $node)) {
    return FALSE;
  }
  if ($user->uid === 1 || webform_results_access($node)) {
    return TRUE;
  }
  if (!empty($node->webform['components'][$cid]['private'])) {
    return FALSE;
  }
  if (variable_get('webform_submission_access_control', 1)) {
    $allowed_roles = array();
    foreach ($node->webform['roles'] as $rid) {
      $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
    }
    if (array_search(TRUE, $allowed_roles) === FALSE) {
      return FALSE;
    }
  }
  return TRUE;
}

/**
 * Validation callback for webform component.
 */
function webform_autocomplete_validate_restricted($element, &$form_state) {
  $options = preg_split('/\\r\\n|\\r|\\n/', $element['#webform_component']['extra']['autocomplete_items']);
  if (!in_array($element['#value'], $options)) {
    form_error($element, t('Invalid choice for @field.', array(
      '@field' => $element['#title'],
    )));
  }
}

/**
 * Implements hook_form_builder_element_types().
 * @param string $form_type
 * @param int $form_id
 * @return array
 */
function webform_autocomplete_form_builder_element_types($form_type, $form_id) {
  if ($form_type == 'webform') {
    drupal_add_css(drupal_get_path('module', 'webform_autocomplete') . '/autocomplete.css');
    $fields = array(
      'autocomplete' => array(
        'title' => t('Autocomplete'),
        'weight' => -30,
        'properties' => array(
          'title',
          'description',
          'field_prefix',
          'field_suffix',
          'default_value',
          'required',
          'size',
          'maxlength',
          'disabled',
          'autocomplete_items',
          'autocomplete_existing',
          'autocomplete_taxonomy',
          'autocomplete_match_rule',
          'autocomplete_restrict',
          'unique',
        ),
      ),
    );
    if (function_exists('_form_builder_webform_default')) {
      $fields['autocomplete']['default'] = _form_builder_webform_default('autocomplete', array(), array(
        'name' => t('New autocomplete'),
      ));
    }
    return $fields;
  }
}

/**
 * Implements hook_form_builder_preview_alter().
 */
function webform_autocomplete_form_builder_preview_alter(&$element, $form_type, $form_id) {

  // Disable live autocomplete during preview
  if ($form_type == 'webform' && $element['#webform_component']['type'] == 'autocomplete') {
    $element['#attributes']['class'][] = 'form-autocomplete';
    $element['#autocomplete_path'] = '';
  }
}

Functions

Constants

Namesort descending Description
WEBFORM_AUTOCOMPLETE_MATCH_BEGIN This module provides an autocomplete element for webforms.
WEBFORM_AUTOCOMPLETE_MATCH_CONTAINS
WEBFORM_AUTOCOMPLETE_MATCH_END