In my code, I need to get a list of all the view modes defined for nodes.

In Drupal 7, it was available via entity_get_info(). What is the equivalent method for producing a list of available view modes in Drupal 8?


View modes are config entities, so an entity query would do the trick:

$view_modes = Drupal::entityQuery('entity_view_mode')
  ->condition('targetEntityType', 'node')
  • It is no (longer ?) entity_view_mode but entity_view_display. You can also specificate the bundles with ->condition('bundle', ['full', 'teaser'] Jun 13 '19 at 14:28
  • @PierreNoel It's still entity_view_mode to get view modes... entity_view_display is something different (when you want to get the settings for how fields are displayed within a particular view mode)
    – Clive
    Jun 13 '19 at 14:42

You can use entity_display.repository service (EntityDisplayRepositoryInterface).

// Getting all display modes configured for nodes.

// Getting all display modes configured for nodes with content type article
    ->getViewModeOptionsByBundle('node', 'article')

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.