annotate core/modules/field/field.api.php @ 2:92f882872392

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