7

I want to add a view mode for particular entity type programmatically in Drupal 8.I think the documentation on view modes for Drupal 8 has not been provided like it was provided for Drupal 7 hence I am facing problem in implementing this.Any reference from module in core would help.Thanks

What needs to be done if I want to add view modes to many entities?

13

View modes are configuration entities.

Like anything else that is a config entity, it's very simply to put into "code": Make it default configuration. Create it in the UI, do a configuration export (admin/config/development/configuration/single/export), and then place core.entity_view_mode.$entity_type.$view_mode.yml file into config/install inside your module.

The view mode will then automatically be imported and created when the module is installed.

Note that it's not hardcoded then, so a user could remove it. If you want to prevent that, you can implement hook_entity_view_mode_access() to deny delete access to your entity for everyone.

By default, you will have a problem re-installing the module because it will not remove that view mode when you uninstall the module and display an error when you install it again. There are two ways to fix that:

  • Move the config to config/optional instead of config/install. Then it will only be created if it doesn't exist yet.
  • Add an enforced dependency on your module, which will ensure that it will be removed when the module is uninstalled. Note that this only works if you had this in place when you installed the module the first time.

Like this:

  dependencies:
    enforced:
      module:
        - yourmodule
  • Thanks,so if I want to make view modes available for different entities like node,comments,user,block and many other then I will have to export configuration for all of these individually?Or is there a way to create new view modes by using some functionality of entity api. – user2750995 May 27 '15 at 12:28
  • Yes, you can also do that with code, in the same way that every other entity is created. The problem is knowing which entity types to create the view mode for. Because some might already exist when the module is enabled, others might come later. See drupal.org/node/2430821, there I suggested to remove that dynamic logic in token.module (which is doing exactly this right now), and instead just document how to name a view mode in the UI so it works with the token.module. – Berdir May 27 '15 at 18:39
  • how to make this view mode get removed automatically when user uninstalls the module? – user2750995 Jun 16 '15 at 10:11
  • 1
    That should already happen through the config dependency system. If not, then that's a core bug that you should open, link here and I'll comment there. – Berdir Jun 16 '15 at 22:27
  • 1
    when I uninstall module there is no problem but when I reinstall module it can't install because the page says $view_mode exists in active configuration. If this is a error u think then should I post it as an issue of configuration component? or is there is way to solve this problem? – user2750995 Jun 16 '15 at 23:45
6

To create a View mode in Drupal 8 (i.e. a custom manage display), this can now be accomplished through the UI:

admin/structure/display-modes/view

Or if you'd like to programatically provide a View mode with your module, follow these steps:

  1. Create a custom module
  2. Create a config/install directory within your module
  3. Inside of config/install, create a file called core.entity_view_mode.node.my_module.yml

Then inside of core.entity_view_mode.node.my_module.yml add this:

langcode: en
status: true
dependencies:
  module:
    - node
    - my_module
id: node.my_module
label: 'My Module'
targetEntityType: node
cache: true

Then install your module (you'll have to uninstall and then re-install your module if it is already installed). You'll now have a custom view mode that can be enabled for all of your content types.

To support other types of entities (e.g. comment, user) just create a new .yml file and replace occurrences of node with your desired entity type.

0
$entity_type="node"; //The entity for which you want to create the view mode
$view_mode="example"; //The name of the new view mode
$settings=Array();
$settings["id"]=$entity_type.".".$view_mode;
$settings["targetEntityType"]=$entity_type;
$evm=Drupal\Core\Entity\Entity\EntityViewMode::create($settings);
$evm->save();

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.