PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Field/WidgetInterface.php

http://github.com/drupal/drupal
PHP | 161 lines | 12 code | 10 blank | 139 comment | 0 complexity | aa6ab3908b38f12fa86fa77debb1a869 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Symfony\Component\Validator\ConstraintViolationInterface;
  5. /**
  6. * Interface definition for field widget plugins.
  7. *
  8. * This interface details the methods that most plugin implementations will want
  9. * to override. See Drupal\Core\Field\WidgetBaseInterface for base
  10. * wrapping methods that should most likely be inherited directly from
  11. * Drupal\Core\Field\WidgetBase..
  12. *
  13. * @ingroup field_widget
  14. */
  15. interface WidgetInterface extends WidgetBaseInterface {
  16. /**
  17. * Returns a form to configure settings for the widget.
  18. *
  19. * Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow
  20. * administrators to configure the widget. The field_ui module takes care of
  21. * handling submitted form values.
  22. *
  23. * @param array $form
  24. * The form where the settings form is being included in.
  25. * @param \Drupal\Core\Form\FormStateInterface $form_state
  26. * The current state of the form.
  27. *
  28. * @return array
  29. * The form definition for the widget settings.
  30. */
  31. public function settingsForm(array $form, FormStateInterface $form_state);
  32. /**
  33. * Returns a short summary for the current widget settings.
  34. *
  35. * If an empty result is returned, a UI can still be provided to display
  36. * a settings form in case the widget has configurable settings.
  37. *
  38. * @return array
  39. * A short summary of the widget settings.
  40. */
  41. public function settingsSummary();
  42. /**
  43. * Returns the form for a single field widget.
  44. *
  45. * Field widget form elements should be based on the passed-in $element, which
  46. * contains the base form element properties derived from the field
  47. * configuration.
  48. *
  49. * The BaseWidget methods will set the weight, field name and delta values for
  50. * each form element. If there are multiple values for this field, the
  51. * formElement() method will be called as many times as needed.
  52. *
  53. * Other modules may alter the form element provided by this function using
  54. * hook_field_widget_form_alter() or
  55. * hook_field_widget_WIDGET_TYPE_form_alter().
  56. *
  57. * The FAPI element callbacks (such as #process, #element_validate,
  58. * #value_callback, etc.) used by the widget do not have access to the
  59. * original $field_definition passed to the widget's constructor. Therefore,
  60. * if any information is needed from that definition by those callbacks, the
  61. * widget implementing this method, or a
  62. * hook_field_widget[_WIDGET_TYPE]_form_alter() implementation, must extract
  63. * the needed properties from the field definition and set them as ad-hoc
  64. * $element['#custom'] properties, for later use by its element callbacks.
  65. *
  66. * @param \Drupal\Core\Field\FieldItemListInterface $items
  67. * Array of default values for this field.
  68. * @param int $delta
  69. * The order of this item in the array of sub-elements (0, 1, 2, etc.).
  70. * @param array $element
  71. * A form element array containing basic properties for the widget:
  72. * - #field_parents: The 'parents' space for the field in the form. Most
  73. * widgets can simply overlook this property. This identifies the
  74. * location where the field values are placed within
  75. * $form_state->getValues(), and is used to access processing
  76. * information for the field through the getWidgetState() and
  77. * setWidgetState() methods.
  78. * - #title: The sanitized element label for the field, ready for output.
  79. * - #description: The sanitized element description for the field, ready
  80. * for output.
  81. * - #required: A Boolean indicating whether the element value is required;
  82. * for required multiple value fields, only the first widget's values are
  83. * required.
  84. * - #delta: The order of this item in the array of sub-elements; see $delta
  85. * above.
  86. * @param array $form
  87. * The form structure where widgets are being attached to. This might be a
  88. * full form structure, or a sub-element of a larger form.
  89. * @param \Drupal\Core\Form\FormStateInterface $form_state
  90. * The current state of the form.
  91. *
  92. * @return array
  93. * The form elements for a single widget for this field.
  94. *
  95. * @see hook_field_widget_form_alter()
  96. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  97. */
  98. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state);
  99. /**
  100. * Assigns a field-level validation error to the right widget sub-element.
  101. *
  102. * Depending on the widget's internal structure, a field-level validation
  103. * error needs to be flagged on the right sub-element.
  104. *
  105. * @param array $element
  106. * An array containing the form element for the widget, as generated by
  107. * formElement().
  108. * @param \Symfony\Component\Validator\ConstraintViolationInterface $violation
  109. * A constraint violation reported during the validation phase.
  110. * @param array $form
  111. * The form structure where field elements are attached to. This might be a
  112. * full form structure, or a sub-element of a larger form.
  113. * @param \Drupal\Core\Form\FormStateInterface $form_state
  114. * The current state of the form.
  115. *
  116. * @return array|bool
  117. * The element on which the error should be flagged, or FALSE to completely
  118. * ignore the violation (use with care!).
  119. */
  120. public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state);
  121. /**
  122. * Massages the form values into the format expected for field values.
  123. *
  124. * @param array $values
  125. * The submitted form values produced by the widget.
  126. * - If the widget does not manage multiple values itself, the array holds
  127. * the values generated by the multiple copies of the $element generated
  128. * by the formElement() method, keyed by delta.
  129. * - If the widget manages multiple values, the array holds the values
  130. * of the form element generated by the formElement() method.
  131. * @param array $form
  132. * The form structure where field elements are attached to. This might be a
  133. * full form structure, or a sub-element of a larger form.
  134. * @param \Drupal\Core\Form\FormStateInterface $form_state
  135. * The form state.
  136. *
  137. * @return array
  138. * An array of field values, keyed by delta.
  139. */
  140. public function massageFormValues(array $values, array $form, FormStateInterface $form_state);
  141. /**
  142. * Returns if the widget can be used for the provided field.
  143. *
  144. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  145. * The field definition that should be checked.
  146. *
  147. * @return bool
  148. * TRUE if the widget can be used, FALSE otherwise.
  149. */
  150. public static function isApplicable(FieldDefinitionInterface $field_definition);
  151. }