How to change the entity view display configuration programmatically? In a hook_install implementation, I need to change the entity view display configuration of certain bundle / view mode combinations. For instance, I would like to change the 'teaser' view  mode of the 'article' bundle and hide 'field_image'.

When you export the configuration, you'll see those settings stored in core.entity_view_display.node.article.teaser.yml. I know it's possible to load the configuration like this:

$view_mode_config = \Drupal::configFactory()-&gt;getEditable('core.entity_view_display.node.article.default');


This would allow you to change and save the config object, but I suppose that is not the correct way to do it. I'm guessing there must be some kind of view mode manager class I should use to do this, but I haven't been able to find it.

So how do I change the entity view display configuration programmatically? 

Bonus question: When you find yourself in a situation like this, what are your strategies to explore the code and find the classes you need?
There are two configuration types. Simple configuration and configuration entities. In this case this is a configuration entity. The class name is in camelCase:

EntityViewDisplay.php

class EntityViewDisplay extends EntityDisplayBase implements EntityViewDisplayInterface


If you have a config entity for the configuration, you can load the config entity,

$storage = \Drupal::entityTypeManager()-&gt;getStorage('entity_view_display');
$view_display = $storage-&gt;load('node.article.default');


modify it and save it:

$view_display-&gt;save();


Why you want to use config entities?

You can get the array of the configuration:

$config = $view_display-&gt;toArray();


This is not different from what you get in the yml file or config api.

The advantage is, that config entities usually have method for special configuration tasks. With displays this is for example to add or remove components:

$view_display-&gt;setComponent('body', [
     'type' =&gt; 'text_summary_or_trimmed',
     'settings' =&gt; ['trim_length' =&gt; '200'],
     'weight' =&gt; 1,
   ])
   -&gt;removeComponent('field_image')
   -&gt;save();


That is much easier than to do this in the configuration array.