2

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()->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?

4

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()->getStorage('entity_view_display');
$view_display = $storage->load('node.article.default');

modify it and save it:

$view_display->save();

Why you want to use config entities?

You can get the array of the configuration:

$config = $view_display->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->setComponent('body', [
     'type' => 'text_summary_or_trimmed',
     'settings' => ['trim_length' => '200'],
     'weight' => 1,
   ])
   ->removeComponent('field_image')
   ->save();

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

  • Great answer, thanks! I knew this was going to be much easier leveraging config entities, that's exactly why I asked :-) So regarding the strategy part, you simply take the entity_view_display from the config YAML, write it as camelCase, and find that class? That's nice and simple. Knowing this, I noticed there is an even shorter way to load the config entity: $view_display = EntityViewDisplay::load('node.article.default');. – marcvangend Aug 26 '16 at 12:19
  • 1
    Yes, the static load() for entities is shorter, but only works if you know the entity type. If it is in a variable you have to use the entity type manager. – 4k4 Aug 26 '16 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.