When working with entity types, view-modes, fields and configuration in modules, you'll surely run into the situation that you have to add new configuration (from the config/install directory) .yml's into the active configuration in update hooks!
In my specific case today, I had to add a new paragraph entity view mode in an update hook.
I created the view mode in my Drupal dev environment installation, exported it via
drush cex
cleaned it up (removing uuid, etc.) and put it into the config/install directory.
That's fine for fresh installs of the module, but doesn't change anything for existing installations.
So we also had to import this additional config yml into the active configuration and that can be done with the following snippet, which surely needs to be modified for your special case:
<?php
/**
* Add "preview" entity view display for xyz paragraph type.
*/
function MY_MODULE_update_9000(&$sandbox) {
$config_id = 'core.entity_view_display.paragraph.xyz.preview';
$config_path = \Drupal::service('extension.list.module')->getPath('MY_MODULE') . '/config/install/' . $config_id .'.yml';
$data = \Symfony\Component\Yaml\Yaml::parseFile($config_path);
\Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE);
}
This might not be perfect and correct for all cases, but in some cases it might help.
This is the related issue and merge request: https://www.drupal.org/project/drowl_paragraphs/issues/3345569
Also see: