The new Entity/Field API in Drupal 8 unified the notion of "field" to cover all the "values held in an ContentEntity" :
- "base" (hardcoded) fields like 'node title' or 'node author',
- and configurable fields (what Drupal 7 called "fields") like 'node body' or any custom field added in Field UI.
In Drupal 7, widgets and formatters were only applicable to the latter.
In Drupal 8, widgets and formatters can be applied to base fields too: base fields, defined in code (typically in the baseFieldDefinitions() method in the entity class, like Node or User), can specify in their "field definition" if and how they want to be part of the generic widget/formatter rendering pipeline.
This generic rendering notably provides automatic support for in-place editing if quickedit.module is enabled.
- The BaseFieldDefinition::setDisplayOptions() method can be used to specify a widget / a formatter (or 'hidden') to use for rendering in entity forms / in displayed entities.
- The BaseFieldDefinition::setDisplayConfigurable() method can additionally be used to opt-in for configurability in Field UI "Manage display" screens, allowing site admins to choose a different widget / formatter, change the settings or reorder the field.
See example below.
Fields that do not specify anything stay out of the pipeline, and keep their current behavior: they are not displayed, or possibly displayed by their own custom ad-hoc render API / Form API code, and possibly exposed as "extra fields" using hook_field_extra_fields() to allow minimal configurability in Field UI (visible / hidden, ordering).
Example for node title
Drupal 7
node.module
// Called from node_form() :
function node_content_form($node, $form_state) {
...
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
'#default_value' => $node->title,
'#maxlength' => 255,
'#weight' => -5,
);
...
return $form;
}
function template_preprocess_node(&$variables) {
...
$variables['title'] = $node->title;
...
}
Drupal 8
core/modules/node/src/Entity/Node.php
class Node extends ContentEntityBase implements NodeInterface {
public static function baseFieldDefinitions($entity_type) {
$fields['title'] = BaseFieldDefinition::create('text')
->setLabel(t('Title'))
->setRequired(TRUE)
...
// Field is rendered in viewed entities using the 'text_default' formatter.
// This is hardcoded and cannot be changed in configuration.
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => -5,
))
// Field is rendered in entity forms using the 'text_textfield' widget by default,
// but those options that can be overriden through configuration.
->setDisplayOptions('form', array(
'type' => 'text_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
...
return $fields;
}
...
}
Comments
Note that hook_field_extra
Note that hook_field_extra_fields() was renamed to hook_entity_extra_field_info(). See > https://drupal.org/node/2227585
Mastodon account: @Artusamak
https://happyculture.coop
Is it possible to make an
Is it possible to make an extra field configurable?