You are here

function webform_hints_add_title in Webform Hints 7

Same name and namespace in other branches
  1. 6 webform_hints.module \webform_hints_add_title()

Alter the passed element appropriately to add the hint.

Parameters

array $element: The Webform element.

string $required_label: The string appended to the end of a required element's hint.

boolean $use_description: Determines whether the label (default) or description is used as the hint.

1 call to webform_hints_add_title()
webform_hints_form_alter in ./webform_hints.module
Implements hook_form_alter().

File

./webform_hints.module, line 165
This module applies the titles of webform components as placeholder hints.

Code

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);
  }
}