You are here

elements.module in Elements 6

Same filename and directory in other branches
  1. 5 elements.module
  2. 7 elements.module

File

elements.module
View source
<?php

/**
 * Implements hook_elements().
 */
function elements_elements() {
  $types['tableselect'] = array(
    '#input' => TRUE,
    '#js_select' => TRUE,
    '#multiple' => TRUE,
    '#process' => array(
      'form_process_tableselect',
    ),
    '#options' => array(),
    '#empty' => '',
  );
  $types['emailfield'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );
  $types['searchfield'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );
  $types['telfield'] = array(
    '#input' => TRUE,
    '#size' => 20,
    '#maxlength' => 64,
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );
  $types['urlfield'] = array(
    '#input' => TRUE,
    '#size' => 80,
    '#maxlength' => 128,
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );
  $types['numberfield'] = array(
    '#input' => TRUE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );
  $types['rangefield'] = array(
    '#input' => TRUE,
    '#process' => array(
      'form_expand_ahah',
    ),
    '#value_callback' => 'form_type_textfield_value',
  );

  // Add placeholder support to textfields and textareas.
  $types['textfield'] = array(
    '#process' => array(
      'form_process_placeholder',
    ),
  );
  $types['textarea'] = array(
    '#process' => array(
      'form_process_placeholder',
    ),
  );
  $types['password'] = array(
    '#process' => array(
      'form_process_placeholder',
    ),
  );
  return $types;
}

/**
 * Implements hook_theme().
 */
function elements_theme() {
  return array(
    'tableselect' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'emailfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'searchfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'telfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'urlfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'numberfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
    'rangefield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'elements.theme.inc',
    ),
  );
}

/**
 * Return the autocompletion HTML for a form element.
 *
 * @param $element
 *   The renderable element to process for autocompletion.
 *
 * @return
 *   The rendered autocompletion element HTML, or an empty string if the field
 *   has no autocompletion enabled.
 */
function elements_add_autocomplete(&$element) {
  $output = '';
  if (!empty($element['#autocomplete_path']) && menu_valid_path(array(
    'link_path' => $element['#autocomplete_path'],
  ))) {
    drupal_add_js('misc/autocomplete.js');
    $element['#attributes']['class'] .= ' form-autocomplete';
    $attributes = array();
    $attributes['type'] = 'hidden';
    $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
    $attributes['value'] = url($element['#autocomplete_path'], array(
      'absolute' => TRUE,
    ));
    $attributes['disabled'] = 'disabled';
    $attributes['class'] = 'autocomplete';
    $output = '<input' . drupal_attributes($attributes) . ' />';
  }
  return $output;
}

/**
 * Element process callback; adds support for the HTML5 placeholder attribute.
 *
 * @param $element
 *   An associative array containing the properties of the element.
 *
 * @return
 *   The processed element.
 */
function form_process_placeholder($element) {
  if (isset($element['#placeholder']) && !isset($element['#attributes']['placeholder'])) {

    // If the placeholder FAPI property is set, simply add it to the form's
    // attributes so it will be output in the HTML tag.
    $element['#attributes']['placeholder'] = $element['#placeholder'];
  }
  return $element;
}

/**
 * Sets HTML attributes based on element properties.
 *
 * This is a backport from the Drupal 7 core function of the same name.
 *
 * @param $element
 *   The renderable element to process.
 * @parm $map
 *   An associative array whose keys are element property names and whose
 *   values are the HTML attribute names to set for corresponding the property;
 *   e.g., array('#propertyname' => 'attributename'). If both names are
 *   identical except for the leading '#', then an attribute name value is
 *   sufficient and no property name needs to be specified.
 */
function element_set_attributes(array &$element, array $map) {
  foreach ($map as $property => $attribute) {

    // If the key is numeric, the attribute name needs to be taken over.
    if (is_int($property)) {
      $property = '#' . $attribute;
    }

    // Do not overwrite already existing attributes.
    if (isset($element[$property]) && !isset($element['#attributes'][$attribute])) {
      $element['#attributes'][$attribute] = $element[$property];
    }
  }
}

/**
 * Create the correct amount of checkbox or radio elements to populate the table.
 *
 * @param $element
 *   An associative array containing the properties and children of the
 *   tableselect element.
 * @return
 *   The processed element.
 */
function form_process_tableselect($element) {
  if ($element['#multiple']) {
    $value = is_array($element['#value']) ? $element['#value'] : array();
  }
  else {

    // Advanced selection behaviour make no sense for radios.
    $element['#js_select'] = FALSE;
  }
  $element['#tree'] = TRUE;
  if (count($element['#options']) > 0) {
    if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
      $element['#default_value'] = array();
    }

    // Create a checkbox or radio for each item in #options in such a way that
    // the value of the tableselect element behaves as if it had been of type
    // checkboxes or radios.
    foreach ($element['#options'] as $key => $choice) {

      // Do not overwrite manually created children.
      if (!isset($element[$key])) {
        if ($element['#multiple']) {
          $element[$key] = array(
            '#type' => 'checkbox',
            '#title' => '',
            '#return_value' => $key,
            '#default_value' => isset($value[$key]),
            '#attributes' => $element['#attributes'],
            '#ahah' => isset($element['#ahah']) ? $element['#ahah'] : NULL,
          );
        }
        else {

          // Generate the parents as the autogenerator does, so we will have a
          // unique id for each radio button.
          $parents_for_id = array_merge($element['#parents'], array(
            $key,
          ));
          $element[$key] = array(
            '#type' => 'radio',
            '#title' => '',
            '#return_value' => $key,
            '#default_value' => $element['#default_value'] == $key ? $key : NULL,
            '#attributes' => $element['#attributes'],
            '#parents' => $element['#parents'],
            '#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
            '#ahah' => isset($element['#ahah']) ? $element['#ahah'] : NULL,
          );
        }
        if (isset($element['#options'][$key]['#weight'])) {
          $element[$key]['#weight'] = $element['#options'][$key]['#weight'];
        }
      }
    }
  }
  else {
    $element['#value'] = array();
  }
  return $element;
}

Functions

Namesort descending Description
elements_add_autocomplete Return the autocompletion HTML for a form element.
elements_elements Implements hook_elements().
elements_theme Implements hook_theme().
element_set_attributes Sets HTML attributes based on element properties.
form_process_placeholder Element process callback; adds support for the HTML5 placeholder attribute.
form_process_tableselect Create the correct amount of checkbox or radio elements to populate the table.