Skip to content

Integrating other modules

A module breaks with overrides when it finds the display by entity type, bundle and mode instead of using the display it is handed. Field Group does this, which is why theme_view_display_field_group exists. Two things help.

The runtime marker

The display handed to hook_entity_view() and hook_entity_display_build_alter() is a runtime copy of the override. It carries the override ID:

use Drupal\theme_view_display\Hook\ThemeViewDisplayHooks;

$override_id = $display->getThirdPartySetting(
  ThemeViewDisplayHooks::MARKER_PROVIDER,
  ThemeViewDisplayHooks::MARKER_KEY,
);

The value is NULL on a core display. The display has the content, hidden and third-party settings of the override, except the Display Builder settings.

To read your own data from the override, either read it from the display you were handed, which has the third-party settings of the override, or load the entity:

$override = \Drupal::entityTypeManager()->getStorage('theme_view_display')->load($override_id);

Hook order

hook_entity_view_alter() and hook_entity_display_build_alter() of your module run in the usual order. To replace what another module attached from the core display, order yours after it:

#[Hook('entity_view_alter', order: new OrderAfter(modules: ['field_group']))]

The Field Group sub-module does this to replace #fieldgroups.

The Manage display form

The form of an override has the ID theme_view_display_edit_form. Use hook_form_theme_view_display_edit_form_alter(). Both sub-modules do. The entity of the form is the override: $form_state->getFormObject()->getEntity().

Loads of the core display return a fresh object, so a form that reads the core display by ID sees its settings, not the override's. The Field Group sub-module changes that while Field Group's own form code runs, with hook_entity_view_display_load(). Copy that approach only when you cannot pass the display to the code you call.

Config schema

An override stores third-party settings, and config schema is checked on save in tests. Add a schema for your settings under the override type:

theme_view_display.display.*.*.*.*.third_party.my_module:
  type: core.entity_view_display.*.*.*.third_party.my_module

Display Builder buildable

The sub-module's plugin theme_entity_view follows the Display Builder buildable API, which is beta. See the Display Builder documentation for the contract.