FieldItemListInterface.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Entity\FieldableEntityInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Drupal\Core\Access\AccessibleInterface;
  7. use Drupal\Core\TypedData\ListInterface;
  8. /**
  9. * Interface for fields, being lists of field items.
  10. *
  11. * This interface must be implemented by every entity field, whereas contained
  12. * field items must implement the FieldItemInterface.
  13. * Some methods of the fields are delegated to the first contained item, in
  14. * particular get() and set() as well as their magic equivalences.
  15. *
  16. * Optionally, a typed data object implementing
  17. * Drupal\Core\TypedData\TypedDataInterface may be passed to
  18. * ArrayAccess::offsetSet() instead of a plain value.
  19. *
  20. * When implementing this interface which extends Traversable, make sure to list
  21. * IteratorAggregate or Iterator before this interface in the implements clause.
  22. *
  23. * @see \Drupal\Core\Field\FieldItemInterface
  24. */
  25. interface FieldItemListInterface extends ListInterface, AccessibleInterface {
  26. /**
  27. * Gets the entity that field belongs to.
  28. *
  29. * @return \Drupal\Core\Entity\FieldableEntityInterface
  30. * The entity object.
  31. */
  32. public function getEntity();
  33. /**
  34. * Sets the langcode of the field values held in the object.
  35. *
  36. * @param string $langcode
  37. * The langcode.
  38. */
  39. public function setLangcode($langcode);
  40. /**
  41. * Gets the langcode of the field values held in the object.
  42. *
  43. * @return string
  44. * The langcode.
  45. */
  46. public function getLangcode();
  47. /**
  48. * Gets the field definition.
  49. *
  50. * @return \Drupal\Core\Field\FieldDefinitionInterface
  51. * The field definition.
  52. */
  53. public function getFieldDefinition();
  54. /**
  55. * Returns the array of field settings.
  56. *
  57. * @return array
  58. * An array of key/value pairs.
  59. */
  60. public function getSettings();
  61. /**
  62. * Returns the value of a given field setting.
  63. *
  64. * @param string $setting_name
  65. * The setting name.
  66. *
  67. * @return mixed
  68. * The setting value.
  69. */
  70. public function getSetting($setting_name);
  71. /**
  72. * Contains the default access logic of this field.
  73. *
  74. * See \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess() for
  75. * the parameter documentation.
  76. *
  77. * @return \Drupal\Core\Access\AccessResultInterface
  78. * The access result.
  79. */
  80. public function defaultAccess($operation = 'view', AccountInterface $account = NULL);
  81. /**
  82. * Filters out empty field items and re-numbers the item deltas.
  83. *
  84. * @return $this
  85. */
  86. public function filterEmptyItems();
  87. /**
  88. * Magic method: Gets a property value of to the first field item.
  89. *
  90. * @see \Drupal\Core\Field\FieldItemInterface::__set()
  91. */
  92. public function __get($property_name);
  93. /**
  94. * Magic method: Sets a property value of the first field item.
  95. *
  96. * @see \Drupal\Core\Field\FieldItemInterface::__get()
  97. */
  98. public function __set($property_name, $value);
  99. /**
  100. * Magic method: Determines whether a property of the first field item is set.
  101. *
  102. * @see \Drupal\Core\Field\FieldItemInterface::__unset()
  103. */
  104. public function __isset($property_name);
  105. /**
  106. * Magic method: Unsets a property of the first field item.
  107. *
  108. * @see \Drupal\Core\Field\FieldItemInterface::__isset()
  109. */
  110. public function __unset($property_name);
  111. /**
  112. * Defines custom presave behavior for field values.
  113. *
  114. * This method is called during the process of saving an entity, just before
  115. * item values are written into storage.
  116. *
  117. * @see \Drupal\Core\Field\FieldItemInterface::preSave()
  118. */
  119. public function preSave();
  120. /**
  121. * Defines custom post-save behavior for field values.
  122. *
  123. * This method is called during the process of saving an entity, just after
  124. * item values are written into storage.
  125. *
  126. * @param bool $update
  127. * Specifies whether the entity is being updated or created.
  128. *
  129. * @return bool
  130. * Whether field items should be rewritten to the storage as a consequence
  131. * of the logic implemented by the custom behavior.
  132. *
  133. * @see \Drupal\Core\Field\FieldItemInterface::postSave()
  134. */
  135. public function postSave($update);
  136. /**
  137. * Defines custom delete behavior for field values.
  138. *
  139. * This method is called during the process of deleting an entity, just before
  140. * values are deleted from storage.
  141. */
  142. public function delete();
  143. /**
  144. * Defines custom revision delete behavior for field values.
  145. *
  146. * This method is called from during the process of deleting an entity
  147. * revision, just before the field values are deleted from storage. It is only
  148. * called for entity types that support revisioning.
  149. */
  150. public function deleteRevision();
  151. /**
  152. * Returns a renderable array for the field items.
  153. *
  154. * @param array $display_options
  155. * Can be either the name of a view mode, or an array of display settings.
  156. * See EntityViewBuilderInterface::viewField() for more information.
  157. *
  158. * @return array
  159. * A renderable array for the field values.
  160. *
  161. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewField()
  162. * @see \Drupal\Core\Field\FieldItemInterface::view()
  163. */
  164. public function view($display_options = []);
  165. /**
  166. * Populates a specified number of field items with valid sample data.
  167. *
  168. * @param int $count
  169. * The number of items to create.
  170. */
  171. public function generateSampleItems($count = 1);
  172. /**
  173. * Returns a form for the default value input.
  174. *
  175. * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
  176. * administrators to configure instance-level default value.
  177. *
  178. * @param array $form
  179. * The form where the settings form is being included in.
  180. * @param \Drupal\Core\Form\FormStateInterface $form_state
  181. * The form state of the (entire) configuration form.
  182. *
  183. * @return array
  184. * The form definition for the field default value.
  185. */
  186. public function defaultValuesForm(array &$form, FormStateInterface $form_state);
  187. /**
  188. * Validates the submitted default value.
  189. *
  190. * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
  191. * administrators to configure instance-level default value.
  192. *
  193. * @param array $element
  194. * The default value form element.
  195. * @param array $form
  196. * The form where the settings form is being included in.
  197. * @param \Drupal\Core\Form\FormStateInterface $form_state
  198. * The form state of the (entire) configuration form.
  199. */
  200. public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state);
  201. /**
  202. * Processes the submitted default value.
  203. *
  204. * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
  205. * administrators to configure instance-level default value.
  206. *
  207. * @param array $element
  208. * The default value form element.
  209. * @param array $form
  210. * The form where the settings form is being included in.
  211. * @param \Drupal\Core\Form\FormStateInterface $form_state
  212. * The form state of the (entire) configuration form.
  213. *
  214. * @return array
  215. * The field default value.
  216. */
  217. public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state);
  218. /**
  219. * Processes the default value before being applied.
  220. *
  221. * Defined or configured default values of a field might need some processing
  222. * in order to be a valid runtime value for the field type; e.g., a date field
  223. * could process the defined value of 'NOW' to a valid date.
  224. *
  225. * @param array $default_value
  226. * The unprocessed default value defined for the field, as a numerically
  227. * indexed array of items, each item being an array of property/value pairs.
  228. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  229. * The entity for which the default value is generated.
  230. * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
  231. * The definition of the field.
  232. *
  233. * @return array
  234. * The return default value for the field.
  235. */
  236. public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition);
  237. /**
  238. * Determines equality to another object implementing FieldItemListInterface.
  239. *
  240. * This method is usually used by the storage to check for not computed
  241. * value changes, which will be saved into the storage.
  242. *
  243. * @param \Drupal\Core\Field\FieldItemListInterface $list_to_compare
  244. * The field item list to compare to.
  245. *
  246. * @return bool
  247. * TRUE if the field item lists are equal, FALSE if not.
  248. */
  249. public function equals(FieldItemListInterface $list_to_compare);
  250. /**
  251. * Determines whether the field has relevant changes.
  252. *
  253. * This is for example used to determine if a revision of an entity has
  254. * changes in a given translation. Unlike
  255. * \Drupal\Core\Field\FieldItemListInterface::equals(), this can report
  256. * that for example an untranslatable field, despite being changed and
  257. * therefore technically affecting all translations, is only internal metadata
  258. * or only affects a single translation.
  259. *
  260. * @param \Drupal\Core\Field\FieldItemListInterface $original_items
  261. * The original field items to compare against.
  262. * @param string $langcode
  263. * The language that should be checked.
  264. *
  265. * @return bool
  266. * TRUE if the field has relevant changes, FALSE if not.
  267. */
  268. public function hasAffectingChanges(FieldItemListInterface $original_items, $langcode);
  269. }