0

Somewhat the continuation of Getting existing fields in hook_entity_bundle_field_info() with a similar problem. I'm using this hook now to make modifications not only to the bundle fields but their form and view settings as well. The probem I face is similar: it's restricted what queries I can make from this hook. This time it's using the entity_display.repository service. getViewModes() works but reading the config data with getViewDisplay() leads to the same infinite loop. I also tried workarounds -- \Drupal::entityQuery('entity_view_display') and getting the same storage on the entity type manager but, as they probably all lead to the same code, anyway, all three blow up.

And this time, it doesn't look like my oversight of not using the right hook. There's no direct hook for the view and form displays documented anywhere. The view modes have one but they only get the modes, not the displays, that's different. Yes, I know of hook_entity_view_display_alter() but this time this is a real alter(), not an info_alter(), it only influences the actual display, I tried, just like its form display counterpart.

The core node module has something remotely similar, node_add_body_field() but that doesn't reply to a hook call, it's called directly and I couldn't yet decide where to do it from in my own code. I still tend to contemplate this route, especially if you tell me this is a wild goose chase, a known limitation that I'm sure to run into and have no chance of solving...

1 Answer 1

0

Bundle fields live in configuration and you probably need to solve this entirely with config entity CRUD hooks like this:

/**
 * Implements hook_ENTITY_TYPE_insert() for 'field_config'.
 */
function mymodule_field_config_insert(FieldConfigInterface $field) {
  // don't run when this config is importing
  if ($field->isSyncing()) {
    return;
  }
  ...
}

or

/**
 * Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
 */
function mymodule_entity_view_display_presave(EntityViewDisplayInterface $display) {
  // don't run when this config is importing
  if ($display->isSyncing()) {
    return;
  }
  ...
}

See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/group/entity_crud/11.x

1
  • That's a pretty interesting approach, I could identify the scenarios where I need to call my separate function à la node style from, yes, including upon module installation. I do a few rapid tests but I feel I'll be back accepting it soon. :-)
    – Gábor
    Commented Jun 24 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.