You are here

webform_hints.module in Webform Hints 6

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

File

webform_hints.module
View source
<?php

/**
 * 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
 */

/**
 * Implementation of hook_init().
 */
function webform_hints_init() {
  drupal_add_js(drupal_get_path('module', 'webform_hints') . '/jquery.formHints.js');
  drupal_add_js(drupal_get_path('module', 'webform_hints') . '/webform_hints.js');
}

/**
 * Implementation of hook_perm().
 */
function webform_hints_perm() {
  return array(
    'administer Webform Hints settings',
  );
}

/**
 * Implementation of hook_menu().
 */
function webform_hints_menu() {
  $items = array();

  // Main configuration page of the Webform Hints module
  $items['admin/settings/webform-hints'] = array(
    'title' => 'Webform Hints',
    '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 $items;
}

/**
 * Implementation of 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_') {
    if (in_array($form['#node']->nid, $webforms)) {

      // If the current form gets hints, add a webform-hints class to the form
      // so that jQuery can target the form.
      $form['#attributes']['class'] .= ' webform-hints';
      array_walk($form['submitted'], 'webform_hints_add_title');
    }
  }
}

/**
 * Alter the passed webform element,
 * add the elements title to the title attribute,
 * and unset the label or title of the element.
 */
function webform_hints_add_title(&$element) {

  // Skip the boolean #tree.
  if (is_array($element)) {
    $fieldtypes = array(
      'textfield',
      'textarea',
      'webform_email',
      'email',
    );
    if (in_array($element['#type'], $fieldtypes)) {
      $element['#attributes']['title'] = $element['#title'];
      $element['#attributes']['placeholder'] = $element['#title'];
    }
  }
}

Functions

Namesort descending Description
webform_hints_add_title Alter the passed webform element, add the elements title to the title attribute, and unset the label or title of the element.
webform_hints_form_alter Implementation of hook_form_alter().
webform_hints_init Implementation of hook_init().
webform_hints_menu Implementation of hook_menu().
webform_hints_perm Implementation of hook_perm().