elements.module 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Implements hook_element_info().
  4. */
  5. function elements_element_info() {
  6. $types['emailfield'] = array(
  7. '#input' => TRUE,
  8. '#size' => 60,
  9. '#maxlength' => 128,
  10. '#autocomplete_path' => FALSE,
  11. '#process' => array('ajax_process_form'),
  12. '#theme' => 'emailfield',
  13. '#theme_wrappers' => array('form_element'),
  14. );
  15. $types['searchfield'] = array(
  16. '#input' => TRUE,
  17. '#size' => 60,
  18. '#maxlength' => 128,
  19. '#autocomplete_path' => FALSE,
  20. '#process' => array('ajax_process_form'),
  21. '#theme' => 'searchfield',
  22. '#theme_wrappers' => array('form_element'),
  23. );
  24. $types['telfield'] = array(
  25. '#input' => TRUE,
  26. '#size' => 20,
  27. '#maxlength' => 64,
  28. '#process' => array('ajax_process_form'),
  29. '#theme' => 'telfield',
  30. '#theme_wrappers' => array('form_element'),
  31. );
  32. $types['urlfield'] = array(
  33. '#input' => TRUE,
  34. '#size' => 80,
  35. '#maxlength' => 128,
  36. '#autocomplete_path' => FALSE,
  37. '#process' => array('ajax_process_form'),
  38. '#theme' => 'urlfield',
  39. '#theme_wrappers' => array('form_element'),
  40. );
  41. $types['numberfield'] = array(
  42. '#input' => TRUE,
  43. '#process' => array('ajax_process_form'),
  44. '#theme' => 'numberfield',
  45. '#theme_wrappers' => array('form_element'),
  46. );
  47. $types['rangefield'] = array(
  48. '#input' => TRUE,
  49. '#process' => array('ajax_process_form'),
  50. '#theme' => 'rangefield',
  51. '#theme_wrappers' => array('form_element'),
  52. );
  53. return $types;
  54. }
  55. /**
  56. * Implements hook_element_info_alter().
  57. */
  58. function elements_element_info_alter(&$types) {
  59. // Add placeholder support to textfields and textareas.
  60. foreach (array_keys($types) as $type) {
  61. switch ($type) {
  62. case 'textfield':
  63. case 'textarea':
  64. $types[$type]['#process'][] = 'form_process_placeholder';
  65. break;
  66. }
  67. }
  68. }
  69. /**
  70. * Implements hook_theme().
  71. */
  72. function elements_theme() {
  73. return array(
  74. 'emailfield' => array(
  75. 'arguments' => array('element' => NULL),
  76. 'render element' => 'element',
  77. 'file' => 'elements.theme.inc',
  78. ),
  79. 'searchfield' => array(
  80. 'arguments' => array('element' => NULL),
  81. 'render element' => 'element',
  82. 'file' => 'elements.theme.inc',
  83. ),
  84. 'telfield' => array(
  85. 'arguments' => array('element' => NULL),
  86. 'render element' => 'element',
  87. 'file' => 'elements.theme.inc',
  88. ),
  89. 'urlfield' => array(
  90. 'arguments' => array('element' => NULL),
  91. 'render element' => 'element',
  92. 'file' => 'elements.theme.inc',
  93. ),
  94. 'numberfield' => array(
  95. 'arguments' => array('element' => NULL),
  96. 'render element' => 'element',
  97. 'file' => 'elements.theme.inc',
  98. ),
  99. 'rangefield' => array(
  100. 'arguments' => array('element' => NULL),
  101. 'render element' => 'element',
  102. 'file' => 'elements.theme.inc',
  103. ),
  104. );
  105. }
  106. /**
  107. * Return the autocompletion HTML for a form element.
  108. *
  109. * @param $element
  110. * The renderable element to process for autocompletion.
  111. *
  112. * @return
  113. * The rendered autocompletion element HTML, or an empty string if the field
  114. * has no autocompletion enabled.
  115. */
  116. function elements_add_autocomplete(&$element) {
  117. $extra = '';
  118. if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
  119. drupal_add_library('system', 'drupal.autocomplete');
  120. $element['#attributes']['class'][] = 'form-autocomplete';
  121. $attributes = array();
  122. $attributes['type'] = 'hidden';
  123. $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
  124. $attributes['value'] = url($element['#autocomplete_path'], array('absolute' => TRUE));
  125. $attributes['disabled'] = 'disabled';
  126. $attributes['class'][] = 'autocomplete';
  127. $extra = '<input' . drupal_attributes($attributes) . ' />';
  128. }
  129. return $extra;
  130. }
  131. /**
  132. * Element process callback; adds support for the HTML5 placeholder attribute.
  133. *
  134. * @param $element
  135. * An associative array containing the properties of the element.
  136. *
  137. * @return
  138. * The processed element.
  139. */
  140. function form_process_placeholder($element) {
  141. if (isset($element['#placeholder']) && !isset($element['#attributes']['placeholder'])) {
  142. // If the placeholder FAPI property is set, simply add it to the form's
  143. // attributes so it will be output in the HTML tag.
  144. $element['#attributes']['placeholder'] = $element['#placeholder'];
  145. }
  146. return $element;
  147. }