FieldItemInterface.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\TypedData\ComplexDataInterface;
  5. /**
  6. * Interface for entity field items.
  7. *
  8. * Entity field items are typed data objects containing the field values, i.e.
  9. * implementing the ComplexDataInterface.
  10. *
  11. * When implementing this interface which extends Traversable, make sure to list
  12. * IteratorAggregate or Iterator before this interface in the implements clause.
  13. *
  14. * @see \Drupal\Core\Field\FieldItemListInterface
  15. * @see \Drupal\Core\Field\FieldItemBase
  16. * @ingroup field_types
  17. */
  18. interface FieldItemInterface extends ComplexDataInterface {
  19. /**
  20. * Defines field item properties.
  21. *
  22. * Properties that are required to constitute a valid, non-empty item should
  23. * be denoted with \Drupal\Core\TypedData\DataDefinition::setRequired().
  24. *
  25. * @return \Drupal\Core\TypedData\DataDefinitionInterface[]
  26. * An array of property definitions of contained properties, keyed by
  27. * property name.
  28. *
  29. * @see \Drupal\Core\Field\BaseFieldDefinition
  30. */
  31. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition);
  32. /**
  33. * Returns the name of the main property, if any.
  34. *
  35. * Some field items consist mainly of one main property, e.g. the value of a
  36. * text field or the target_id of an entity reference. If the field item has
  37. * no main property, the method returns NULL.
  38. *
  39. * @return string|null
  40. * The name of the value property, or NULL if there is none.
  41. *
  42. * @see \Drupal\Core\Field\BaseFieldDefinition
  43. */
  44. public static function mainPropertyName();
  45. /**
  46. * Returns the schema for the field.
  47. *
  48. * This method is static because the field schema information is needed on
  49. * creation of the field. FieldItemInterface objects instantiated at that
  50. * time are not reliable as field settings might be missing.
  51. *
  52. * Computed fields having no schema should return an empty array.
  53. *
  54. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition
  55. * The field definition.
  56. *
  57. * @return array
  58. * An empty array if there is no schema, or an associative array with the
  59. * following key/value pairs:
  60. * - columns: An array of Schema API column specifications, keyed by column
  61. * name. The columns need to be a subset of the properties defined in
  62. * propertyDefinitions(). The 'not null' property is ignored if present,
  63. * as it is determined automatically by the storage controller depending
  64. * on the table layout and the property definitions. It is recommended to
  65. * avoid having the column definitions depend on field settings when
  66. * possible. No assumptions should be made on how storage engines
  67. * internally use the original column name to structure their storage.
  68. * - unique keys: (optional) An array of Schema API unique key definitions.
  69. * Only columns that appear in the 'columns' array are allowed.
  70. * - indexes: (optional) An array of Schema API index definitions. Only
  71. * columns that appear in the 'columns' array are allowed. Those indexes
  72. * will be used as default indexes. Field definitions can specify
  73. * additional indexes or, at their own risk, modify the default indexes
  74. * specified by the field-type module. Some storage engines might not
  75. * support indexes.
  76. * - foreign keys: (optional) An array of Schema API foreign key
  77. * definitions. Note, however, that the field data is not necessarily
  78. * stored in SQL. Also, the possible usage is limited, as you cannot
  79. * specify another field as related, only existing SQL tables,
  80. * such as {taxonomy_term_data}.
  81. */
  82. public static function schema(FieldStorageDefinitionInterface $field_definition);
  83. /**
  84. * Gets the entity that field belongs to.
  85. *
  86. * @return \Drupal\Core\Entity\FieldableEntityInterface
  87. * The entity object.
  88. */
  89. public function getEntity();
  90. /**
  91. * Gets the langcode of the field values held in the object.
  92. *
  93. * @return string
  94. * The langcode.
  95. */
  96. public function getLangcode();
  97. /**
  98. * Gets the field definition.
  99. *
  100. * @return \Drupal\Core\Field\FieldDefinitionInterface
  101. * The field definition.
  102. */
  103. public function getFieldDefinition();
  104. /**
  105. * Magic method: Gets a property value.
  106. *
  107. * @param string $property_name
  108. * The name of the property to get; e.g., 'title' or 'name'.
  109. *
  110. * @return mixed
  111. * The property value.
  112. *
  113. * @throws \InvalidArgumentException
  114. * If a not existing property is accessed.
  115. */
  116. public function __get($property_name);
  117. /**
  118. * Magic method: Sets a property value.
  119. *
  120. * @param string $property_name
  121. * The name of the property to set; e.g., 'title' or 'name'.
  122. * @param mixed $value
  123. * The value to set, or NULL to unset the property. Optionally, a typed
  124. * data object implementing Drupal\Core\TypedData\TypedDataInterface may be
  125. * passed instead of a plain value.
  126. *
  127. * @throws \InvalidArgumentException
  128. * If a not existing property is set.
  129. */
  130. public function __set($property_name, $value);
  131. /**
  132. * Magic method: Determines whether a property is set.
  133. *
  134. * @param string $property_name
  135. * The name of the property to get; e.g., 'title' or 'name'.
  136. *
  137. * @return bool
  138. * Returns TRUE if the property exists and is set, FALSE otherwise.
  139. */
  140. public function __isset($property_name);
  141. /**
  142. * Magic method: Unsets a property.
  143. *
  144. * @param string $property_name
  145. * The name of the property to get; e.g., 'title' or 'name'.
  146. */
  147. public function __unset($property_name);
  148. /**
  149. * Returns a renderable array for a single field item.
  150. *
  151. * @param array $display_options
  152. * Can be either the name of a view mode, or an array of display settings.
  153. * See EntityViewBuilderInterface::viewField() for more information.
  154. *
  155. * @return array
  156. * A renderable array for the field item.
  157. *
  158. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewField()
  159. * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewFieldItem()
  160. * @see \Drupal\Core\Field\FieldItemListInterface::view()
  161. */
  162. public function view($display_options = []);
  163. /**
  164. * Defines custom presave behavior for field values.
  165. *
  166. * This method is called during the process of saving an entity, just before
  167. * values are written into storage. When storing a new entity, its identifier
  168. * will not be available yet. This should be used to massage item property
  169. * values or perform any other operation that needs to happen before values
  170. * are stored. For instance this is the proper phase to auto-create a new
  171. * entity for an entity reference field item, because this way it will be
  172. * possible to store the referenced entity identifier.
  173. */
  174. public function preSave();
  175. /**
  176. * Defines custom post-save behavior for field values.
  177. *
  178. * This method is called during the process of saving an entity, just after
  179. * values are written into storage. This is useful mostly when the business
  180. * logic to be implemented always requires the entity identifier, even when
  181. * storing a new entity. For instance, when implementing circular entity
  182. * references, the referenced entity will be created on pre-save with a dummy
  183. * value for the referring entity identifier, which will be updated with the
  184. * actual one on post-save.
  185. *
  186. * In the rare cases where item properties depend on the entity identifier,
  187. * massaging logic will have to be implemented on post-save and returning TRUE
  188. * will allow them to be rewritten to the storage with the updated values.
  189. *
  190. * @param bool $update
  191. * Specifies whether the entity is being updated or created.
  192. *
  193. * @return bool
  194. * Whether field items should be rewritten to the storage as a consequence
  195. * of the logic implemented by the custom behavior.
  196. */
  197. public function postSave($update);
  198. /**
  199. * Defines custom delete behavior for field values.
  200. *
  201. * This method is called during the process of deleting an entity, just before
  202. * values are deleted from storage.
  203. */
  204. public function delete();
  205. /**
  206. * Defines custom revision delete behavior for field values.
  207. *
  208. * This method is called from during the process of deleting an entity
  209. * revision, just before the field values are deleted from storage. It is only
  210. * called for entity types that support revisioning.
  211. */
  212. public function deleteRevision();
  213. /**
  214. * Generates placeholder field values.
  215. *
  216. * Useful when populating site with placeholder content during site building
  217. * or profiling.
  218. *
  219. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  220. * The field definition.
  221. *
  222. * @return array
  223. * An associative array of values.
  224. */
  225. public static function generateSampleValue(FieldDefinitionInterface $field_definition);
  226. /**
  227. * Defines the storage-level settings for this plugin.
  228. *
  229. * @return array
  230. * A list of default settings, keyed by the setting name.
  231. */
  232. public static function defaultStorageSettings();
  233. /**
  234. * Defines the field-level settings for this plugin.
  235. *
  236. * @return array
  237. * A list of default settings, keyed by the setting name.
  238. */
  239. public static function defaultFieldSettings();
  240. /**
  241. * Returns a settings array that can be stored as a configuration value.
  242. *
  243. * For all use cases where field settings are stored and managed as
  244. * configuration, this method is used to map from the field type's
  245. * representation of its settings to a representation compatible with
  246. * deployable configuration. This includes:
  247. * - Array keys at any depth must not contain a ".".
  248. * - Ideally, array keys at any depth are either numeric or can be enumerated
  249. * as a "mapping" within the configuration schema. While not strictly
  250. * required, this simplifies configuration translation UIs, configuration
  251. * migrations between Drupal versions, and other use cases.
  252. * - To support configuration deployments, references to content entities
  253. * must use UUIDs rather than local IDs.
  254. *
  255. * An example of a conversion between representations might be an
  256. * "allowed_values" setting that's structured by the field type as a
  257. * \Drupal\Core\TypedData\OptionsProviderInterface::getPossibleOptions()
  258. * result (i.e., values as keys and labels as values). For such a use case,
  259. * in order to comply with the above, this method could convert that
  260. * representation to a numerically indexed array whose values are sub-arrays
  261. * with the schema definable keys of "value" and "label".
  262. *
  263. * @param array $settings
  264. * The field's settings in the field type's canonical representation.
  265. *
  266. * @return array
  267. * An array (either the unmodified $settings or a modified representation)
  268. * that is suitable for storing as a deployable configuration value.
  269. *
  270. * @see \Drupal\Core\Config\Config::set()
  271. */
  272. public static function storageSettingsToConfigData(array $settings);
  273. /**
  274. * Returns a settings array in the field type's canonical representation.
  275. *
  276. * This function does the inverse of static::storageSettingsToConfigData(). It's
  277. * called when loading a field's settings from a configuration object.
  278. *
  279. * @param array $settings
  280. * The field's settings, as it is stored within a configuration object.
  281. *
  282. * @return array
  283. * The settings, in the representation expected by the field type and code
  284. * that interacts with it.
  285. *
  286. * @see \Drupal\Core\Field\FieldItemInterface::storageSettingsToConfigData()
  287. */
  288. public static function storageSettingsFromConfigData(array $settings);
  289. /**
  290. * Returns a settings array that can be stored as a configuration value.
  291. *
  292. * Same as static::storageSettingsToConfigData(), but for the field's settings.
  293. *
  294. * @param array $settings
  295. * The field's settings in the field type's canonical representation.
  296. *
  297. * @return array
  298. * An array (either the unmodified $settings or a modified representation)
  299. * that is suitable for storing as a deployable configuration value.
  300. *
  301. * @see \Drupal\Core\Field\FieldItemInterface::storageSettingsToConfigData()
  302. */
  303. public static function fieldSettingsToConfigData(array $settings);
  304. /**
  305. * Returns a settings array in the field type's canonical representation.
  306. *
  307. * This function does the inverse of static::fieldSettingsToConfigData().
  308. * It's called when loading a field's settings from a configuration
  309. * object.
  310. *
  311. * @param array $settings
  312. * The field's settings, as it is stored within a configuration
  313. * object.
  314. *
  315. * @return array
  316. * The field settings, in the representation expected by the field type
  317. * and code that interacts with it.
  318. *
  319. * @see \Drupal\Core\Field\FieldItemInterface::fieldSettingsToConfigData()
  320. */
  321. public static function fieldSettingsFromConfigData(array $settings);
  322. /**
  323. * Returns a form for the storage-level settings.
  324. *
  325. * Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow
  326. * administrators to configure storage-level settings.
  327. *
  328. * Field storage might reject settings changes that affect the field
  329. * storage schema if the storage already has data. When the $has_data
  330. * parameter is TRUE, the form should not allow changing the settings that
  331. * take part in the schema() method. It is recommended to set #access to
  332. * FALSE on the corresponding elements.
  333. *
  334. * @param array $form
  335. * The form where the settings form is being included in.
  336. * @param \Drupal\Core\Form\FormStateInterface $form_state
  337. * The form state of the (entire) configuration form.
  338. * @param bool $has_data
  339. * TRUE if the field already has data, FALSE if not.
  340. *
  341. * @return array
  342. * The form definition for the field settings.
  343. */
  344. public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data);
  345. /**
  346. * Returns a form for the field-level settings.
  347. *
  348. * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
  349. * administrators to configure field-level settings.
  350. *
  351. * @param array $form
  352. * The form where the settings form is being included in.
  353. * @param \Drupal\Core\Form\FormStateInterface $form_state
  354. * The form state of the (entire) configuration form.
  355. *
  356. * @return array
  357. * The form definition for the field settings.
  358. */
  359. public function fieldSettingsForm(array $form, FormStateInterface $form_state);
  360. /**
  361. * Calculates dependencies for field items.
  362. *
  363. * Dependencies are saved in the field configuration entity and are used to
  364. * determine configuration synchronization order. For example, if the field
  365. * type's default value is a content entity, this method should return an
  366. * array of dependencies listing the content entities.
  367. *
  368. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  369. * The field definition.
  370. *
  371. * @return array
  372. * An array of dependencies grouped by type (config, content, module,
  373. * theme). For example:
  374. * @code
  375. * array(
  376. * 'config' => array('user.role.anonymous', 'user.role.authenticated'),
  377. * 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'),
  378. * 'module' => array('node', 'user'),
  379. * 'theme' => array('seven'),
  380. * );
  381. * @endcode
  382. *
  383. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  384. * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()
  385. */
  386. public static function calculateDependencies(FieldDefinitionInterface $field_definition);
  387. /**
  388. * Calculates dependencies for field items on the storage level.
  389. *
  390. * Dependencies are saved in the field storage configuration entity and are
  391. * used to determine configuration synchronization order. For example, if the
  392. * field type storage depends on a particular entity type, this method should
  393. * return an array of dependencies listing the module that provides the entity
  394. * type.
  395. *
  396. * Dependencies returned from this method are stored in field storage
  397. * configuration and are always considered hard dependencies. If the
  398. * dependency is removed the field storage configuration must be deleted.
  399. *
  400. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition
  401. * The field storage definition.
  402. *
  403. * @return array
  404. * An array of dependencies grouped by type (config, content, module,
  405. * theme). For example:
  406. * @code
  407. * [
  408. * 'config' => ['user.role.anonymous', 'user.role.authenticated'],
  409. * 'content' => ['node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'],
  410. * 'module' => ['node', 'user'],
  411. * 'theme' => ['seven'],
  412. * ];
  413. * @endcode
  414. *
  415. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  416. * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()
  417. */
  418. public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_storage_definition);
  419. /**
  420. * Informs the plugin that a dependency of the field will be deleted.
  421. *
  422. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  423. * The field definition.
  424. * @param array $dependencies
  425. * An array of dependencies that will be deleted keyed by dependency type.
  426. * Dependency types are, for example, entity, module and theme.
  427. *
  428. * @return bool
  429. * TRUE if the field definition has been changed as a result, FALSE if not.
  430. *
  431. * @see \Drupal\Core\Config\ConfigEntityInterface::onDependencyRemoval()
  432. */
  433. public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies);
  434. }