Drupal 8.7 included a fix so that node field rendering respects configurable field display. The same fix has now been applied to other core entity types.
The following base fields are not configurable via the field UI by default:
- aggregator_feed.title
- aggregator_item.title
- taxonomy_term.name
Furthermore developers who wished enable configurable display of these fields have until now been obstructed by special preprocessing.
There is now a way to disable this special preprocessing, so that it becomes possible for developers to make the base field display configurable via the field UI. The contrib module Manage Display provides this.
Before
Developers could set one of these fields to be configurable, for example by:
function mymodule_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'taxonomy_term') {
$base_field_definitions['name']->setDisplayConfigurable('view', TRUE);
}
}If a developer did this, the field would appear in the "Manage Display" field UI. However any changes made there would have no effect as the name would be rendered in a fixed position at the top of the taxonomyterm template. This happened because of special preprocessing in template_preprocess_node to take the title out of the variable {{ content }} and move it into {{ name }}.
After
Developers can bypass this special base field preprocessing by setting the enable_base_field_custom_preprocess_skipping on an affected entity type:
function mymodule_entity_type_build(array &$entity_types) {
$entity_types['taxonomy_term']->set('enable_base_field_custom_preprocess_skipping', TRUE);
}
function mymodule_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'taxonomy_term') {
$base_field_definitions['name']->setDisplayConfigurable('view', TRUE);
}
}
Using both hooks like this, the base fields will correctly respond to changes in the field UI settings.
Template changes
The special variables representing the preprocessed base fields are now optional in templates. The core templates for Stable and Classy have been modified with conditional logic before accessing:
- aggregator_feed.title
- aggregator_item.title
- taxonomy_term.name
Contrib theme developers should make the corresponding changes to their templates so that they work correctly if configurable display of base fields is enabled. However the changes are fully back-compatible, so that existing sites that do not enable configurable display of base fields do not need to change templates.
Future plans
Work is continuing to bring configurable display of base fields directly into core, without requiring contrib modules.
The long-term intention (perhaps in Drupal 9) is to remove the special preprocessing entirely and enable configurable display of base fields by default. It would still be possible for a developer to go back to having the title hard-coded in the template using a hook plus the following approach in the template.
{{ content.title }}
{{ content|without('title') }}