FieldableEntityInterface.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. /**
  4. * Interface for entities having fields.
  5. *
  6. * This interface builds upon the general interfaces provided by the typed data
  7. * API, while extending them with entity-specific additions. I.e., fieldable
  8. * entities implement the ComplexDataInterface among others, thus it is complex
  9. * data containing fields as its data properties. The contained fields have to
  10. * implement \Drupal\Core\Field\FieldItemListInterface, which builds upon typed
  11. * data interfaces as well.
  12. *
  13. * When implementing this interface which extends Traversable, make sure to list
  14. * IteratorAggregate or Iterator before this interface in the implements clause.
  15. *
  16. * @see \Drupal\Core\TypedData\TypedDataManager
  17. * @see \Drupal\Core\Field\FieldItemListInterface
  18. *
  19. * @ingroup entity_api
  20. */
  21. interface FieldableEntityInterface extends EntityInterface {
  22. /**
  23. * Provides base field definitions for an entity type.
  24. *
  25. * Implementations typically use the class
  26. * \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions;
  27. * for example a 'name' field could be defined as the following:
  28. * @code
  29. * $fields['name'] = BaseFieldDefinition::create('string')
  30. * ->setLabel(t('Name'));
  31. * @endcode
  32. *
  33. * By definition, base fields are fields that exist for every bundle. To
  34. * provide definitions for fields that should only exist on some bundles, use
  35. * \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().
  36. *
  37. * The definitions returned by this function can be overridden for all
  38. * bundles by hook_entity_base_field_info_alter() or overridden on a
  39. * per-bundle basis via 'base_field_override' configuration entities.
  40. *
  41. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  42. * The entity type definition. Useful when a single class is used for multiple,
  43. * possibly dynamic entity types.
  44. *
  45. * @return \Drupal\Core\Field\FieldDefinitionInterface[]
  46. * An array of base field definitions for the entity type, keyed by field
  47. * name.
  48. *
  49. * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
  50. * @see \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
  51. */
  52. public static function baseFieldDefinitions(EntityTypeInterface $entity_type);
  53. /**
  54. * Provides field definitions for a specific bundle.
  55. *
  56. * This function can return definitions both for bundle fields (fields that
  57. * are not defined in $base_field_definitions, and therefore might not exist
  58. * on some bundles) as well as bundle-specific overrides of base fields
  59. * (fields that are defined in $base_field_definitions, and therefore exist
  60. * for all bundles). However, bundle-specific base field overrides can also
  61. * be provided by 'base_field_override' configuration entities, and that is
  62. * the recommended approach except in cases where an entity type needs to
  63. * provide a bundle-specific base field override that is decoupled from
  64. * configuration. Note that for most entity types, the bundles themselves are
  65. * derived from configuration (e.g., 'node' bundles are managed via
  66. * 'node_type' configuration entities), so decoupling bundle-specific base
  67. * field overrides from configuration only makes sense for entity types that
  68. * also decouple their bundles from configuration. In cases where both this
  69. * function returns a bundle-specific override of a base field and a
  70. * 'base_field_override' configuration entity exists, the latter takes
  71. * precedence.
  72. *
  73. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  74. * The entity type definition. Useful when a single class is used for multiple,
  75. * possibly dynamic entity types.
  76. * @param string $bundle
  77. * The bundle.
  78. * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
  79. * The list of base field definitions.
  80. *
  81. * @return \Drupal\Core\Field\FieldDefinitionInterface[]
  82. * An array of bundle field definitions, keyed by field name.
  83. *
  84. * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
  85. * @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
  86. *
  87. * @todo WARNING: This method will be changed in
  88. * https://www.drupal.org/node/2346347.
  89. */
  90. public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions);
  91. /**
  92. * Determines whether the entity has a field with the given name.
  93. *
  94. * @param string $field_name
  95. * The field name.
  96. *
  97. * @return bool
  98. * TRUE if the entity has a field with the given name. FALSE otherwise.
  99. */
  100. public function hasField($field_name);
  101. /**
  102. * Gets the definition of a contained field.
  103. *
  104. * @param string $name
  105. * The name of the field.
  106. *
  107. * @return \Drupal\Core\Field\FieldDefinitionInterface|null
  108. * The definition of the field or null if the field does not exist.
  109. */
  110. public function getFieldDefinition($name);
  111. /**
  112. * Gets an array of field definitions of all contained fields.
  113. *
  114. * @return \Drupal\Core\Field\FieldDefinitionInterface[]
  115. * An array of field definitions, keyed by field name.
  116. *
  117. * @see \Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
  118. */
  119. public function getFieldDefinitions();
  120. /**
  121. * Gets an array of all field values.
  122. *
  123. * Gets an array of plain field values, including only non-computed values.
  124. * Note that the structure varies by entity type and bundle.
  125. *
  126. * @return array
  127. * An array of field values, keyed by field name.
  128. */
  129. public function toArray();
  130. /**
  131. * Gets a field item list.
  132. *
  133. * @param string $field_name
  134. * The name of the field to get; e.g., 'title' or 'name'.
  135. *
  136. * @return \Drupal\Core\Field\FieldItemListInterface
  137. * The field item list, containing the field items.
  138. *
  139. * @throws \InvalidArgumentException
  140. * If an invalid field name is given.
  141. */
  142. public function get($field_name);
  143. /**
  144. * Sets a field value.
  145. *
  146. * @param string $field_name
  147. * The name of the field to set; e.g., 'title' or 'name'.
  148. * @param mixed $value
  149. * The value to set, or NULL to unset the field.
  150. * @param bool $notify
  151. * (optional) Whether to notify the entity of the change. Defaults to
  152. * TRUE. If the update stems from the entity, set it to FALSE to avoid
  153. * being notified again.
  154. *
  155. * @return $this
  156. *
  157. * @throws \InvalidArgumentException
  158. * If the specified field does not exist.
  159. */
  160. public function set($field_name, $value, $notify = TRUE);
  161. /**
  162. * Gets an array of all field item lists.
  163. *
  164. * @param bool $include_computed
  165. * If set to TRUE, computed fields are included. Defaults to TRUE.
  166. *
  167. * @return \Drupal\Core\Field\FieldItemListInterface[]
  168. * An array of field item lists implementing, keyed by field name.
  169. */
  170. public function getFields($include_computed = TRUE);
  171. /**
  172. * Gets an array of field item lists for translatable fields.
  173. *
  174. * @param bool $include_computed
  175. * If set to TRUE, computed fields are included. Defaults to TRUE.
  176. *
  177. * @return \Drupal\Core\Field\FieldItemListInterface[]
  178. * An array of field item lists implementing, keyed by field name.
  179. */
  180. public function getTranslatableFields($include_computed = TRUE);
  181. /**
  182. * Reacts to changes to a field.
  183. *
  184. * Note that this is invoked after any changes have been applied.
  185. *
  186. * @param string $field_name
  187. * The name of the field which is changed.
  188. *
  189. * @throws \InvalidArgumentException
  190. * When trying to assign a value to the language field that matches an
  191. * existing translation.
  192. * @throws \LogicException
  193. * When trying to change:
  194. * - The language of a translation.
  195. * - The value of the flag identifying the default translation object.
  196. */
  197. public function onChange($field_name);
  198. /**
  199. * Validates the currently set values.
  200. *
  201. * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
  202. * A list of constraint violations. If the list is empty, validation
  203. * succeeded.
  204. */
  205. public function validate();
  206. /**
  207. * Checks whether entity validation is required before saving the entity.
  208. *
  209. * @return bool
  210. * TRUE if validation is required, FALSE if not.
  211. */
  212. public function isValidationRequired();
  213. /**
  214. * Sets whether entity validation is required before saving the entity.
  215. *
  216. * @param bool $required
  217. * TRUE if validation is required, FALSE otherwise.
  218. *
  219. * @return $this
  220. */
  221. public function setValidationRequired($required);
  222. }