5

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?

3
  • 2
    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).
    – 4uk4
    Commented Oct 21, 2018 at 9:30
  • 1
    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
    Commented Dec 15, 2019 at 16:12
  • At least in newer Drupal versions (D10) you can now create view modes via the UI (for older versions I don't know if this works). To do this, go to the page mentioned and click on "Add view mode" at the top. The new custom entity should now also be selectable there.
    – egolive
    Commented Jan 26 at 14:53

2 Answers 2

10

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
2
  • 2
    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
    Commented Dec 15, 2019 at 16:14
  • Works for me. Nice and simple..
    – Daniel
    Commented Apr 15, 2021 at 20:28
7

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

1
  • I tried this and it did not work for me. Maybe because I need to reinstall the module? This is something I am averse to doing as I do not want to lose all content etc. (although might not be too bad) Solution for me was to navigate to /admin/structure/display-modes/view as already mentioned by Ashraf Abed
    – Daniel
    Commented Apr 15, 2021 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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