field.api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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
  132. * \Drupal\Core\Field\WidgetBaseInterface::form().
  133. * @param $form_state
  134. * The current state of the form.
  135. * @param $context
  136. * An associative array containing the following key-value pairs:
  137. * - form: The form structure to which widgets are being attached. This may be
  138. * a full form structure, or a sub-element of a larger form.
  139. * - widget: The widget plugin instance.
  140. * - items: The field values, as a
  141. * \Drupal\Core\Field\FieldItemListInterface object.
  142. * - delta: The order of this item in the array of subelements (0, 1, 2, etc).
  143. * - default: A boolean indicating whether the form is being shown as a dummy
  144. * form to set default values.
  145. *
  146. * @see \Drupal\Core\Field\WidgetBaseInterface::form()
  147. * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
  148. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  149. */
  150. function hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  151. // Add a css class to widget form elements for all fields of type mytype.
  152. $field_definition = $context['items']->getFieldDefinition();
  153. if ($field_definition->getType() == 'mytype') {
  154. // Be sure not to overwrite existing attributes.
  155. $element['#attributes']['class'][] = 'myclass';
  156. }
  157. }
  158. /**
  159. * Alter widget forms for a specific widget provided by another module.
  160. *
  161. * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
  162. * specific widget form, rather than using hook_field_widget_form_alter() and
  163. * checking the widget type.
  164. *
  165. * @param $element
  166. * The field widget form element as constructed by
  167. * \Drupal\Core\Field\WidgetBaseInterface::form().
  168. * @param $form_state
  169. * The current state of the form.
  170. * @param $context
  171. * An associative array. See hook_field_widget_form_alter() for the structure
  172. * and content of the array.
  173. *
  174. * @see \Drupal\Core\Field\WidgetBaseInterface::form()
  175. * @see \Drupal\Core\Field\WidgetBase::formSingleElement()
  176. * @see hook_field_widget_form_alter()
  177. */
  178. function hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  179. // Code here will only act on widgets of type WIDGET_TYPE. For example,
  180. // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  181. // widgets of type 'mymodule_autocomplete'.
  182. $element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
  183. }
  184. /**
  185. * @} End of "defgroup field_widget".
  186. */
  187. /**
  188. * @defgroup field_formatter Field Formatter API
  189. * @{
  190. * Define Field API formatter types.
  191. *
  192. * Field API formatters specify how fields are displayed when the entity to
  193. * which the field is attached is displayed. Fields of a given
  194. * @link field_types field type @endlink may be displayed using more than one
  195. * formatter. In this case, the Field UI module allows the site builder to
  196. * choose which formatter to use.
  197. *
  198. * Formatters are Plugins managed by the
  199. * \Drupal\Core\Field\FormatterPluginManager class. A formatter is a plugin
  200. * annotated with class \Drupal\Core\Field\Annotation\FieldFormatter that
  201. * implements \Drupal\Core\Field\FormatterInterface (in most cases, by
  202. * subclassing \Drupal\Core\Field\FormatterBase). Formatter plugins need to be
  203. * in the namespace \Drupal\{your_module}\Plugin\Field\FieldFormatter.
  204. *
  205. * @see field
  206. * @see field_types
  207. * @see field_widget
  208. * @see plugin_api
  209. */
  210. /**
  211. * Perform alterations on Field API formatter types.
  212. *
  213. * @param array $info
  214. * An array of information on existing formatter types, as collected by the
  215. * annotation discovery mechanism.
  216. */
  217. function hook_field_formatter_info_alter(array &$info) {
  218. // Let a new field type re-use an existing formatter.
  219. $info['text_default']['field_types'][] = 'my_field_type';
  220. }
  221. /**
  222. * @} End of "defgroup field_formatter".
  223. */
  224. /**
  225. * Returns the maximum weight for the entity components handled by the module.
  226. *
  227. * Field API takes care of fields and 'extra_fields'. This hook is intended for
  228. * third-party modules adding other entity components (e.g. field_group).
  229. *
  230. * @param string $entity_type
  231. * The type of entity; e.g. 'node' or 'user'.
  232. * @param string $bundle
  233. * The bundle name.
  234. * @param string $context
  235. * The context for which the maximum weight is requested. Either 'form' or
  236. * 'display'.
  237. * @param string $context_mode
  238. * The view or form mode name.
  239. *
  240. * @return int
  241. * The maximum weight of the entity's components, or NULL if no components
  242. * were found.
  243. *
  244. * @ingroup field_info
  245. */
  246. function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) {
  247. $weights = [];
  248. foreach (my_module_entity_additions($entity_type, $bundle, $context, $context_mode) as $addition) {
  249. $weights[] = $addition['weight'];
  250. }
  251. return $weights ? max($weights) : NULL;
  252. }
  253. /**
  254. * @addtogroup field_purge
  255. * @{
  256. */
  257. /**
  258. * Acts when a field storage definition is being purged.
  259. *
  260. * In field_purge_field_storage(), after the storage definition has been removed
  261. * from the system, the entity storage has purged stored field data, and the
  262. * field definitions cache has been cleared, this hook is invoked on all modules
  263. * to allow them to respond to the field storage being purged.
  264. *
  265. * @param $field_storage \Drupal\field\Entity\FieldStorageConfig
  266. * The field storage being purged.
  267. */
  268. function hook_field_purge_field_storage(\Drupal\field\Entity\FieldStorageConfig $field_storage) {
  269. db_delete('my_module_field_storage_info')
  270. ->condition('uuid', $field_storage->uuid())
  271. ->execute();
  272. }
  273. /**
  274. * Acts when a field is being purged.
  275. *
  276. * In field_purge_field(), after the field definition has been removed
  277. * from the system, the entity storage has purged stored field data, and the
  278. * field info cache has been cleared, this hook is invoked on all modules to
  279. * allow them to respond to the field being purged.
  280. *
  281. * @param $field
  282. * The field being purged.
  283. */
  284. function hook_field_purge_field(\Drupal\field\Entity\FieldConfig $field) {
  285. db_delete('my_module_field_info')
  286. ->condition('id', $field->id())
  287. ->execute();
  288. }
  289. /**
  290. * @} End of "addtogroup field_purge".
  291. */
  292. /**
  293. * @} End of "addtogroup hooks".
  294. */