2

I have created a custom entity type (without bundles) using the Drupal Console command. Everything works fine with the out the box stuff. I can configure fields, the order of the form and the order of the output display.

However, it seems impossible to add and new View Modes to my new entity type. I have tried adding a new mode to the "Content" view modes section but I don't have the collapsable details section to be able to enable new view modes.

Is there a setting I have missed in the entity code that will display this?

  • It is possible to add new view modes in UI. When you go to /admin/structure/display-modes/view/add you should be able the select the custom entity (and not "Content", which would be the wrong entity). – 4k4 Oct 21 '18 at 9:30
  • Newly created custom content entities do NOT show up in the UI at /admin/structure/display-modes/view/add, unless you also explicitly create at least one view mode programmatically for that entity (see @Jimmyb_1991's answer below). After at least one view mode exists, additional view modes can be added using the UI. – Hudri Dec 15 '19 at 16:12
1

As always, I found the answer just after posting the question.

For others looking for the same thing, you need to create an install .yml file in your custom module. I found this by looking at the Media module in core.

In your module create config/install/core.entity_view_mode.ENTITY_TYPE_ID.NAME_OF_VIEW_MODE_TO_CREATE.yml that implements the following:

langcode: en # Or your default site language
status: false
dependencies:
  module:
    - my_module
id: entity_type_id.name_of_view_mode_to_create
label: 'My new View Mode'
targetEntityType: entity_type_id
cache: true

I hope this helps someone else too :D

0

This can also be achieved through the UI.

  1. Logged in as admin, press Structure > Display modes > View modes > Add ( /admin/structure/display-modes/view/add )
  2. Select your custom entity type to add a display mode
  3. Enter a name for the display mode
  • This does not work for the very first view mode of your custom content entity. The first one must be created using code/config, only after that additional views modes can be added through the UI – Hudri Dec 15 '19 at 16:14

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.