field_ui.api.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Field UI module.
  5. */
  6. /**
  7. * @addtogroup field_types
  8. * @{
  9. */
  10. /**
  11. * Add settings to a field settings form.
  12. *
  13. * Invoked from field_ui_field_settings_form() to allow the module defining the
  14. * field to add global settings (i.e. settings that do not depend on the bundle
  15. * or instance) to the field settings form. If the field already has data, only
  16. * include settings that are safe to change.
  17. *
  18. * @todo: Only the field type module knows which settings will affect the
  19. * field's schema, but only the field storage module knows what schema
  20. * changes are permitted once a field already has data. Probably we need an
  21. * easy way for a field type module to ask whether an update to a new schema
  22. * will be allowed without having to build up a fake $prior_field structure
  23. * for hook_field_update_forbid().
  24. *
  25. * @param $field
  26. * The field structure being configured.
  27. * @param $instance
  28. * The instance structure being configured.
  29. * @param $has_data
  30. * TRUE if the field already has data, FALSE if not.
  31. *
  32. * @return
  33. * The form definition for the field settings.
  34. */
  35. function hook_field_settings_form($field, $instance, $has_data) {
  36. $settings = $field['settings'];
  37. $form['max_length'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Maximum length'),
  40. '#default_value' => $settings['max_length'],
  41. '#required' => FALSE,
  42. '#element_validate' => array('element_validate_integer_positive'),
  43. '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
  44. );
  45. return $form;
  46. }
  47. /**
  48. * Add settings to an instance field settings form.
  49. *
  50. * Invoked from field_ui_field_edit_form() to allow the module defining the
  51. * field to add settings for a field instance.
  52. *
  53. * @param $field
  54. * The field structure being configured.
  55. * @param $instance
  56. * The instance structure being configured.
  57. *
  58. * @return
  59. * The form definition for the field instance settings.
  60. */
  61. function hook_field_instance_settings_form($field, $instance) {
  62. $settings = $instance['settings'];
  63. $form['text_processing'] = array(
  64. '#type' => 'radios',
  65. '#title' => t('Text processing'),
  66. '#default_value' => $settings['text_processing'],
  67. '#options' => array(
  68. t('Plain text'),
  69. t('Filtered text (user selects text format)'),
  70. ),
  71. );
  72. if ($field['type'] == 'text_with_summary') {
  73. $form['display_summary'] = array(
  74. '#type' => 'select',
  75. '#title' => t('Display summary'),
  76. '#options' => array(
  77. t('No'),
  78. t('Yes'),
  79. ),
  80. '#description' => t('Display the summary to allow the user to input a summary value. Hide the summary to automatically fill it with a trimmed portion from the main post.'),
  81. '#default_value' => !empty($settings['display_summary']) ? $settings['display_summary'] : 0,
  82. );
  83. }
  84. return $form;
  85. }
  86. /**
  87. * Add settings to a widget settings form.
  88. *
  89. * Invoked from field_ui_field_edit_form() to allow the module defining the
  90. * widget to add settings for a widget instance.
  91. *
  92. * @param $field
  93. * The field structure being configured.
  94. * @param $instance
  95. * The instance structure being configured.
  96. *
  97. * @return
  98. * The form definition for the widget settings.
  99. */
  100. function hook_field_widget_settings_form($field, $instance) {
  101. $widget = $instance['widget'];
  102. $settings = $widget['settings'];
  103. if ($widget['type'] == 'text_textfield') {
  104. $form['size'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Size of textfield'),
  107. '#default_value' => $settings['size'],
  108. '#element_validate' => array('element_validate_integer_positive'),
  109. '#required' => TRUE,
  110. );
  111. }
  112. else {
  113. $form['rows'] = array(
  114. '#type' => 'textfield',
  115. '#title' => t('Rows'),
  116. '#default_value' => $settings['rows'],
  117. '#element_validate' => array('element_validate_integer_positive'),
  118. '#required' => TRUE,
  119. );
  120. }
  121. return $form;
  122. }
  123. /**
  124. * Specify the form elements for a formatter's settings.
  125. *
  126. * This hook is only invoked if hook_field_formatter_settings_summary()
  127. * returns a non-empty value.
  128. *
  129. * @param $field
  130. * The field structure being configured.
  131. * @param $instance
  132. * The instance structure being configured.
  133. * @param $view_mode
  134. * The view mode being configured.
  135. * @param $form
  136. * The (entire) configuration form array, which will usually have no use here.
  137. * @param $form_state
  138. * The form state of the (entire) configuration form.
  139. *
  140. * @return
  141. * The form elements for the formatter settings.
  142. */
  143. function hook_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  144. $display = $instance['display'][$view_mode];
  145. $settings = $display['settings'];
  146. $element = array();
  147. if ($display['type'] == 'text_trimmed' || $display['type'] == 'text_summary_or_trimmed') {
  148. $element['trim_length'] = array(
  149. '#title' => t('Length'),
  150. '#type' => 'textfield',
  151. '#size' => 20,
  152. '#default_value' => $settings['trim_length'],
  153. '#element_validate' => array('element_validate_integer_positive'),
  154. '#required' => TRUE,
  155. );
  156. }
  157. return $element;
  158. }
  159. /**
  160. * Return a short summary for the current formatter settings of an instance.
  161. *
  162. * If an empty result is returned, the formatter is assumed to have no
  163. * configurable settings, and no UI will be provided to display a settings
  164. * form.
  165. *
  166. * @param $field
  167. * The field structure.
  168. * @param $instance
  169. * The instance structure.
  170. * @param $view_mode
  171. * The view mode for which a settings summary is requested.
  172. *
  173. * @return
  174. * A string containing a short summary of the formatter settings.
  175. */
  176. function hook_field_formatter_settings_summary($field, $instance, $view_mode) {
  177. $display = $instance['display'][$view_mode];
  178. $settings = $display['settings'];
  179. $summary = '';
  180. if ($display['type'] == 'text_trimmed' || $display['type'] == 'text_summary_or_trimmed') {
  181. $summary = t('Length: @chars chars', array('@chars' => $settings['trim_length']));
  182. }
  183. return $summary;
  184. }
  185. /**
  186. * @} End of "addtogroup field_types".
  187. */