You are here

field_placeholder.module in Field placeholder 7

Same filename and directory in other branches
  1. 7.2 field_placeholder.module

Field placeholder module hooks and main functionality.

This module gives support to add HTML5 placeholder attribute to field widget forms.

File

field_placeholder.module
View source
<?php

/**
 * @file
 * Field placeholder module hooks and main functionality.
 *
 * This module gives support to add HTML5 placeholder
 * attribute to field widget forms.
 */

/**
 * Implements hook_field_form_field_ui_field_edit_form_alter().
 */
function field_placeholder_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  if (in_array($form['#instance']['widget']['type'], field_placeholder_get_widget_types())) {
    $form['instance']['placeholder'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder'),
      '#maxlength' => 512,
      '#description' => t('Placeholder value for this field instance'),
      '#weight' => -4,
      '#default_value' => isset($form['#instance']['placeholder']) ? $form['#instance']['placeholder'] : '',
    );
  }
}

/**
 * Implements hook_field_widget_form_alter().
 */
function field_placeholder_field_widget_form_alter(&$element, &$form_state, $context) {
  if (in_array($context['instance']['widget']['type'], field_placeholder_get_widget_types()) && isset($context['instance']['placeholder'])) {
    $placeholder = field_placeholder_get_widget($context['instance']['widget']['type']);
    $instance = module_exists('i18n_field_placeholder') ? i18n_string_object_translate('field_instance', $context['instance']) : $context['instance'];
    if ($placeholder) {
      $element[$placeholder]['#attributes']['placeholder'] = check_plain($instance['placeholder']);
    }
    else {
      $element['#attributes']['placeholder'] = check_plain($instance['placeholder']);
    }
  }
}

/**
 * Returns information about field widgets from hook_field_placeholder_info().
 *
 * @return array
 *   An array containing all the Field placeholder available widgets
 *   information.
 */
function field_placeholder_get_widgets() {
  $widgets =& drupal_static(__FUNCTION__);
  if (!$widgets) {
    $widgets = module_invoke_all('field_placeholder_info');
    $widgets = is_array($widgets) ? $widgets : array();
  }
  return $widgets;
}

/**
 * Returns information about a widget_type from hook_field_placeholder_info().
 *
 * @param string $widget_type
 *   (optional) A widget type name. If omitted, all widget types will be
 *   returned.
 *
 * @return string|bool
 *   Columns name for the required widget if if exists, otherwise returns FALSE.
 */
function field_placeholder_get_widget($widget_type) {
  $widgets = module_invoke_all('field_placeholder_info');
  return isset($widgets[$widget_type]) ? $widgets[$widget_type] : FALSE;
}

/**
 * Returns an array with names of all the placeholder available widgets.
 *
 * @return array
 *   Array containing all the Field placeholder available widgets name.
 */
function field_placeholder_get_widget_types() {
  $widgets = field_placeholder_get_widgets();
  return array_keys($widgets);
}

/**
 * Implements hook_field_placeholder_info().
 */
function field_placeholder_field_placeholder_info() {
  return array(
    'text_textfield' => 'value',
    'text_textarea' => 'value',
    'text_textarea_with_summary' => 'summary',
    'number' => 'value',
    'email_textfield' => 'email',
    'phone_textfield' => 'value',
    'taxonomy_autocomplete' => NULL,
  );
}

Functions

Namesort descending Description
field_placeholder_field_placeholder_info Implements hook_field_placeholder_info().
field_placeholder_field_widget_form_alter Implements hook_field_widget_form_alter().
field_placeholder_form_field_ui_field_edit_form_alter Implements hook_field_form_field_ui_field_edit_form_alter().
field_placeholder_get_widget Returns information about a widget_type from hook_field_placeholder_info().
field_placeholder_get_widgets Returns information about field widgets from hook_field_placeholder_info().
field_placeholder_get_widget_types Returns an array with names of all the placeholder available widgets.