Problem/Motivation
I was doing this in a kernel test:
$view_display = \Drupal::service('entity_display.repository')->getViewDisplay('node', 'page');
$view_display->removeComponent('created');
$view_display->save();
and found myself down a rabbithole trying to figure out why the display settings for 'created' were coming back as soon as I saved display.
The reason is here:
if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) {
and here:
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the node was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
-- the field wasn't configurable for view displays!
Steps to reproduce
Proposed resolution
removeComponent() and setComponent() should throw an exception or do an assert() if you call them for a field that isn't configurable.