EntityFormDisplayInterface.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Drupal\Core\Entity\Display;
  3. use Drupal\Core\Entity\EntityConstraintViolationListInterface;
  4. use Drupal\Core\Entity\FieldableEntityInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Provides a common interface for entity form displays.
  8. */
  9. interface EntityFormDisplayInterface extends EntityDisplayInterface {
  10. /**
  11. * Adds field widgets to an entity form.
  12. *
  13. * The form elements for the entity's fields are added by reference as direct
  14. * children in the $form parameter. This parameter can be a full form
  15. * structure (most common case for entity edit forms), or a sub-element of a
  16. * larger form.
  17. *
  18. * By default, submitted field values appear at the top-level of
  19. * $form_state->getValues(). A different location within
  20. * $form_state->getValues() can be specified by setting the '#parents'
  21. * property on the incoming $form parameter. Because of name clashes, two
  22. * instances of the same field cannot appear within the same $form element, or
  23. * within the same '#parents' space.
  24. *
  25. * Sample resulting structure in $form:
  26. * @code
  27. * '#parents' => The location of field values in $form_state->getValues(),
  28. * '#entity_type' => The name of the entity type,
  29. * '#bundle' => The name of the bundle,
  30. * // One sub-array per field appearing in the entity, keyed by field name.
  31. * // The structure of the array differs slightly depending on whether the
  32. * // widget is 'single-value' (provides the input for one field value,
  33. * // most common case), and will therefore be repeated as many times as
  34. * // needed, or 'multiple-values' (one single widget allows the input of
  35. * // several values; e.g., checkboxes, select box, etc.).
  36. * 'field_foo' => array(
  37. * '#access' => TRUE if the current user has 'edit' grants for the field,
  38. * FALSE if not.
  39. * 'widget' => array(
  40. * '#field_name' => The name of the field,
  41. * '#title' => The label of the field,
  42. * '#description' => The description text for the field,
  43. * '#required' => Whether or not the field is required,
  44. * '#field_parents' => The 'parents' space for the field in the form,
  45. * equal to the #parents property of the $form parameter received by
  46. * this method,
  47. *
  48. * // For 'multiple-value' widgets, the remaining elements in the
  49. * // sub-array depend on the widget.
  50. *
  51. * // For 'single-value' widgets:
  52. * '#theme' => 'field_multiple_value_form',
  53. * '#cardinality' => The field cardinality,
  54. * '#cardinality_multiple' => TRUE if the field can contain multiple
  55. * items, FALSE otherwise.
  56. * // One sub-array per copy of the widget, keyed by delta.
  57. * 0 => array(
  58. * '#title' => The title to be displayed by the widget,
  59. * '#description' => The description text for the field,
  60. * '#required' => Whether the widget should be marked required,
  61. * '#delta' => 0,
  62. * '#weight' => 0,
  63. * '#field_parents' => Same as above,
  64. * // The remaining elements in the sub-array depend on the widget.
  65. * ...
  66. * ),
  67. * 1 => array(
  68. * ...
  69. * ),
  70. * ...
  71. * ),
  72. * ...
  73. * ),
  74. * )
  75. * @endcode
  76. *
  77. * Additionally, some processing data is placed in $form_state, and can be
  78. * accessed by \Drupal\Core\Field\WidgetBaseInterface::getWidgetState() and
  79. * \Drupal\Core\Field\WidgetBaseInterface::setWidgetState().
  80. *
  81. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  82. * The entity.
  83. * @param array $form
  84. * The form structure to fill in. This can be a full form structure, or a
  85. * sub-element of a larger form. The #parents property can be set to
  86. * control the location of submitted field values within
  87. * $form_state->getValues(). If not specified, $form['#parents'] is set to
  88. * an empty array, which results in field values located at the top-level of
  89. * $form_state->getValues().
  90. * @param \Drupal\Core\Form\FormStateInterface $form_state
  91. * The form state.
  92. */
  93. public function buildForm(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state);
  94. /**
  95. * Extracts field values from the submitted widget values into the entity.
  96. *
  97. * This accounts for drag-and-drop reordering of field values, and filtering
  98. * of empty values.
  99. *
  100. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  101. * The entity.
  102. * @param array $form
  103. * The form structure where field elements are attached to. This might be a
  104. * full form structure, or a sub-element of a larger form.
  105. * @param \Drupal\Core\Form\FormStateInterface $form_state
  106. * The form state.
  107. *
  108. * @return array
  109. * An array whose keys and values are the keys of the top-level entries in
  110. * $form_state->getValues() that have been processed. The remaining entries,
  111. * if any, do not correspond to widgets and should be extracted manually by
  112. * the caller if needed.
  113. */
  114. public function extractFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state);
  115. /**
  116. * Validates submitted widget values and sets the corresponding form errors.
  117. *
  118. * This method invokes entity validation and takes care of flagging them on
  119. * the form. This is particularly useful when all elements on the form are
  120. * managed by the form display.
  121. *
  122. * As an alternative, entity validation can be invoked separately such that
  123. * some violations can be flagged manually. In that case
  124. * \Drupal\Core\Entity\Display\EntityFormDisplayInterface::flagViolations()
  125. * must be used for flagging violations related to the form display.
  126. *
  127. * Note that there are two levels of validation for fields in forms: widget
  128. * validation and field validation:
  129. * - Widget validation steps are specific to a given widget's own form
  130. * structure and UI metaphors. They are executed during normal form
  131. * validation, usually through Form API's #element_validate property.
  132. * Errors reported at this level are typically those that prevent the
  133. * extraction of proper field values from the submitted form input.
  134. * - If no form / widget errors were reported for the field, field validation
  135. * steps are performed according to the "constraints" specified by the
  136. * field definition as part of the entity validation. That validation is
  137. * independent of the specific widget being used in a given form, and is
  138. * also performed on REST entity submissions.
  139. *
  140. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  141. * The entity.
  142. * @param array $form
  143. * The form structure where field elements are attached to. This might be a
  144. * full form structure, or a sub-element of a larger form.
  145. * @param \Drupal\Core\Form\FormStateInterface $form_state
  146. * The form state.
  147. */
  148. public function validateFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state);
  149. /**
  150. * Flags entity validation violations as form errors.
  151. *
  152. * This method processes all violations passed, thus any violations not
  153. * related to fields of the form display must be processed before this method
  154. * is invoked.
  155. *
  156. * The method flags constraint violations related to fields shown on the
  157. * form as form errors on the correct form elements. Possibly pre-existing
  158. * violations of hidden fields (so fields not appearing in the display) are
  159. * ignored. Other, non-field related violations are passed through and set as
  160. * form errors according to the property path of the violations.
  161. *
  162. * @param \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations
  163. * The violations to flag.
  164. * @param array $form
  165. * The form structure where field elements are attached to. This might be a
  166. * full form structure, or a sub-element of a larger form.
  167. * @param \Drupal\Core\Form\FormStateInterface $form_state
  168. * The form state.
  169. */
  170. public function flagWidgetsErrorsFromViolations(EntityConstraintViolationListInterface $violations, array &$form, FormStateInterface $form_state);
  171. }