Problem/Motivation
In #2939594: Umami missing some Media "plumbing" found in Standard profile we discovered that some default settings provided by modules to make it easier the UX of unexperienced users is not added to any viewmode except for the default form/view displays.
It turns out it is by design, in \Drupal\field_ui\Form\FieldStorageAddForm we have:
/**
* Configures the field for the default view mode.
*
* @param string $field_name
* The field name.
* @param string|null $formatter_id
* (optional) The plugin ID of the formatter. Defaults to NULL.
* @param array $formatter_settings
* (optional) An array of formatter settings. Defaults to an empty array.
*/
protected function configureEntityViewDisplay($field_name, $formatter_id = NULL, array $formatter_settings = []) {
$options = [];
if ($formatter_id) {
$options['type'] = $formatter_id;
if (!empty($formatter_settings)) {
$options['settings'] = $formatter_settings;
}
}
// Make sure the field is displayed in the 'default' view mode (using
// default formatter and settings). It stays hidden for other view
// modes until it is explicitly configured.
entity_get_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($field_name, $options)
->save();
}
Unfortunatelly this happens just after the field config is saved:
...
try {
$field = $this->entityManager->getStorage('field_config')->create([
'field_name' => $field_name,
'entity_type' => $this->entityTypeId,
'bundle' => $this->bundle,
'label' => $values['existing_storage_label'],
]);
$field->save();
$this->configureEntityFormDisplay($field_name);
$this->configureEntityViewDisplay($field_name);
...
So modules don't have opportunity to modify any of this if trying to use hook_ENTITY_TYPE_insert() or similar...
Proposed resolution
Give modules the opportunity to modify these default display options, by providing a new alter hook.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
marcoscanoFirst proof-of-concept. Another approach could be to pass the modified but unsaved display, and then just save it after the alter hook.
Opinions?
(No tests added, gathering feedback on the approach first)
Comment #3
marcoscanoOops, this is the good one.
Comment #5
andypostCould be useful for domain module
Comment #6
marcoscanoComment #7
amateescu commentedI think I understand the issue that this patch is trying to solve, but I will defer to @Berdir because he's been thinking about this problem space for much longer than me.
My opinion is that trying to keep the default/full view modes in sync like attempted in #2939594: Umami missing some Media "plumbing" found in Standard profile is error-prone and we should try to address #2844203: Improve/Simplify situation around Default/Full view modes/view displays first.
Comment #17
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.