FieldConfigInterface.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Config\Entity\ConfigEntityInterface;
  4. /**
  5. * Defines an interface for configurable field definitions.
  6. *
  7. * This interface allows both configurable fields and overridden base fields to
  8. * share a common interface. The interface also extends ConfigEntityInterface
  9. * to ensure that implementations have the expected save() method.
  10. *
  11. * @see \Drupal\Core\Field\Entity\BaseFieldOverride
  12. * @see \Drupal\field\Entity\FieldConfig
  13. */
  14. interface FieldConfigInterface extends FieldDefinitionInterface, ConfigEntityInterface {
  15. /**
  16. * Sets the field definition label.
  17. *
  18. * @param string $label
  19. * The label to set.
  20. *
  21. * @return $this
  22. */
  23. public function setLabel($label);
  24. /**
  25. * Sets a human readable description.
  26. *
  27. * Descriptions are usually used on user interfaces where the data is edited
  28. * or displayed.
  29. *
  30. * @param string $description
  31. * The description for this field.
  32. *
  33. * @return $this
  34. */
  35. public function setDescription($description);
  36. /**
  37. * Sets whether the field is translatable.
  38. *
  39. * @param bool $translatable
  40. * Whether the field is translatable.
  41. *
  42. * @return $this
  43. */
  44. public function setTranslatable($translatable);
  45. /**
  46. * Sets field settings.
  47. *
  48. * Note that the method does not unset existing settings not specified in the
  49. * incoming $settings array.
  50. *
  51. * For example:
  52. * @code
  53. * // Given these are the default settings.
  54. * $field_definition->getSettings() === [
  55. * 'fruit' => 'apple',
  56. * 'season' => 'summer',
  57. * ];
  58. * // Change only the 'fruit' setting.
  59. * $field_definition->setSettings(['fruit' => 'banana']);
  60. * // The 'season' setting persists unchanged.
  61. * $field_definition->getSettings() === [
  62. * 'fruit' => 'banana',
  63. * 'season' => 'summer',
  64. * ];
  65. * @endcode
  66. *
  67. * For clarity, it is preferred to use setSetting() if not all available
  68. * settings are supplied.
  69. *
  70. * @param array $settings
  71. * The array of field settings.
  72. *
  73. * @return $this
  74. */
  75. public function setSettings(array $settings);
  76. /**
  77. * Sets the value for a field setting by name.
  78. *
  79. * @param string $setting_name
  80. * The name of the setting.
  81. * @param mixed $value
  82. * The value of the setting.
  83. *
  84. * @return $this
  85. */
  86. public function setSetting($setting_name, $value);
  87. /**
  88. * Sets whether the field can be empty.
  89. *
  90. * If a field is required, an entity needs to have at least a valid,
  91. * non-empty item in that field's FieldItemList in order to pass validation.
  92. *
  93. * An item is considered empty if its isEmpty() method returns TRUE.
  94. * Typically, that is if at least one of its required properties is empty.
  95. *
  96. * @param bool $required
  97. * TRUE if the field is required. FALSE otherwise.
  98. *
  99. * @return $this
  100. * The current object, for a fluent interface.
  101. */
  102. public function setRequired($required);
  103. /**
  104. * Sets a default value.
  105. *
  106. * Note that if a default value callback is set, it will take precedence over
  107. * any value set here.
  108. *
  109. * @param mixed $value
  110. * The default value for the field. This can be either:
  111. * - a literal, in which case it will be assigned to the first property of
  112. * the first item.
  113. * - a numerically indexed array of items, each item being a property/value
  114. * array.
  115. * - a non-numerically indexed array, in which case the array is assumed to
  116. * be a property/value array and used as the first item
  117. * - NULL or array() for no default value.
  118. *
  119. * @return $this
  120. */
  121. public function setDefaultValue($value);
  122. /**
  123. * Sets a custom default value callback.
  124. *
  125. * If set, the callback overrides any set default value.
  126. *
  127. * @param string|null $callback
  128. * The callback to invoke for getting the default value (pass NULL to unset
  129. * a previously set callback). The callback will be invoked with the
  130. * following arguments:
  131. * - \Drupal\Core\Entity\FieldableEntityInterface $entity
  132. * The entity being created.
  133. * - \Drupal\Core\Field\FieldDefinitionInterface $definition
  134. * The field definition.
  135. * It should return the default value in the format accepted by the
  136. * setDefaultValue() method.
  137. *
  138. * @return $this
  139. */
  140. public function setDefaultValueCallback($callback);
  141. /**
  142. * Sets constraints for a given field item property.
  143. *
  144. * Note: this overwrites any existing property constraints. If you need to
  145. * add to the existing constraints, use
  146. * \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints()
  147. *
  148. * Note that constraints added via this method are not stored in configuration
  149. * and as such need to be added at runtime using
  150. * hook_entity_bundle_field_info_alter().
  151. *
  152. * @param string $name
  153. * The name of the property to set constraints for.
  154. * @param array $constraints
  155. * The constraints to set.
  156. *
  157. * @return static
  158. * The object itself for chaining.
  159. *
  160. * @see hook_entity_bundle_field_info_alter()
  161. */
  162. public function setPropertyConstraints($name, array $constraints);
  163. /**
  164. * Adds constraints for a given field item property.
  165. *
  166. * Adds a constraint to a property of a field item. e.g.
  167. * @code
  168. * // Limit the field item's value property to the range 0 through 10.
  169. * // e.g. $node->field_how_many->value.
  170. * $field->addPropertyConstraints('value', [
  171. * 'Range' => [
  172. * 'min' => 0,
  173. * 'max' => 10,
  174. * ]
  175. * ]);
  176. * @endcode
  177. *
  178. * If you want to add a validation constraint that applies to the
  179. * \Drupal\Core\Field\FieldItemList, use FieldConfigInterface::addConstraint()
  180. * instead.
  181. *
  182. * Note: passing a new set of options for an existing property constraint will
  183. * overwrite with the new options.
  184. *
  185. * Note that constraints added via this method are not stored in configuration
  186. * and as such need to be added at runtime using
  187. * hook_entity_bundle_field_info_alter().
  188. *
  189. * @param string $name
  190. * The name of the property to set constraints for.
  191. * @param array $constraints
  192. * The constraints to set.
  193. *
  194. * @return static
  195. * The object itself for chaining.
  196. *
  197. * @see \Drupal\Core\Field\FieldConfigInterface::addConstraint()
  198. * @see hook_entity_bundle_field_info_alter()
  199. */
  200. public function addPropertyConstraints($name, array $constraints);
  201. /**
  202. * Adds a validation constraint to the FieldItemList.
  203. *
  204. * Note: If you wish to apply a constraint to just a property of a FieldItem
  205. * use \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints()
  206. * instead.
  207. * @code
  208. * // Add a constraint to the 'field_username' FieldItemList.
  209. * // e.g. $node->field_username
  210. * $fields['field_username']->addConstraint('UniqueField');
  211. * @endcode
  212. *
  213. * If you wish to apply a constraint to a \Drupal\Core\Field\FieldItem instead
  214. * of a property or FieldItemList, you can use the
  215. * \Drupal\Core\Field\FieldConfigBase::getItemDefinition() method.
  216. * @code
  217. * // Add a constraint to the 'field_entity_reference' FieldItem (entity
  218. * // reference item).
  219. * $fields['field_entity_reference']->getItemDefinition()->addConstraint('MyCustomFieldItemValidationPlugin', []);
  220. * @endcode
  221. *
  222. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
  223. * details.
  224. *
  225. * Note that constraints added via this method are not stored in configuration
  226. * and as such need to be added at runtime using
  227. * hook_entity_bundle_field_info_alter().
  228. *
  229. * @param string $constraint_name
  230. * The name of the constraint to add, i.e. its plugin id.
  231. * @param array|null $options
  232. * The constraint options as required by the constraint plugin, or NULL.
  233. *
  234. * @return static
  235. * The object itself for chaining.
  236. *
  237. * @see \Drupal\Core\Field\FieldItemList
  238. * @see \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints()
  239. * @see hook_entity_bundle_field_info_alter()
  240. */
  241. public function addConstraint($constraint_name, $options = NULL);
  242. /**
  243. * Sets the array of validation constraints for the FieldItemList.
  244. *
  245. * NOTE: This will overwrite any previously set constraints. In most cases
  246. * FieldConfigInterface::addConstraint() should be used instead.
  247. *
  248. * Note that constraints added via this method are not stored in configuration
  249. * and as such need to be added at runtime using
  250. * hook_entity_bundle_field_info_alter().
  251. *
  252. * @param array $constraints
  253. * The array of constraints. See
  254. * \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
  255. *
  256. * @return $this
  257. *
  258. * @see \Drupal\Core\TypedData\DataDefinition::addConstraint()
  259. * @see \Drupal\Core\TypedData\DataDefinition::getConstraints()
  260. * @see \Drupal\Core\Field\FieldItemList
  261. * @see hook_entity_bundle_field_info_alter()
  262. */
  263. public function setConstraints(array $constraints);
  264. }