webform_hints.module in Webform Hints 7
Same filename and directory in other branches
This module applies the titles of webform components as placeholder hints.
The original Development and on-going maintenance of this module is sponsored by CHROMATIC, LLC. http://chromaticsites.com
File
webform_hints.moduleView source
<?php
/**
* @file
* This module applies the titles of webform components as placeholder hints.
*
* The original Development and on-going maintenance of this module is
* sponsored by CHROMATIC, LLC. http://chromaticsites.com
*/
/**
* Default indicator for required field labels. Configurable in module settings.
*/
define('WEBFORM_HINTS_DEFAULT_REQUIRED_INDICATOR', ' (required)');
/**
* Implements hook_permission().
*/
function webform_hints_permission() {
return array(
'administer Webform Hints settings' => array(
'title' => t('Administer Webform Hints settings'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_libraries_info().
*/
function webform_hints_libraries_info() {
$libraries['form-defaults'] = array(
'name' => 'jQuery Form Defaults',
'vendor url' => 'https://github.com/palmerj3/jQuery-Plugin---Form-Field-Default-Value',
'download url' => 'https://github.com/palmerj3/jQuery-Plugin---Form-Field-Default-Value',
// No version provided by plugin author.
'version' => '1.0',
'files' => array(
'js' => array(
'jquery.form-defaults.min.js',
),
),
);
return $libraries;
}
/**
* Check if the Form Defaults library is installed.
*
* @return bool
* A boolean indicating the installed status.
*/
function webform_hints_form_defaults_installed() {
if (($library = libraries_detect('form-defaults')) && !empty($library['installed'])) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Check if the Form Defaults library has been loaded.
*
* @return bool
* A boolean indicating the loaded status.
*/
function webform_hints_form_defaults_loaded() {
if (($library = libraries_load('form-defaults')) && !empty($library['loaded'])) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Implements hook_menu().
*/
function webform_hints_menu() {
$menu = array();
// Main configuration page of the Webform Hints module.
$menu['admin/config/content/webform-hints'] = array(
'title' => 'Webform Hints settings',
'description' => 'Administer which of your Webforms will receive the hints effect.',
'file' => 'webform_hints.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'webform_hints_admin_settings',
),
'access arguments' => array(
'administer Webform Hints settings',
),
'type' => MENU_NORMAL_ITEM,
);
return $menu;
}
/**
* Implements hook_variable_group_info().
*/
function webform_hints_variable_group_info() {
// Adds multilingual variable support to Webform Hints with i18n_variable.
$groups['webform_hints'] = array(
'title' => t('Webform hints'),
'description' => t('Webform hints module settings'),
'access' => 'administer site configuration',
'path' => array(
'admin/config/content/webform-hints',
),
);
return $groups;
}
/**
* Implements hook_variable_info().
*/
function webform_hints_variable_info($options) {
// Webform Hints variables with multilingual support using i18n_variable.
$variables['webform_hints_required'] = array(
'type' => 'string',
'title' => t('Required indicator', array(), $options),
'default' => WEBFORM_HINTS_DEFAULT_REQUIRED_INDICATOR,
'description' => t('Appends to the hint text of a required component.', array(), $options),
'required' => TRUE,
'group' => 'webform_hints',
);
return $variables;
}
/**
* Implements hook_form_alter().
*/
function webform_hints_form_alter(&$form, &$form_state, $form_id) {
// Get webforms to add hints to.
$webforms = array_values(variable_get('webform_hints_forms', array()));
// Is this a webform?
if (substr($form_id, 0, 20) == 'webform_client_form_') {
$webform_selected = in_array($form['#node']->nid, $webforms);
$webform_hints_reverse = variable_get('webform_hints_reverse', 0);
if ($webform_hints_reverse == 1) {
$webform_selected = !$webform_selected;
}
if ($webform_selected) {
// Load legacy JS if enabled.
if (webform_hints_form_defaults_loaded() && variable_get('webform_hints_legacy_support', '')) {
drupal_add_js(drupal_get_path('module', 'webform_hints') . '/js/webform_hints.js');
}
// Add webform-hints class to the form for easy jQuery targeting.
$form['#attributes']['class'][] = 'webform-hints';
// Perform element adjustments.
$required_label = variable_get('webform_hints_required', WEBFORM_HINTS_DEFAULT_REQUIRED_INDICATOR);
$use_description = variable_get('webform_hints_use_description', FALSE);
foreach (element_children($form['submitted']) as $key) {
webform_hints_add_title($form['submitted'][$key], $required_label, $use_description);
}
}
}
}
/**
* Alter the passed element appropriately to add the hint.
*
* @param array $element
* The Webform element.
* @param string $required_label
* The string appended to the end of a required element's hint.
* @param boolean $use_description
* Determines whether the label (default) or description is used as the hint.
*/
function webform_hints_add_title(&$element, $required_label, $use_description = FALSE) {
// Verify that the element is an array before treating it as such.
if (!is_array($element)) {
return;
}
// Perform a recursive call on container (fieldset, etc.) form items.
foreach (element_children($element) as $key) {
webform_hints_add_title($element[$key], $required_label, $use_description);
}
// Regular form items.
if (isset($element['#type'])) {
// Define the field types to act on.
$fieldtypes = array(
'textfield',
'textarea',
'webform_email',
'webform_number',
'select',
);
drupal_alter('webform_hints_fieldtypes', $fieldtypes, $element);
if (in_array($element['#type'], $fieldtypes)) {
// Build the hint value.
if (!$use_description) {
// Use the components title/label for the hint.
$hint_value = $element['#title'];
$element['#title_display'] = 'invisible';
if (!empty($element['#required'])) {
$hint_value .= $required_label;
}
}
else {
// The Description is used as the hint. The visible label will show
// a required marker, so there is no need to add $required_label here.
$hint_value = $element['#description'];
$element['#description'] = '';
}
// Assign the hint value to the component attributes that will use it.
if ($element['#type'] != 'select') {
$element['#attributes']['title'] = $hint_value;
if (!isset($element['#attributes']['placeholder'])) {
$element['#attributes']['placeholder'] = $hint_value;
}
if (variable_get('webform_hints_legacy_support', FALSE)) {
$element['#attributes']['label'] = $hint_value;
}
}
else {
// Add the hint as an empty option to select lists.
$element['#empty_option'] = '- ' . $hint_value . ' -';
}
// Add a webform_hints class to the component.
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = array();
}
$element['#attributes']['class'][] = 'webform-hints-field';
}
drupal_alter('webform_hints_element', $element, $required_label);
}
}
Functions
Name![]() |
Description |
---|---|
webform_hints_add_title | Alter the passed element appropriately to add the hint. |
webform_hints_form_alter | Implements hook_form_alter(). |
webform_hints_form_defaults_installed | Check if the Form Defaults library is installed. |
webform_hints_form_defaults_loaded | Check if the Form Defaults library has been loaded. |
webform_hints_libraries_info | Implements hook_libraries_info(). |
webform_hints_menu | Implements hook_menu(). |
webform_hints_permission | Implements hook_permission(). |
webform_hints_variable_group_info | Implements hook_variable_group_info(). |
webform_hints_variable_info | Implements hook_variable_info(). |
Constants
Name![]() |
Description |
---|---|
WEBFORM_HINTS_DEFAULT_REQUIRED_INDICATOR | Default indicator for required field labels. Configurable in module settings. |