In Drupal 7, Field UI provided a hardcoded UI to input default values for a field on the "field instance" edit form, by simply embedding the widget currently configured for the field. Widgets could opt out of this behavior by specifying 'default value' = FIELD_BEHAVIOR_NONE
in their hook_field_widget_info() definition.
However, opting out was generally a choice related to the field type rather than to a specific widget. Typical examples were:
- date fields, whose "useful" default values are like "current date", "next monday", which are values that no date widget allows to input.
- file / image fields, that use their own separate concept of default values
In Drupal 8, the "field type" classes are in control of the UI for their default values, and the 'default_value' entry in widget definitions (annotations in widget classes) is not supported anymore.
\Drupal\Core\Field\FieldItemListInterface includes the following methods, that allow field types to define the "default values" subform to be displayed by Field UI:
- defaultValuesForm()
- defaultValuesFormValidate()
- defaultValuesFormSubmit()
Note that the methods live on the Field classes, as opposed to most "field type" methods that live on the FieldItem classes.
The \Drupal\Core\Field\FieldItemList
base class, used by most field types, provides base implementations of those methods that reproduce the "default" behavior (i.e. "display the widget currently configured for the field").
Field types that need to provide a different UI (or no UI at all) for the "default values' input need to:
- provide their own field class, extending FieldItemList
, and override one or more of the methods above,
- specify in their @FieldType annotation the name of the field class to use ('list_class' entry)
Code samples
Drupal 7
function file_field_widget_info() {
return array(
'file_generic' => array(
'label' => t('File'),
'field types' => array('file'),
// (...)
'behaviors' => array(
'default value' => FIELD_BEHAVIOR_NONE,
),
),
);
}
Drupal 8
in file/src/Plugin/Field/FieldType/FileItem.php
:
@FieldType(
* id = "file",
* label = @Translation("File"),
* (...)
* list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList"
* )
class FileItem implements EntityReferenceItem {
// (...)
}
in file/src/Plugin/Field/FieldType/FileFieldItemList.php
:
class FileFieldItemList extends FieldItemList {
/**
* Simply opt-out of the "default_value' input.
*/
public function defaultValuesForm(array &$form, array &$form_state) { }
}