2009-02-05 03:42:58 +00:00
< ? php
2016-02-11 20:54:33 +00:00
/**
* @ file
* Field API documentation .
*/
2009-02-05 03:42:58 +00:00
/**
2012-03-09 16:23:34 +00:00
* @ addtogroup hooks
2009-02-05 03:42:58 +00:00
* @ {
*/
/**
* @ defgroup field_types Field Types API
* @ {
2012-09-27 15:44:17 +00:00
* Defines field , widget , display formatter , and storage types .
2009-02-05 03:42:58 +00:00
*
2012-03-09 16:23:34 +00:00
* In the Field API , each field has a type , which determines what kind of data
* ( integer , string , date , etc . ) the field can hold , which settings it provides ,
* and so on . The data type ( s ) accepted by a field are defined in
2013-06-25 10:27:47 +00:00
* hook_field_schema () .
2009-08-01 06:03:48 +00:00
*
2014-06-20 15:33:13 +00:00
* Field types are plugins annotated with class
2014-07-25 17:26:47 +00:00
* \Drupal\Core\Field\Annotation\FieldType , and implement plugin interface
2014-06-20 15:33:13 +00:00
* \Drupal\Core\Field\FieldItemInterface . Field Type plugins are managed by the
* \Drupal\Core\Field\FieldTypePluginManager class . Field type classes usually
* extend base class \Drupal\Core\Field\FieldItemBase . Field - type plugins need
* to be in the namespace \Drupal\ { your_module } \Plugin\Field\FieldType . See the
* @ link plugin_api Plugin API topic @ endlink for more information on how to
* define plugins .
*
2009-08-01 06:03:48 +00:00
* The Field Types API also defines two kinds of pluggable handlers : widgets
2012-03-09 16:23:34 +00:00
* and formatters . @ link field_widget Widgets @ endlink specify how the field
* appears in edit forms , while @ link field_formatter formatters @ endlink
* specify how the field appears in displayed entities .
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
*
2011-12-22 09:32:53 +00:00
* See @ link field Field API @ endlink for information about the other parts of
* the Field API .
2014-06-20 15:33:13 +00:00
*
* @ see field
* @ see field_widget
* @ see field_formatter
* @ see plugin_api
2009-02-05 03:42:58 +00:00
*/
2009-06-30 03:12:03 +00:00
/**
* Perform alterations on Field API field types .
*
* @ param $info
2013-06-25 10:27:47 +00:00
* Array of information on field types as collected by the " field type " plugin
* manager .
2009-06-30 03:12:03 +00:00
*/
function hook_field_info_alter ( & $info ) {
// Change the default widget for fields of type 'foo'.
if ( isset ( $info [ 'foo' ])) {
2020-05-09 20:20:16 +00:00
$info [ 'foo' ][ 'default_widget' ] = 'mymodule_widget' ;
2009-06-30 03:12:03 +00:00
}
}
2018-01-03 21:29:24 +00:00
/**
* Perform alterations on preconfigured field options .
*
* @ param array $options
* Array of options as returned from
* \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface :: getPreconfiguredOptions () .
* @ param string $field_type
* The field type plugin ID .
*
* @ see \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface :: getPreconfiguredOptions ()
*/
function hook_field_ui_preconfigured_options_alter ( array & $options , $field_type ) {
// If the field is not an "entity_reference"-based field, bail out.
/** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
$field_type_manager = \Drupal :: service ( 'plugin.manager.field.field_type' );
$class = $field_type_manager -> getPluginClass ( $field_type );
if ( ! is_a ( $class , 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem' , TRUE )) {
return ;
}
// Set the default formatter for media in entity reference fields to be the
// "Rendered entity" formatter.
if ( ! empty ( $options [ 'media' ])) {
$options [ 'media' ][ 'entity_view_display' ][ 'type' ] = 'entity_reference_entity_view' ;
}
}
2014-08-07 21:16:24 +00:00
/**
* Forbid a field storage update from occurring .
*
* Any module may forbid any update for any reason . For example , the
* field ' s storage module might forbid an update if it would change
* the storage schema while data for the field exists . A field type
* module might forbid an update if it would change existing data ' s
* semantics , or if there are external dependencies on field settings
* that cannot be updated .
*
* To forbid the update from occurring , throw a
* \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException .
*
* @ param \Drupal\field\FieldStorageConfigInterface $field_storage
* The field storage as it will be post - update .
* @ param \Drupal\field\FieldStorageConfigInterface $prior_field_storage
* The field storage as it is pre - update .
*
* @ see entity_crud
*/
function hook_field_storage_config_update_forbid ( \Drupal\field\FieldStorageConfigInterface $field_storage , \Drupal\field\FieldStorageConfigInterface $prior_field_storage ) {
2014-09-27 11:08:55 +00:00
if ( $field_storage -> module == 'options' && $field_storage -> hasData ()) {
// Forbid any update that removes allowed values with actual data.
$allowed_values = $field_storage -> getSetting ( 'allowed_values' );
$prior_allowed_values = $prior_field_storage -> getSetting ( 'allowed_values' );
2015-12-23 14:28:29 +00:00
$lost_keys = array_keys ( array_diff_key ( $prior_allowed_values , $allowed_values ));
2015-02-10 13:39:31 +00:00
if ( _options_values_in_use ( $field_storage -> getTargetEntityTypeId (), $field_storage -> getName (), $lost_keys )) {
Issue #2055851 by andypost, sja112, jungle, dawehner, Mac_Weber, borisson_, fietserwin, xjm, init90, Gábor Hojtsy, effulgentsia, tim.plunkett: [backport] Remove translation of exception messages
2020-05-19 10:46:11 +00:00
throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException ( " A list field ' { $field_storage -> getName () } ' with existing data cannot have its keys changed. " );
2014-08-07 21:16:24 +00:00
}
}
}
2009-02-05 03:42:58 +00:00
/**
2012-05-17 12:58:49 +00:00
* @ } End of " defgroup field_types " .
2012-03-09 16:23:34 +00:00
*/
/**
* @ defgroup field_widget Field Widget API
* @ {
* Define Field API widget types .
*
* Field API widgets specify how fields are displayed in edit forms . Fields of a
* given @ link field_types field type @ endlink may be edited using more than one
* widget . In this case , the Field UI module allows the site builder to choose
2012-09-26 19:39:39 +00:00
* which widget to use .
*
* Widgets are Plugins managed by the
2014-06-20 15:33:13 +00:00
* \Drupal\Core\Field\WidgetPluginManager class . A widget is a plugin annotated
2014-07-24 23:17:15 +00:00
* with class \Drupal\Core\Field\Annotation\FieldWidget that implements
2014-06-20 15:33:13 +00:00
* \Drupal\Core\Field\WidgetInterface ( in most cases , by
* subclassing \Drupal\Core\Field\WidgetBase ) . Widget plugins need to be in the
* namespace \Drupal\ { your_module } \Plugin\Field\FieldWidget .
2009-04-13 05:18:18 +00:00
*
2015-12-02 20:18:33 +00:00
* Widgets are @ link form_api Form API @ endlink elements with additional
* processing capabilities . The methods of the WidgetInterface object are
* typically called by respective methods in the
2014-09-02 09:36:57 +00:00
* \Drupal\Core\Entity\Entity\EntityFormDisplay class .
2012-03-09 16:23:34 +00:00
*
* @ see field
* @ see field_types
* @ see field_formatter
2014-06-20 15:33:13 +00:00
* @ see plugin_api
2012-03-09 16:23:34 +00:00
*/
2009-08-01 06:03:48 +00:00
/**
* Perform alterations on Field API widget types .
*
2012-09-26 19:39:39 +00:00
* @ param array $info
2014-03-26 09:32:27 +00:00
* An array of information on existing widget types , as collected by the
2012-09-26 19:39:39 +00:00
* annotation discovery mechanism .
2009-08-01 06:03:48 +00:00
*/
2012-09-26 19:39:39 +00:00
function hook_field_widget_info_alter ( array & $info ) {
2009-08-01 06:03:48 +00:00
// Let a new field type re-use an existing widget.
2012-09-26 19:39:39 +00:00
$info [ 'options_select' ][ 'field_types' ][] = 'my_field_type' ;
2009-02-05 03:42:58 +00:00
}
2011-08-29 18:00:33 +00:00
/**
* Alter forms for field widgets provided by other modules .
*
2018-02-06 15:32:24 +00:00
* This hook can only modify individual elements within a field widget and
* cannot alter the top level ( parent element ) for multi - value fields . In most
* cases , you should use hook_field_widget_multivalue_form_alter () instead and
* loop over the elements .
*
2011-08-29 18:00:33 +00:00
* @ param $element
2017-12-30 22:13:45 +00:00
* The field widget form element as constructed by
* \Drupal\Core\Field\WidgetBaseInterface :: form () .
2011-08-29 18:00:33 +00:00
* @ param $form_state
2014-07-31 00:50:42 +00:00
* The current state of the form .
2011-08-29 18:00:33 +00:00
* @ param $context
2013-06-16 08:27:11 +00:00
* An associative array containing the following key - value pairs :
2012-04-17 22:13:57 +00:00
* - form : The form structure to which widgets are being attached . This may be
* a full form structure , or a sub - element of a larger form .
2013-06-16 08:27:11 +00:00
* - widget : The widget plugin instance .
2013-09-28 11:29:14 +00:00
* - items : The field values , as a
2013-10-19 08:12:13 +00:00
* \Drupal\Core\Field\FieldItemListInterface object .
2012-04-17 22:13:57 +00:00
* - delta : The order of this item in the array of subelements ( 0 , 1 , 2 , etc ) .
2012-08-15 14:20:02 +00:00
* - default : A boolean indicating whether the form is being shown as a dummy
* form to set default values .
2011-08-29 18:00:33 +00:00
*
2017-12-30 22:13:45 +00:00
* @ see \Drupal\Core\Field\WidgetBaseInterface :: form ()
2013-10-18 11:57:37 +00:00
* @ see \Drupal\Core\Field\WidgetBase :: formSingleElement ()
2012-04-17 22:13:57 +00:00
* @ see hook_field_widget_WIDGET_TYPE_form_alter ()
2018-02-06 15:32:24 +00:00
* @ see hook_field_widget_multivalue_form_alter ()
2011-08-29 18:00:33 +00:00
*/
2014-07-31 00:50:42 +00:00
function hook_field_widget_form_alter ( & $element , \Drupal\Core\Form\FormStateInterface $form_state , $context ) {
2011-08-29 18:00:33 +00:00
// Add a css class to widget form elements for all fields of type mytype.
2013-09-16 15:52:28 +00:00
$field_definition = $context [ 'items' ] -> getFieldDefinition ();
2013-12-09 23:19:58 +00:00
if ( $field_definition -> getType () == 'mytype' ) {
2011-08-29 18:00:33 +00:00
// Be sure not to overwrite existing attributes.
$element [ '#attributes' ][ 'class' ][] = 'myclass' ;
}
}
/**
* Alter widget forms for a specific widget provided by another module .
*
* Modules can implement hook_field_widget_WIDGET_TYPE_form_alter () to modify a
* specific widget form , rather than using hook_field_widget_form_alter () and
* checking the widget type .
*
2018-02-06 15:32:24 +00:00
* This hook can only modify individual elements within a field widget and
* cannot alter the top level ( parent element ) for multi - value fields . In most
* cases , you should use hook_field_widget_multivalue_WIDGET_TYPE_form_alter ()
* instead and loop over the elements .
*
2011-08-29 18:00:33 +00:00
* @ param $element
2017-12-30 22:13:45 +00:00
* The field widget form element as constructed by
* \Drupal\Core\Field\WidgetBaseInterface :: form () .
2011-08-29 18:00:33 +00:00
* @ param $form_state
2014-07-31 00:50:42 +00:00
* The current state of the form .
2011-08-29 18:00:33 +00:00
* @ param $context
2013-06-16 08:27:11 +00:00
* An associative array . See hook_field_widget_form_alter () for the structure
* and content of the array .
2011-08-29 18:00:33 +00:00
*
2017-12-30 22:13:45 +00:00
* @ see \Drupal\Core\Field\WidgetBaseInterface :: form ()
2013-10-18 11:57:37 +00:00
* @ see \Drupal\Core\Field\WidgetBase :: formSingleElement ()
2011-08-29 18:00:33 +00:00
* @ see hook_field_widget_form_alter ()
2018-02-06 15:32:24 +00:00
* @ see hook_field_widget_multivalue_WIDGET_TYPE_form_alter ()
2011-08-29 18:00:33 +00:00
*/
2014-07-31 00:50:42 +00:00
function hook_field_widget_WIDGET_TYPE_form_alter ( & $element , \Drupal\Core\Form\FormStateInterface $form_state , $context ) {
2011-08-29 18:00:33 +00:00
// Code here will only act on widgets of type WIDGET_TYPE. For example,
// hook_field_widget_mymodule_autocomplete_form_alter() will only act on
// widgets of type 'mymodule_autocomplete'.
2013-10-15 11:54:21 +00:00
$element [ '#autocomplete_route_name' ] = 'mymodule.autocomplete_route' ;
2011-08-29 18:00:33 +00:00
}
2018-02-06 15:32:24 +00:00
/**
* Alter forms for multi - value field widgets provided by other modules .
*
* To alter the individual elements within the widget , loop over
* \Drupal\Core\Render\Element :: children ( $elements ) .
*
* @ param array $elements
* The field widget form elements as constructed by
* \Drupal\Core\Field\WidgetBase :: formMultipleElements () .
* @ param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form .
* @ param array $context
* An associative array containing the following key - value pairs :
* - form : The form structure to which widgets are being attached . This may be
* a full form structure , or a sub - element of a larger form .
* - widget : The widget plugin instance .
* - items : The field values , as a
* \Drupal\Core\Field\FieldItemListInterface object .
* - default : A boolean indicating whether the form is being shown as a dummy
* form to set default values .
*
* @ see \Drupal\Core\Field\WidgetBaseInterface :: form ()
* @ see \Drupal\Core\Field\WidgetBase :: formMultipleElements ()
* @ see hook_field_widget_multivalue_WIDGET_TYPE_form_alter ()
*/
function hook_field_widget_multivalue_form_alter ( array & $elements , \Drupal\Core\Form\FormStateInterface $form_state , array $context ) {
// Add a css class to widget form elements for all fields of type mytype.
$field_definition = $context [ 'items' ] -> getFieldDefinition ();
if ( $field_definition -> getType () == 'mytype' ) {
// Be sure not to overwrite existing attributes.
$elements [ '#attributes' ][ 'class' ][] = 'myclass' ;
}
}
/**
* Alter multi - value widget forms for a widget provided by another module .
*
* Modules can implement hook_field_widget_multivalue_WIDGET_TYPE_form_alter () to
* modify a specific widget form , rather than using
* hook_field_widget_form_alter () and checking the widget type .
*
* To alter the individual elements within the widget , loop over
* \Drupal\Core\Render\Element :: children ( $elements ) .
*
* @ param array $elements
* The field widget form elements as constructed by
* \Drupal\Core\Field\WidgetBase :: formMultipleElements () .
* @ param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form .
* @ param array $context
* An associative array . See hook_field_widget_multivalue_form_alter () for
* the structure and content of the array .
*
* @ see \Drupal\Core\Field\WidgetBaseInterface :: form ()
* @ see \Drupal\Core\Field\WidgetBase :: formMultipleElements ()
* @ see hook_field_widget_multivalue_form_alter ()
*/
function hook_field_widget_multivalue_WIDGET_TYPE_form_alter ( array & $elements , \Drupal\Core\Form\FormStateInterface $form_state , array $context ) {
// Code here will only act on widgets of type WIDGET_TYPE. For example,
// hook_field_widget_multivalue_mymodule_autocomplete_form_alter() will only
// act on widgets of type 'mymodule_autocomplete'.
Issue #2829185 by vaplas, Jo Fitzgerald, anmolgoyal74, chipway, gaurav.kapoor, mark_fullmer, amit.drupal, cilefen, longwave, xjm, wturrell, anavarre: Fix spelling errors in Drupal core comments
2018-09-20 16:12:38 +00:00
// Change the autocomplete route for each autocomplete element within the
2018-02-06 15:32:24 +00:00
// multivalue widget.
foreach ( Element :: children ( $elements ) as $delta => $element ) {
$elements [ $delta ][ '#autocomplete_route_name' ] = 'mymodule.autocomplete_route' ;
}
}
2012-03-09 16:23:34 +00:00
/**
2012-05-17 12:58:49 +00:00
* @ } End of " defgroup field_widget " .
2012-03-09 16:23:34 +00:00
*/
/**
* @ defgroup field_formatter Field Formatter API
* @ {
* Define Field API formatter types .
*
* Field API formatters specify how fields are displayed when the entity to
* which the field is attached is displayed . Fields of a given
* @ link field_types field type @ endlink may be displayed using more than one
* formatter . In this case , the Field UI module allows the site builder to
2012-10-14 06:04:15 +00:00
* choose which formatter to use .
*
* Formatters are Plugins managed by the
2014-06-20 15:33:13 +00:00
* \Drupal\Core\Field\FormatterPluginManager class . A formatter is a plugin
2014-07-25 17:26:47 +00:00
* annotated with class \Drupal\Core\Field\Annotation\FieldFormatter that
2014-06-20 15:33:13 +00:00
* implements \Drupal\Core\Field\FormatterInterface ( in most cases , by
* subclassing \Drupal\Core\Field\FormatterBase ) . Formatter plugins need to be
* in the namespace \Drupal\ { your_module } \Plugin\Field\FieldFormatter .
2012-03-09 16:23:34 +00:00
*
* @ see field
* @ see field_types
* @ see field_widget
2014-06-20 15:33:13 +00:00
* @ see plugin_api
2012-03-09 16:23:34 +00:00
*/
2009-08-01 06:03:48 +00:00
/**
* Perform alterations on Field API formatter types .
*
2012-10-14 06:04:15 +00:00
* @ param array $info
2014-03-26 09:32:27 +00:00
* An array of information on existing formatter types , as collected by the
2012-10-14 06:04:15 +00:00
* annotation discovery mechanism .
2009-08-01 06:03:48 +00:00
*/
2012-10-14 06:04:15 +00:00
function hook_field_formatter_info_alter ( array & $info ) {
2009-08-01 06:03:48 +00:00
// Let a new field type re-use an existing formatter.
2014-10-12 11:51:20 +00:00
$info [ 'text_default' ][ 'field_types' ][] = 'my_field_type' ;
2009-08-01 06:03:48 +00:00
}
2009-02-05 03:42:58 +00:00
/**
2012-05-17 12:58:49 +00:00
* @ } End of " defgroup field_formatter " .
2009-02-05 03:42:58 +00:00
*/
2010-09-11 00:03:42 +00:00
/**
* Returns the maximum weight for the entity components handled by the module .
*
* Field API takes care of fields and 'extra_fields' . This hook is intended for
* third - party modules adding other entity components ( e . g . field_group ) .
*
2013-05-19 20:02:16 +00:00
* @ param string $entity_type
2010-09-11 00:03:42 +00:00
* The type of entity ; e . g . 'node' or 'user' .
2013-05-19 20:02:16 +00:00
* @ param string $bundle
2010-09-11 00:03:42 +00:00
* The bundle name .
2013-05-19 20:02:16 +00:00
* @ param string $context
* The context for which the maximum weight is requested . Either 'form' or
* 'display' .
* @ param string $context_mode
* The view or form mode name .
*
* @ return int
2010-09-11 00:03:42 +00:00
* The maximum weight of the entity ' s components , or NULL if no components
* were found .
2014-05-13 05:55:52 +00:00
*
* @ ingroup field_info
2010-09-11 00:03:42 +00:00
*/
2013-05-19 20:02:16 +00:00
function hook_field_info_max_weight ( $entity_type , $bundle , $context , $context_mode ) {
2017-03-04 01:20:24 +00:00
$weights = [];
2010-09-11 00:03:42 +00:00
2013-05-19 20:02:16 +00:00
foreach ( my_module_entity_additions ( $entity_type , $bundle , $context , $context_mode ) as $addition ) {
2010-09-11 00:03:42 +00:00
$weights [] = $addition [ 'weight' ];
}
return $weights ? max ( $weights ) : NULL ;
}
2009-02-05 03:42:58 +00:00
/**
2014-06-13 21:50:50 +00:00
* @ addtogroup field_purge
2009-02-05 03:42:58 +00:00
* @ {
*/
2010-04-22 22:51:54 +00:00
/**
2014-07-18 18:56:27 +00:00
* Acts when a field storage definition is being purged .
2010-04-22 22:51:54 +00:00
*
2014-07-18 18:56:27 +00:00
* In field_purge_field_storage (), after the storage definition has been removed
* from the system , the entity storage has purged stored field data , and the
* field definitions cache has been cleared , this hook is invoked on all modules
* to allow them to respond to the field storage being purged .
2010-04-22 22:51:54 +00:00
*
2014-07-18 18:56:27 +00:00
* @ param $field_storage \Drupal\field\Entity\FieldStorageConfig
* The field storage being purged .
2010-04-22 22:51:54 +00:00
*/
2014-07-18 18:56:27 +00:00
function hook_field_purge_field_storage ( \Drupal\field\Entity\FieldStorageConfig $field_storage ) {
2018-08-31 01:15:01 +00:00
\Drupal :: database () -> delete ( 'my_module_field_storage_info' )
2014-07-18 18:56:27 +00:00
-> condition ( 'uuid' , $field_storage -> uuid ())
2010-04-22 22:51:54 +00:00
-> execute ();
}
/**
2014-09-20 02:45:52 +00:00
* Acts when a field is being purged .
2010-04-22 22:51:54 +00:00
*
2014-09-20 02:45:52 +00:00
* In field_purge_field (), after the field definition has been removed
2015-02-04 10:08:41 +00:00
* from the system , the entity storage has purged stored field data , and the
2013-09-02 20:26:08 +00:00
* field info cache has been cleared , this hook is invoked on all modules to
2014-09-20 02:45:52 +00:00
* allow them to respond to the field being purged .
2010-04-22 22:51:54 +00:00
*
2014-09-20 02:45:52 +00:00
* @ param $field
* The field being purged .
2010-04-22 22:51:54 +00:00
*/
2014-09-20 02:45:52 +00:00
function hook_field_purge_field ( \Drupal\field\Entity\FieldConfig $field ) {
2018-08-31 01:15:01 +00:00
\Drupal :: database () -> delete ( 'my_module_field_info' )
2014-09-20 02:45:52 +00:00
-> condition ( 'id' , $field -> id ())
2010-04-22 22:51:54 +00:00
-> execute ();
}
2009-02-05 03:42:58 +00:00
/**
2014-06-13 21:50:50 +00:00
* @ } End of " addtogroup field_purge " .
2009-02-05 03:42:58 +00:00
*/
2011-11-25 03:35:12 +00:00
/**
2012-05-17 12:58:49 +00:00
* @ } End of " addtogroup hooks " .
2011-11-25 03:35:12 +00:00
*/