field_extrawidgets.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Provides two extra widgets for the Field API.
  5. *
  6. * Provides two extra widgets:
  7. *
  8. * - Hidden: this widget doesn't show anything on the edit form, and as a
  9. * consequence prevents the user from modifying the content of the widget.
  10. * This is useful in some situations where using hook_field_access() is not
  11. * suitable (for example when you want to have different edit forms for
  12. * different users).
  13. * - Read-only: this widget shows the content of the field on the edit form,
  14. * but doesn't allow the user to edit it. The content is rendered using one
  15. * of the formatter of the field.
  16. */
  17. /**
  18. * Implements hook_field_widget_info().
  19. */
  20. function field_extrawidgets_field_widget_info() {
  21. $widgets['field_extrawidgets_hidden'] = array(
  22. 'label' => t('Hidden'),
  23. 'description' => t('A widget that is completely hidden.'),
  24. 'field types' => array_keys(field_info_field_types()),
  25. 'settings' => array(),
  26. 'behaviors' => array(
  27. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  28. ),
  29. 'weight' => 10,
  30. );
  31. $widgets['field_extrawidgets_read_only'] = array(
  32. 'label' => t('Read-only'),
  33. 'description' => t('A widget that is read-only.'),
  34. 'field types' => array_keys(field_info_field_types()),
  35. 'settings' => array(
  36. 'formatter' => '',
  37. 'formatter_settings' => array(),
  38. 'display_empty' => 0,
  39. ),
  40. 'behaviors' => array(
  41. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  42. ),
  43. 'weight' => 11,
  44. );
  45. return $widgets;
  46. }
  47. /**
  48. * Get settings for a widget, merging in default values.
  49. */
  50. function field_extrawidgets_widget_settings($field, $instance) {
  51. $settings = isset($instance['widget']['settings']) ? $instance['widget']['settings'] : array();
  52. if (empty($settings['formatter'])) {
  53. $field_type = field_info_field_types($field['type']);
  54. $settings['formatter'] = $field_type['default_formatter'];
  55. $settings['formatter_settings'] = field_info_formatter_settings($settings['formatter']);
  56. }
  57. return $settings;
  58. }
  59. /**
  60. * Implements hook_field_widget_settings_form().
  61. */
  62. function field_extrawidgets_field_widget_settings_form($field, $instance) {
  63. // The Hidden widget doesn't have any settings.
  64. if ($instance['widget']['type'] != 'field_extrawidgets_read_only') {
  65. return array();
  66. }
  67. $settings = field_extrawidgets_widget_settings($field, $instance);
  68. // Include the field_ui admin form.
  69. module_load_include('inc', 'field_ui', 'field_ui.admin');
  70. // Get the list of formatters for this field type, and remove our own.
  71. $formatters = field_ui_formatter_options($field['type']);
  72. $element['formatter'] = array(
  73. '#type' => 'select',
  74. '#title' => t('Formatter'),
  75. '#options' => $formatters,
  76. '#default_value' => $settings['formatter'],
  77. // AJAX refresh the settings area when the formatter is changed.
  78. '#ajax' => array(
  79. 'callback' => 'field_extrawidgets_field_widget_settings_ajax',
  80. 'wrapper' => 'field_extrawidgets-formatter-settings',
  81. ),
  82. '#limit_validation_errors' => array(),
  83. );
  84. // Get the settings form.
  85. $element['formatter_settings'] = array(
  86. '#type' => 'container',
  87. '#id' => 'field_extrawidgets-formatter-settings',
  88. '#process' => array('field_extrawidgets_field_widget_render_settings'),
  89. '#tree' => TRUE,
  90. '#field' => $field,
  91. '#instance' => $instance,
  92. );
  93. $element['display_empty'] = array(
  94. '#type' => 'checkbox',
  95. '#title' => t('Display %text if empty', array('%text' => t('Not defined'))),
  96. '#default_value' => $settings['display_empty'],
  97. );
  98. return $element;
  99. }
  100. /**
  101. * Form #process callback: generates the formatter settings form.
  102. */
  103. function field_extrawidgets_field_widget_render_settings($element, $form_state, $form) {
  104. $parents = $element['#parents'];
  105. array_pop($parents);
  106. $parents[] = 'formatter';
  107. $formatter_type = drupal_array_get_nested_value($form_state['values'], $parents);
  108. $field = $element['#field'];
  109. $instance = $element['#instance'];
  110. $formatter = field_info_formatter_types($formatter_type);
  111. $formatter_settings = array();
  112. if (isset($form_state['values']['instance']['widget']['settings']['formatter_settings'])) {
  113. $formatter_settings += $form_state['values']['instance']['widget']['settings']['formatter_settings'];
  114. }
  115. elseif (isset($instance['widget']['settings']['formatter_settings'])) {
  116. $formatter_settings += $instance['widget']['settings']['formatter_settings'];
  117. }
  118. $formatter_settings += field_info_formatter_settings($formatter_type);
  119. $settings_form = array('#value' => array());
  120. $function = $formatter['module'] . '_field_formatter_settings_form';
  121. $instance['display']['_dummy'] = array(
  122. 'type' => $formatter_type,
  123. 'settings' => $formatter_settings,
  124. );
  125. if (function_exists($function)) {
  126. $element += $function($field, $instance, '_dummy', $form, $form_state);
  127. }
  128. return $element;
  129. }
  130. /**
  131. * Form AJAX callback: refresh the formatter settings form.
  132. */
  133. function field_extrawidgets_field_widget_settings_ajax($form, $form_state) {
  134. $trigger = $form_state['triggering_element'];
  135. $parents = $trigger['#array_parents'];
  136. // Pop the container.
  137. array_pop($parents);
  138. $parents[] = 'formatter_settings';
  139. $element = drupal_array_get_nested_value($form, $parents);
  140. return $element;
  141. }
  142. /**
  143. * Implements hook_field_widget_form().
  144. */
  145. function field_extrawidgets_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  146. $widget_type = $instance['widget']['type'];
  147. $widget_settings = field_extrawidgets_widget_settings($field, $instance);
  148. if ($widget_type == 'field_extrawidgets_hidden') {
  149. return array();
  150. }
  151. if ($widget_type == 'field_extrawidgets_read_only') {
  152. // Clone the entity to avoid messing with it.
  153. $entity_type = $element['#entity_type'];
  154. if (!isset($form_state[$entity_type])) {
  155. return array();
  156. }
  157. $cloned_entity = clone $form_state[$entity_type];
  158. if (!field_access('view', $field, $entity_type, $cloned_entity)) {
  159. // The current user doesn't have access to view this field.
  160. return array();
  161. }
  162. // Set the fallback formatter.
  163. $display['type'] = $widget_settings['formatter'];
  164. $display['settings'] = $widget_settings['formatter_settings'];
  165. $formatter_type = field_info_formatter_types($display['type']);
  166. $display['module'] = $formatter_type['module'];
  167. $element += field_view_field($entity_type, $cloned_entity, $field['field_name'], $display, $langcode);
  168. // If asked to display a placeholder text, detect if the result is empty.
  169. if (!isset($element[0]) && !empty($widget_settings['display_empty'])) {
  170. $display = _field_info_prepare_instance_display($field, $display);
  171. // Generate the field structure as done in field_default_view().
  172. $element += array(
  173. '#theme' => 'field',
  174. '#weight' => $display['weight'],
  175. '#title' => $instance['label'],
  176. '#access' => TRUE,
  177. '#label_display' => $display['label'],
  178. '#view_mode' => '_custom_display',
  179. '#language' => $langcode,
  180. '#field_name' => $field['field_name'],
  181. '#field_type' => $field['type'],
  182. '#field_translatable' => $field['translatable'],
  183. '#entity_type' => $entity_type,
  184. '#bundle' => '',
  185. '#object' => $cloned_entity,
  186. '#items' => array(0 => ''),
  187. '#formatter' => $display['type']
  188. );
  189. $element[0] = array(
  190. '#markup' => t('Not defined'),
  191. );
  192. }
  193. return $element;
  194. }
  195. }