Problem/Motivation
In #3623275: Replace the instances admin list with a View, we have replaced (most of) InstanceListBuilder by a View.
However, there are 2 tasks we were not able to achieve.
Replacing views_pre_build hook
This hook exists only because #3629063: Instances list: load instances from storage, not from each buildable has not be merged yet, to use DisplayBuildableInterface::collectInstances() which will not be necessary.
Replacing InstanceDerived
InstanceDerived exists only because we didn't use computed fields in Instance entity, and we relied too much on logic inside our own public methods not managed by the Field API
We can move the logic of:
::label()to src/Plugin/Field/InstanceLabelItemList.php::getProfile()to src/Plugin/Field/InstanceProfileItemList.php::getPublicationStatus()to src/Plugin/Field/InstancePublicationItemList.php
It is also the opportunity to move PublicationStatus::badge() (a renderbale in a PHP enum, so weird...) to a proper field formatter: PublicationBadgeFormatter. So, instead of:
$instance->getPublicationStatus()->badge();
We will be able to do:
$instance->get('publication')->view('default')
So, the additions to Instance base fields will look like that:
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Label'))
->setComputed(TRUE)
->setReadOnly(TRUE)
->setClass(InstanceLabelItemList::class);
$fields['publication'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Publication'))
->setComputed(TRUE)
->setReadOnly(TRUE)
->setClass(InstancePublicationItemList::class)
->setDisplayOptions('view', ['type' => PublicationBadgeFormatter::ID, 'label' => 'hidden']);
$fields['profile'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Profile'))
->setSetting('target_type', 'display_builder_profile')
->setComputed(TRUE)
->setReadOnly(TRUE)
->setClass(InstanceProfileItemList::class);
Views and other Druapl API will now be able to work with this data without needing weird mechanisms like InstanceDerived for Views.
It will also make Instance entity cleaner, with specific logic isolated in dedicated classes.
Important: strictly no interface must change. However, it may need an update of views.view.display_builder_instances.yml, so is it a breaking change?
Proposed resolution
Do the changes when #3623275: Replace the instances admin list with a View and #3629063: Instances list: load instances from storage, not from each buildable are merged.
Comments