During the Drupal 8 release cycle hook_entity_view_mode_info
was introduced and then removed. View modes are now configuration entities, aka yml files stored in config system.
Drupal 7
To create view modes in Drupal 7, you had to implement hook_entity_info_alter()
and add to the view modes key for a given entity type. Contrib modules such as display suite and entity view modes did the same thing, with a UI for managing the process.
function MYMODULE_entity_info_alter(&$info) {
// Add a promo view mode for nodes.
$info['node']['view modes']['promo'] = array(
'label' => t('Promo'),
'custom settings' => FALSE,
);
}
Drupal 8
Default view modes can be stored in the /config folder of your module as yml
Example from /core/modules/block/custom_block/config/install/entity.view_mode.custom_block.full.yml
id: custom_block.full
label: Full
status: '0'
targetEntityType: custom_block
Creating a view mode programatically. As these are config entities, its just a simple entity_create
then save.
$view_mode = EntityViewMode::create(array(
'id' => 'mymodule.promo',
'label' => 'Promo',
'status' => 0,
'targetEntityType' => 'node'
));
$view_mode->save();
If you need to upgrade your view modes from Drupal 7, see system_update_8055().