Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By tim.plunkett on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
In Drupal 7, bundles and view modes were defined in hook_entity_info()
:
/**
* Implements hook_entity_info().
*/
function user_entity_info() {
$return = array(
'user' => array(
'label' => t('User'),
// ...
'bundles' => array(
'user' => array(
'label' => t('User'),
'admin' => array(
'path' => 'admin/config/people/accounts',
'access arguments' => array('administer users'),
),
),
),
'view modes' => array(
'full' => array(
'label' => t('User account'),
'custom settings' => FALSE,
),
),
),
);
return $return;
}
In Drupal 8, there is a hook for bundle information and view mode information is defined in config files, while other information is now in annotations (see change record Entity types are annotated plugins).
/**
* Implements hook_entity_bundle_info().
*/
function user_entity_bundle_info() {
$bundles['user']['user'] = array(
'label' => t('User'),
'admin' => array(
'path' => 'admin/config/people/accounts',
),
);
return $bundles;
}
Example of view mode in core/modules/user/config/install/entity.view_mode.user.full.yml
:
id: user.full
label: 'User account'
status: false
cache: true
targetEntityType: user
So, to access this data now you can use entity_get_bundles($entity_type_id)
and \Drupal::entityManager->getViewModes($entity_type_id)
respectively.
There are also corresponding alter hooks in Drupal 8:
/**
* Implements hook_entity_bundle_info_alter().
*/
function user_entity_bundle_info_alter(&$bundles) {
$bundles['user']['user']['label'] = t('Full account');
}
/**
* Implements hook_entity_view_mode_alter().
*/
function user_entity_view_mode_info_alter(&$view_modes) {
$view_modes['user']['full']['status'] = TRUE;
}
Impacts:
Module developers