field.api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Field API documentation.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * @defgroup field_types Field Types API
  12. * @{
  13. * Defines field, widget, display formatter, and storage types.
  14. *
  15. * In the Field API, each field has a type, which determines what kind of data
  16. * (integer, string, date, etc.) the field can hold, which settings it provides,
  17. * and so on. The data type(s) accepted by a field are defined in
  18. * hook_field_schema().
  19. *
  20. * Field types are plugins annotated with class
  21. * \Drupal\Core\Field\Annotation\FieldType, and implement plugin interface
  22. * \Drupal\Core\Field\FieldItemInterface. Field Type plugins are managed by the
  23. * \Drupal\Core\Field\FieldTypePluginManager class. Field type classes usually
  24. * extend base class \Drupal\Core\Field\FieldItemBase. Field-type plugins need
  25. * to be in the namespace \Drupal\{your_module}\Plugin\Field\FieldType. See the
  26. * @link plugin_api Plugin API topic @endlink for more information on how to
  27. * define plugins.
  28. *
  29. * The Field Types API also defines two kinds of pluggable handlers: widgets
  30. * and formatters. @link field_widget Widgets @endlink specify how the field
  31. * appears in edit forms, while @link field_formatter formatters @endlink
  32. * specify how the field appears in displayed entities.
  33. *
  34. * See @link field Field API @endlink for information about the other parts of
  35. * the Field API.
  36. *
  37. * @see field
  38. * @see field_widget
  39. * @see field_formatter
  40. * @see plugin_api
  41. */
  42. /**
  43. * Perform alterations on Field API field types.
  44. *
  45. * @param $info
  46. * Array of information on field types as collected by the "field type" plugin
  47. * manager.
  48. */
  49. function hook_field_info_alter(&$info) {
  50. // Change the default widget for fields of type 'foo'.
  51. if (isset($info['foo'])) {
  52. $info['foo']['default widget'] = 'mymodule_widget';
  53. }
  54. }
  55. /**
  56. * Forbid a field storage update from occurring.
  57. *
  58. * Any module may forbid any update for any reason. For example, the
  59. * field's storage module might forbid an update if it would change
  60. * the storage schema while data for the field exists. A field type
  61. * module might forbid an update if it would change existing data's
  62. * semantics, or if there are external dependencies on field settings
  63. * that cannot be updated.
  64. *
  65. * To forbid the update from occurring, throw a
  66. * \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException.
  67. *
  68. * @param \Drupal\field\FieldStorageConfigInterface $field_storage
  69. * The field storage as it will be post-update.
  70. * @param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
  71. * The field storage as it is pre-update.
  72. *
  73. * @see entity_crud
  74. */
  75. function hook_field_storage_config_update_forbid(\Drupal\field\FieldStorageConfigInterface $field_storage, \Drupal\field\FieldStorageConfigInterface $prior_field_storage) {
  76. if ($field_storage->module == 'options' && $field_storage->hasData()) {
  77. // Forbid any update that removes allowed values with actual data.
  78. $allowed_values = $field_storage->getSetting('allowed_values');
  79. $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
  80. $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
  81. if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
  82. throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', ['@field_name' => $field_storage->getName()]));
  83. }
  84. }
  85. }
  86. /**
  87. * @} End of "defgroup field_types".
  88. */
  89. /**
  90. * @defgroup field_widget Field Widget API
  91. * @{
  92. * Define Field API widget types.
  93. *
  94. * Field API widgets specify how fields are displayed in edit forms. Fields of a
  95. * given @link field_types field type @endlink may be edited using more than one
  96. * widget. In this case, the Field UI module allows the site builder to choose
  97. * which widget to use.
  98. *
  99. * Widgets are Plugins managed by the
  100. * \Drupal\Core\Field\WidgetPluginManager class. A widget is a plugin annotated
  101. * with class \Drupal\Core\Field\Annotation\FieldWidget that implements
  102. * \Drupal\Core\Field\WidgetInterface (in most cases, by
  103. * subclassing \Drupal\Core\Field\WidgetBase). Widget plugins need to be in the
  104. * namespace \Drupal\{your_module}\Plugin\Field\FieldWidget.
  105. *
  106. * Widgets are @link form_api Form API @endlink elements with additional
  107. * processing capabilities. The methods of the WidgetInterface object are
  108. * typically called by respective methods in the
  109. * \Drupal\Core\Entity\Entity\EntityFormDisplay class.
  110. *
  111. * @see field
  112. * @see field_types
  113. * @see field_formatter
  114. * @see plugin_api
  115. */
  116. /**
  117. * Perform alterations on Field API widget types.
  118. *
  119. * @param array $info
  120. * An array of information on existing widget types, as collected by the
  121. * annotation discovery mechanism.
  122. */
  123. function hook_field_widget_info_alter(array &$info) {
  124. // Let a new field type re-use an existing widget.
  125. $info['options_select']['field_types'][] = 'my_field_type';
  126. }
  127. /**
  128. * Alter forms for field widgets provided by other modules.
  129. *
  130. * @param $element
  131. * The field widget form element as constructed by hook_field_widget_form().
  132. * @param $form_state
  133. * The current state of the form.
  134. * @param $context
  135. * An associative array containing the following key-value pairs:
  136. * - form: The form structure to which widgets are being attached. This may be
  137. * a full form structure, or a sub-element of a larger form.
  138. * - widget: The widget plugin instance.
  139. * - items: The field values, as a
  140. * \Drupal\Core\Field\FieldItemListInterface object.
  141. * - delta: The order of this item in the array of subelements (0, 1, 2, etc).
  142. * - default: A boolean indicating whether the form is being shown as a dummy
  143. * form to set default values.
  144. *
  145. * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
  146. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  147. */
  148. function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  149. // Add a css class to widget form elements for all fields of type mytype.
  150. $field_definition = $context['items']->getFieldDefinition();
  151. if ($field_definition->getType() == 'mytype') {
  152. // Be sure not to overwrite existing attributes.
  153. $element['#attributes']['class'][] = 'myclass';
  154. }
  155. }
  156. /**
  157. * Alter widget forms for a specific widget provided by another module.
  158. *
  159. * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
  160. * specific widget form, rather than using hook_field_widget_form_alter() and
  161. * checking the widget type.
  162. *
  163. * @param $element
  164. * The field widget form element as constructed by hook_field_widget_form().
  165. * @param $form_state
  166. * The current state of the form.
  167. * @param $context
  168. * An associative array. See hook_field_widget_form_alter() for the structure
  169. * and content of the array.
  170. *
  171. * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
  172. * @see hook_field_widget_form_alter()
  173. */
  174. function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  175. // Code here will only act on widgets of type WIDGET_TYPE. For example,
  176. // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  177. // widgets of type 'mymodule_autocomplete'.
  178. $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
  179. }
  180. /**
  181. * @} End of "defgroup field_widget".
  182. */
  183. /**
  184. * @defgroup field_formatter Field Formatter API
  185. * @{
  186. * Define Field API formatter types.
  187. *
  188. * Field API formatters specify how fields are displayed when the entity to
  189. * which the field is attached is displayed. Fields of a given
  190. * @link field_types field type @endlink may be displayed using more than one
  191. * formatter. In this case, the Field UI module allows the site builder to
  192. * choose which formatter to use.
  193. *
  194. * Formatters are Plugins managed by the
  195. * \Drupal\Core\Field\FormatterPluginManager class. A formatter is a plugin
  196. * annotated with class \Drupal\Core\Field\Annotation\FieldFormatter that
  197. * implements \Drupal\Core\Field\FormatterInterface (in most cases, by
  198. * subclassing \Drupal\Core\Field\FormatterBase). Formatter plugins need to be
  199. * in the namespace \Drupal\{your_module}\Plugin\Field\FieldFormatter.
  200. *
  201. * @see field
  202. * @see field_types
  203. * @see field_widget
  204. * @see plugin_api
  205. */
  206. /**
  207. * Perform alterations on Field API formatter types.
  208. *
  209. * @param array $info
  210. * An array of information on existing formatter types, as collected by the
  211. * annotation discovery mechanism.
  212. */
  213. function hook_field_formatter_info_alter(array &$info) {
  214. // Let a new field type re-use an existing formatter.
  215. $info['text_default']['field_types'][] = 'my_field_type';
  216. }
  217. /**
  218. * @} End of "defgroup field_formatter".
  219. */
  220. /**
  221. * Returns the maximum weight for the entity components handled by the module.
  222. *
  223. * Field API takes care of fields and 'extra_fields'. This hook is intended for
  224. * third-party modules adding other entity components (e.g. field_group).
  225. *
  226. * @param string $entity_type
  227. * The type of entity; e.g. 'node' or 'user'.
  228. * @param string $bundle
  229. * The bundle name.
  230. * @param string $context
  231. * The context for which the maximum weight is requested. Either 'form' or
  232. * 'display'.
  233. * @param string $context_mode
  234. * The view or form mode name.
  235. *
  236. * @return int
  237. * The maximum weight of the entity's components, or NULL if no components
  238. * were found.
  239. *
  240. * @ingroup field_info
  241. */
  242. function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) {
  243. $weights = [];
  244. foreach (my_module_entity_additions($entity_type, $bundle, $context, $context_mode) as $addition) {
  245. $weights[] = $addition['weight'];
  246. }
  247. return $weights ? max($weights) : NULL;
  248. }
  249. /**
  250. * @addtogroup field_purge
  251. * @{
  252. */
  253. /**
  254. * Acts when a field storage definition is being purged.
  255. *
  256. * In field_purge_field_storage(), after the storage definition has been removed
  257. * from the system, the entity storage has purged stored field data, and the
  258. * field definitions cache has been cleared, this hook is invoked on all modules
  259. * to allow them to respond to the field storage being purged.
  260. *
  261. * @param $field_storage \Drupal\field\Entity\FieldStorageConfig
  262. * The field storage being purged.
  263. */
  264. function hook_field_purge_field_storage(\Drupal\field\Entity\FieldStorageConfig $field_storage) {
  265. db_delete('my_module_field_storage_info')
  266. ->condition('uuid', $field_storage->uuid())
  267. ->execute();
  268. }
  269. /**
  270. * Acts when a field is being purged.
  271. *
  272. * In field_purge_field(), after the field definition has been removed
  273. * from the system, the entity storage has purged stored field data, and the
  274. * field info cache has been cleared, this hook is invoked on all modules to
  275. * allow them to respond to the field being purged.
  276. *
  277. * @param $field
  278. * The field being purged.
  279. */
  280. function hook_field_purge_field(\Drupal\field\Entity\FieldConfig $field) {
  281. db_delete('my_module_field_info')
  282. ->condition('id', $field->id())
  283. ->execute();
  284. }
  285. /**
  286. * @} End of "addtogroup field_purge".
  287. */
  288. /**
  289. * @} End of "addtogroup hooks".
  290. */