editableviews_handler_field_field_edit.inc

  1. nittany7 modules/contrib/editableviews/handlers/editableviews_handler_field_field_edit.inc
  2. cis7 modules/contrib/editableviews/handlers/editableviews_handler_field_field_edit.inc

Classes

Namesort descending Description
editableviews_handler_field_field_edit Field handler for toggling between rendered value and edit form element.

File

modules/contrib/editableviews/handlers/editableviews_handler_field_field_edit.inc
View source
  1. <?php
  2. /**
  3. * Field handler for toggling between rendered value and edit form element.
  4. *
  5. * TODO: figure out a way to show the widget type in the admin summary??
  6. */
  7. class editableviews_handler_field_field_edit extends views_handler_field_field {
  8. /**
  9. * Boolean to indicate to the style plugin that this field is editable.
  10. *
  11. * We do this here rather than just check the class parentage to allow field
  12. * handlers to provide form elements for non-FieldAPI entity properties.
  13. */
  14. public $editable = TRUE;
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. // We can't realistically provide a default widget type, because there may
  18. // be entities of different bundles in the view result, and therefore
  19. // different field instances with different widgets.
  20. $options['widget_type'] = array('default' => NULL);
  21. $options['suppress_label'] = array('default' => FALSE);
  22. $options['suppress_description'] = array('default' => FALSE);
  23. return $options;
  24. }
  25. function options_form(&$form, &$form_state) {
  26. parent::options_form($form, $form_state);
  27. $field = field_info_field($this->definition['field_name']);
  28. module_load_include('inc', 'field_ui', 'field_ui.admin');
  29. $widget_options = array(
  30. // Banking on there being no widget type called '0', which is a reasonable
  31. // assumption to make!
  32. 0 => t('Default'),
  33. );
  34. $widget_options += field_ui_widget_type_options($field['type']);
  35. $form['widget_type'] = array(
  36. '#type' => 'select',
  37. '#title' => t('Widget type'),
  38. '#options' => $widget_options,
  39. '#default_value' => $this->options['widget_type'],
  40. '#description' => t("The type of form element you would like to present to the user when editing this field." . ' ' .
  41. "'Default' will use the widget from field settings, and may result in different widgets showing if the entities are of different bundles."),
  42. );
  43. $form['suppress_label'] = array(
  44. '#type' => 'checkbox',
  45. '#title' => t('Hide widget label'),
  46. '#description' => t('If selected, the label on field widget is hidden. (If the field is required, the * will still show.)'),
  47. '#default_value' => $this->options['suppress_label'],
  48. );
  49. $form['suppress_description'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Hide field widget description'),
  52. '#description' => t('If selected, the description on field widgets is hidden.'),
  53. '#default_value' => $this->options['suppress_description'],
  54. );
  55. }
  56. /**
  57. * Add the form element for this handler's field to the form.
  58. *
  59. * We break this out into the handler to allow other handlers that work with
  60. * non-FieldAPI fields (eg node title) to also provide a form element.
  61. *
  62. * @param $entity_type
  63. * The entity type.
  64. * @param $entity
  65. * The entity.
  66. * @param &$element
  67. * The partial form, at $form[ENTITY_ID]. This is passed by reference and
  68. * should be altered in place.
  69. * @param &$form_state
  70. * The form state.
  71. */
  72. function edit_form($entity_type, $entity, &$element, &$form_state) {
  73. list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  74. // TODO: this prevents this method being static. This would mean we could
  75. // store class names in the form rather than handler objects, which would
  76. // considerably save on form cache size!
  77. $field_name = $this->definition['field_name'];
  78. $field_instance = field_info_instance($entity_type, $field_name, $bundle);
  79. // Because a View result can show entities of different bundles, it is
  80. // essential that we check the field actually exists on the current entity.
  81. // ctools_field_invoke_field() does actually check for this too, but that
  82. // only works when it's passed a field name rather than a whole instance.
  83. if (empty($field_instance)) {
  84. return;
  85. }
  86. // TODO: Faffy to have to keep doing this for the entity in each handler!!!!
  87. if (isset($entity->language)) {
  88. $langcode = field_valid_language($entity->language);
  89. }
  90. else {
  91. $langcode = field_valid_language(NULL);
  92. }
  93. if (!empty($this->options['suppress_label'])) {
  94. $field_instance['label'] = '';
  95. }
  96. if (!empty($this->options['suppress_description'])) {
  97. $field_instance['description'] = '';
  98. }
  99. // Only doctor the widget type if the option is set; otherwise the widget
  100. // type set for the entity's bundle will be used.
  101. if (!empty($this->options['widget_type'])) {
  102. $field_instance['widget']['type'] = $this->options['widget_type'];
  103. }
  104. // On new entities, force this to not be required, to allow the user to not
  105. // create the new entity.
  106. // Obviously, problems arise when there are multiple editable fields on
  107. // this entity, since the title *is* required if actually creating an
  108. // entity!
  109. // TODO: consider this thorny problem.
  110. if (!empty($entity->is_new)) {
  111. $field_instance['required'] = FALSE;
  112. }
  113. // If no language is provided use the default site language.
  114. $field_invoke_options = array(
  115. 'language' => $langcode,
  116. 'default' => TRUE,
  117. );
  118. $element += (array) ctools_field_invoke_field($field_instance, 'form', $entity_type, $entity, $element, $form_state, $field_invoke_options);
  119. }
  120. /**
  121. * Handle the form validation for this field's form element.
  122. */
  123. function edit_form_validate($entity_type, $entity, &$element, &$form_state) {
  124. list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  125. $field_name = $this->definition['field_name'];
  126. $field_instance = field_info_instance($entity_type, $field_name, $bundle);
  127. // Because a View result can show entities of different bundles, it is
  128. // essential that we check the field actually exists on the current entity.
  129. // ctools_field_invoke_field() does actually check for this too, but that
  130. // only works when it's passed a field name rather than a whole instance.
  131. if (empty($field_instance)) {
  132. return;
  133. }
  134. // Start with a fresh errors array for each field.
  135. $errors = array();
  136. // Extract field values from submitted values.
  137. // We pass a partial $form array to the Field API hook. This contains at
  138. // its base the #parents array, which tells Field API where to look for
  139. // the values in $form_state.
  140. ctools_field_invoke_field_default($field_instance, 'extract_form_values', $entity_type, $entity, $element, $form_state);
  141. // Check generic, field-type-agnostic errors first.
  142. ctools_field_invoke_field_default($field_instance, 'validate', $entity_type, $entity, $errors);
  143. // Check field-type specific errors.
  144. ctools_field_invoke_field($field_instance, 'validate', $entity_type, $entity, $errors);
  145. // Let other modules validate the entity.
  146. // Avoid module_invoke_all() to let $errors be taken by reference.
  147. foreach (module_implements('field_attach_validate') as $module) {
  148. $function = $module . '_field_attach_validate';
  149. $function($entity_type, $entity, $errors);
  150. }
  151. if ($errors) {
  152. // Pass field-level validation errors back to widgets for accurate error
  153. // flagging.
  154. foreach ($errors as $field_errors) {
  155. foreach ($field_errors as $langcode => $errors) {
  156. $field_state = field_form_get_state($element['#parents'], $field_name, $langcode, $form_state);
  157. $field_state['errors'] = $errors;
  158. field_form_set_state($element['#parents'], $field_name, $langcode, $form_state, $field_state);
  159. }
  160. }
  161. ctools_field_invoke_field_default($field_instance, 'form_errors', $entity_type, $entity, $element, $form_state);
  162. }
  163. }
  164. /**
  165. * Handle the form submission for this field's form element.
  166. */
  167. function edit_form_submit($entity_type, $entity, &$element, &$form_state) {
  168. list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  169. $field_name = $this->definition['field_name'];
  170. $field_instance = field_info_instance($entity_type, $field_name, $bundle);
  171. // Because a View result can show entities of different bundles, it is
  172. // essential that we check the field actually exists on the current entity.
  173. // ctools_field_invoke_field() does actually check for this too, but that
  174. // only works when it's passed a field name rather than a whole instance.
  175. if (empty($field_instance)) {
  176. return;
  177. }
  178. // Extract field values from submitted values.
  179. // We pass the partial $form array to the Field API hook. This contains at
  180. // its base the #parents array, which tells Field API where to look for
  181. // the values in $form_state.
  182. ctools_field_invoke_field_default($field_instance, 'extract_form_values', $entity_type, $entity, $element, $form_state);
  183. ctools_field_invoke_field_default($field_instance, 'submit', $entity_type, $entity, $element, $form_state);
  184. }
  185. }