FieldDefinitionInterface.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Cache\CacheableDependencyInterface;
  4. use Drupal\Core\Entity\FieldableEntityInterface;
  5. use Drupal\Core\TypedData\ListDataDefinitionInterface;
  6. /**
  7. * Defines an interface for entity field definitions.
  8. *
  9. * An entity field is a data object that holds the values of a particular field
  10. * for a particular entity (see \Drupal\Core\Field\FieldItemListInterface). For
  11. * example, $node_1->body and $node_2->body contain different data and therefore
  12. * are different field objects.
  13. *
  14. * In contrast, an entity field *definition* is an object that returns
  15. * information *about* a field (e.g., its type and settings) rather than its
  16. * values. As such, if all the information about $node_1->body and $node_2->body
  17. * is the same, then the same field definition object can be used to describe
  18. * both.
  19. *
  20. * It is up to the class implementing this interface to manage where the
  21. * information comes from. For example, field.module provides an implementation
  22. * based on two levels of configuration. It allows the site administrator to add
  23. * custom fields to any entity type and bundle via the "field_storage_config"
  24. * and "field_config" configuration entities. The former for storing
  25. * configuration that is independent of which entity type and bundle the field
  26. * is added to, and the latter for storing configuration that is specific to the
  27. * entity type and bundle. The class that implements "field_config"
  28. * configuration entities also implements this interface, returning information
  29. * from either itself, or from the corresponding "field_storage_config"
  30. * configuration, as appropriate.
  31. *
  32. * However, entity base fields, such as $node->title, are not managed by
  33. * field.module and its "field_storage_config"/"field_config"
  34. * configuration entities. Therefore, their definitions are provided by
  35. * different objects based on the class \Drupal\Core\Field\BaseFieldDefinition,
  36. * which implements this interface as well.
  37. *
  38. * Field definitions may fully define a concrete data object (e.g.,
  39. * $node_1->body), or may provide a best-guess definition for a data object that
  40. * might come into existence later. For example, $node_1->body and $node_2->body
  41. * may have different definitions (e.g., if the node types are different). When
  42. * adding the "body" field to a View that can return nodes of different types,
  43. * the View can get a field definition that represents the "body" field
  44. * abstractly, and present Views configuration options to the administrator
  45. * based on that abstract definition, even though that abstract definition can
  46. * differ from the concrete definition of any particular node's body field.
  47. */
  48. interface FieldDefinitionInterface extends ListDataDefinitionInterface, CacheableDependencyInterface {
  49. /**
  50. * Returns the machine name of the field.
  51. *
  52. * This defines how the field data is accessed from the entity. For example,
  53. * if the field name is "foo", then $entity->foo returns its data.
  54. *
  55. * @return string
  56. * The field name.
  57. */
  58. public function getName();
  59. /**
  60. * Returns the field type.
  61. *
  62. * @return string
  63. * The field type, i.e. the id of a field type plugin. For example 'text'.
  64. *
  65. * @see \Drupal\Core\Field\FieldTypePluginManagerInterface
  66. */
  67. public function getType();
  68. /**
  69. * Returns the ID of the entity type the field is attached to.
  70. *
  71. * This method should not be confused with EntityInterface::getEntityTypeId()
  72. * (configurable fields are config entities, and thus implement both
  73. * interfaces):
  74. * - FieldDefinitionInterface::getTargetEntityTypeId() answers "as a field,
  75. * which entity type are you attached to?".
  76. * - EntityInterface::getEntityTypeId() answers "as a (config) entity, what
  77. * is your own entity type?".
  78. *
  79. * @return string
  80. * The entity type ID.
  81. */
  82. public function getTargetEntityTypeId();
  83. /**
  84. * Gets the bundle the field is attached to.
  85. *
  86. * This method should not be confused with EntityInterface::bundle()
  87. * (configurable fields are config entities, and thus implement both
  88. * interfaces):
  89. * - FieldDefinitionInterface::getTargetBundle() answers "as a field,
  90. * which bundle are you attached to?".
  91. * - EntityInterface::bundle() answers "as a (config) entity, what
  92. * is your own bundle?" (not relevant in our case, the config entity types
  93. * used to store the definitions of configurable fields do not have
  94. * bundles).
  95. *
  96. * @return string|null
  97. * The bundle the field is defined for, or NULL if it is a base field; i.e.,
  98. * it is not bundle-specific.
  99. */
  100. public function getTargetBundle();
  101. /**
  102. * Returns whether the display for the field can be configured.
  103. *
  104. * @param string $display_context
  105. * The display context. Either 'view' or 'form'.
  106. *
  107. * @return bool
  108. * TRUE if the display for this field is configurable in the given context.
  109. * If TRUE, the display options returned by getDisplayOptions() may be
  110. * overridden via the respective entity display.
  111. *
  112. * @see \Drupal\Core\Entity\Display\EntityDisplayInterface
  113. */
  114. public function isDisplayConfigurable($display_context);
  115. /**
  116. * Returns the default display options for the field.
  117. *
  118. * If the field's display is configurable, the returned display options act
  119. * as default values and may be overridden via the respective entity display.
  120. * Otherwise, the display options will be applied to entity displays as is.
  121. *
  122. * @param string $display_context
  123. * The display context. Either 'view' or 'form'.
  124. *
  125. * @return array|null
  126. * The array of display options for the field, or NULL if the field is not
  127. * displayed. The following key/value pairs may be present:
  128. * - label: (string) Position of the field label. The default 'field' theme
  129. * implementation supports the values 'inline', 'above' and 'hidden'.
  130. * Defaults to 'above'. Only applies to 'view' context.
  131. * - region: (string) The region the field is in, or 'hidden'. If not
  132. * specified, the default region will be used.
  133. * - type: (string) The plugin (widget or formatter depending on
  134. * $display_context) to use. If not specified or if the requested plugin
  135. * is unknown, the 'default_widget' / 'default_formatter' for the field
  136. * type will be used. Previously 'hidden' was a valid value, it is now
  137. * deprecated in favor of specifying 'region' => 'hidden'.
  138. * - settings: (array) Settings for the plugin specified above. The default
  139. * settings for the plugin will be used for settings left unspecified.
  140. * - third_party_settings: (array) Settings provided by other extensions
  141. * through hook_field_formatter_third_party_settings_form().
  142. * - weight: (float) The weight of the element. Not needed if 'type' is
  143. * 'hidden'.
  144. * The defaults of the various display options above get applied by the used
  145. * entity display.
  146. *
  147. * @see \Drupal\Core\Entity\Display\EntityDisplayInterface
  148. */
  149. public function getDisplayOptions($display_context);
  150. /**
  151. * Returns whether the field can be empty.
  152. *
  153. * If a field is required, an entity needs to have at least a valid,
  154. * non-empty item in that field's FieldItemList in order to pass validation.
  155. *
  156. * An item is considered empty if its isEmpty() method returns TRUE.
  157. * Typically, that is if at least one of its required properties is empty.
  158. *
  159. * @return bool
  160. * TRUE if the field is required.
  161. *
  162. * @see \Drupal\Core\TypedData\Plugin\DataType\ItemList::isEmpty()
  163. * @see \Drupal\Core\Field\FieldItemInterface::isEmpty()
  164. * @see \Drupal\Core\TypedData\DataDefinitionInterface:isRequired()
  165. * @see \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints()
  166. */
  167. public function isRequired();
  168. /**
  169. * Returns the default value literal for the field.
  170. *
  171. * This method retrieves the raw property assigned to the field definition.
  172. * When computing the runtime default value for a field in a given entity,
  173. * ::getDefaultValue() should be used instead.
  174. *
  175. * @return array
  176. * The default value for the field, as a numerically indexed array of items,
  177. * each item being a property/value array (array() for no default value).
  178. *
  179. * @see FieldDefinitionInterface::getDefaultValue()
  180. * @see FieldDefinitionInterface::getDefaultValueCallback()
  181. */
  182. public function getDefaultValueLiteral();
  183. /**
  184. * Returns the default value callback for the field.
  185. *
  186. * This method retrieves the raw property assigned to the field definition.
  187. * When computing the runtime default value for a field in a given entity,
  188. * ::getDefaultValue() should be used instead.
  189. *
  190. * @return string|null
  191. * The default value callback for the field.
  192. *
  193. * @see FieldDefinitionInterface::getDefaultValue()
  194. * @see FieldDefinitionInterface::getDefaultValueLiteral()
  195. */
  196. public function getDefaultValueCallback();
  197. /**
  198. * Returns the default value for the field in a newly created entity.
  199. *
  200. * This method computes the runtime default value for a field in a given
  201. * entity. To access the raw properties assigned to the field definition,
  202. * ::getDefaultValueLiteral() or ::getDefaultValueCallback() should be used
  203. * instead.
  204. *
  205. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
  206. * The entity for which the default value is generated.
  207. *
  208. * @return array
  209. * The default value for the field, as a numerically indexed array of items,
  210. * each item being a property/value array (array() for no default value).
  211. *
  212. * @see FieldDefinitionInterface::getDefaultValueLiteral()
  213. * @see FieldDefinitionInterface::getDefaultValueCallback()
  214. */
  215. public function getDefaultValue(FieldableEntityInterface $entity);
  216. /**
  217. * Returns whether the field is translatable.
  218. *
  219. * @return bool
  220. * TRUE if the field is translatable.
  221. */
  222. public function isTranslatable();
  223. /**
  224. * Returns the field storage definition.
  225. *
  226. * @return \Drupal\Core\Field\FieldStorageDefinitionInterface
  227. * The field storage definition.
  228. */
  229. public function getFieldStorageDefinition();
  230. /**
  231. * Gets an object that can be saved in configuration.
  232. *
  233. * Base fields are defined in code. In order to configure field definition
  234. * properties per bundle use this method to create an override that can be
  235. * saved in configuration.
  236. *
  237. * @see \Drupal\Core\Field\Entity\BaseFieldBundleOverride
  238. *
  239. * @param string $bundle
  240. * The bundle to get the configurable field for.
  241. *
  242. * @return \Drupal\Core\Field\FieldConfigInterface
  243. */
  244. public function getConfig($bundle);
  245. /**
  246. * Returns a unique identifier for the field.
  247. *
  248. * @return string
  249. */
  250. public function getUniqueIdentifier();
  251. }