WidgetBaseInterface.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Symfony\Component\Validator\ConstraintViolationListInterface;
  5. /**
  6. * Base interface definition for "Field widget" plugins.
  7. *
  8. * This interface details base wrapping methods that most widget implementations
  9. * will want to directly inherit from Drupal\Core\Field\WidgetBase. See
  10. * Drupal\Core\Field\WidgetInterface for methods that will more likely be
  11. * overridden in actual widget implementations.
  12. */
  13. interface WidgetBaseInterface extends PluginSettingsInterface {
  14. /**
  15. * Creates a form element for a field.
  16. *
  17. * If the entity associated with the form is new (i.e., $entity->isNew() is
  18. * TRUE), the 'default value', if any, is pre-populated. Also allows other
  19. * modules to alter the form element by implementing their own hooks.
  20. *
  21. * @param \Drupal\Core\Field\FieldItemListInterface $items
  22. * An array of the field values. When creating a new entity this may be NULL
  23. * or an empty array to use default values.
  24. * @param array $form
  25. * An array representing the form that the editing element will be attached
  26. * to.
  27. * @param \Drupal\Core\Form\FormStateInterface $form_state
  28. * The current state of the form.
  29. * @param int $get_delta
  30. * Used to get only a specific delta value of a multiple value field.
  31. *
  32. * @return array
  33. * The form element array created for this field.
  34. */
  35. public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL);
  36. /**
  37. * Extracts field values from submitted form values.
  38. *
  39. * @param \Drupal\Core\Field\FieldItemListInterface $items
  40. * The field values. This parameter is altered by reference to receive the
  41. * incoming form values.
  42. * @param array $form
  43. * The form structure where field elements are attached to. This might be a
  44. * full form structure, or a sub-element of a larger form.
  45. * @param \Drupal\Core\Form\FormStateInterface $form_state
  46. * The form state.
  47. */
  48. public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state);
  49. /**
  50. * Reports field-level validation errors against actual form elements.
  51. *
  52. * @param \Drupal\Core\Field\FieldItemListInterface $items
  53. * The field values.
  54. * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations
  55. * A list of constraint violations to flag.
  56. * @param array $form
  57. * The form structure where field elements are attached to. This might be a
  58. * full form structure, or a sub-element of a larger form.
  59. * @param \Drupal\Core\Form\FormStateInterface $form_state
  60. * The form state.
  61. */
  62. public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state);
  63. /**
  64. * Retrieves processing information about the widget from $form_state.
  65. *
  66. * This method is static so that it can be used in static Form API callbacks.
  67. *
  68. * @param array $parents
  69. * The array of #parents where the field lives in the form.
  70. * @param string $field_name
  71. * The field name.
  72. * @param \Drupal\Core\Form\FormStateInterface $form_state
  73. * The form state.
  74. *
  75. * @return array
  76. * An array with the following key/value pairs:
  77. * - items_count: The number of widgets to display for the field.
  78. * - array_parents: The location of the field's widgets within the $form
  79. * structure. This entry is populated at '#after_build' time.
  80. */
  81. public static function getWidgetState(array $parents, $field_name, FormStateInterface $form_state);
  82. /**
  83. * Stores processing information about the widget in $form_state.
  84. *
  85. * This method is static so that it can be used in static Form API #callbacks.
  86. *
  87. * @param array $parents
  88. * The array of #parents where the widget lives in the form.
  89. * @param string $field_name
  90. * The field name.
  91. * @param \Drupal\Core\Form\FormStateInterface $form_state
  92. * The form state.
  93. * @param array $field_state
  94. * The array of data to store. See getWidgetState() for the structure and
  95. * content of the array.
  96. */
  97. public static function setWidgetState(array $parents, $field_name, FormStateInterface $form_state, array $field_state);
  98. }