166 lines
5.1 KiB
Plaintext
166 lines
5.1 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Manage the entity system.
|
|
*
|
|
* The module is mostly an anchor point for configuration items owned by the
|
|
* entity system.
|
|
*/
|
|
|
|
use Drupal\Core\Config\Entity\ConfigStorageController;
|
|
|
|
/**
|
|
* Implements hook_permission().
|
|
*/
|
|
function entity_permission() {
|
|
return array(
|
|
'administer display modes' => array(
|
|
'title' => t('Add, edit, and delete custom display modes.'),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_menu().
|
|
*/
|
|
function entity_menu() {
|
|
$items = array();
|
|
|
|
$items['admin/structure/display-modes'] = array(
|
|
'title' => 'Display modes',
|
|
'description' => 'Configure what displays are available for your content and forms.',
|
|
'route_name' => 'entity.display_mode',
|
|
);
|
|
|
|
// View modes.
|
|
$items['admin/structure/display-modes/view'] = array(
|
|
'title' => 'View modes',
|
|
'description' => 'Manage custom view modes.',
|
|
'route_name' => 'entity.view_mode_list',
|
|
);
|
|
$items['admin/structure/display-modes/view/list'] = array(
|
|
'title' => 'List',
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
);
|
|
$items['admin/structure/display-modes/view/add'] = array(
|
|
'title' => 'Add view mode',
|
|
'route_name' => 'entity.view_mode_add',
|
|
'type' => MENU_SIBLING_LOCAL_TASK,
|
|
);
|
|
$items['admin/structure/display-modes/view/add/%'] = array(
|
|
'route_name' => 'entity.view_mode_add_type',
|
|
);
|
|
$items['admin/structure/display-modes/view/manage/%/delete'] = array(
|
|
'route_name' => 'entity.view_mode_delete',
|
|
);
|
|
|
|
// Form modes.
|
|
$items['admin/structure/display-modes/form'] = array(
|
|
'title' => 'Form modes',
|
|
'description' => 'Manage custom form modes.',
|
|
'route_name' => 'entity.form_mode_list',
|
|
);
|
|
$items['admin/structure/display-modes/form/list'] = array(
|
|
'title' => 'List',
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
);
|
|
$items['admin/structure/display-modes/form/add'] = array(
|
|
'title' => 'Add form mode',
|
|
'route_name' => 'entity.form_mode_add',
|
|
'type' => MENU_SIBLING_LOCAL_TASK,
|
|
);
|
|
$items['admin/structure/display-modes/form/add/%'] = array(
|
|
'route_name' => 'entity.form_mode_add_type',
|
|
);
|
|
$items['admin/structure/display-modes/form/manage/%/delete'] = array(
|
|
'route_name' => 'entity.form_mode_delete',
|
|
);
|
|
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_local_actions().
|
|
*/
|
|
function entity_local_actions() {
|
|
return array(
|
|
array(
|
|
'route_name' => 'entity.view_mode_add',
|
|
'title' => t('Add view mode'),
|
|
'appears_on' => array(
|
|
'entity.view_mode_list',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_bundle_rename().
|
|
*/
|
|
function entity_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
|
|
// Rename entity displays.
|
|
$entity_info = entity_get_info('entity_display');
|
|
if ($bundle_old !== $bundle_new) {
|
|
$ids = config_get_storage_names_with_prefix('entity.display.' . $entity_type . '.' . $bundle_old);
|
|
foreach ($ids as $id) {
|
|
$id = ConfigStorageController::getIDFromConfigName($id, $entity_info['config_prefix']);
|
|
$display = entity_load('entity_display', $id);
|
|
$new_id = $entity_type . '.' . $bundle_new . '.' . $display->mode;
|
|
$display->id = $new_id;
|
|
$display->bundle = $bundle_new;
|
|
$display->save();
|
|
}
|
|
}
|
|
|
|
// Rename entity form displays.
|
|
$entity_info = entity_get_info('entity_form_display');
|
|
if ($bundle_old !== $bundle_new) {
|
|
$ids = config_get_storage_names_with_prefix('entity.form_display.' . $entity_type . '.' . $bundle_old);
|
|
foreach ($ids as $id) {
|
|
$id = ConfigStorageController::getIDFromConfigName($id, $entity_info['config_prefix']);
|
|
$form_display = entity_load('entity_form_display', $id);
|
|
$new_id = $entity_type . '.' . $bundle_new . '.' . $form_display->mode;
|
|
$form_display->id = $new_id;
|
|
$form_display->bundle = $bundle_new;
|
|
$form_display->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_bundle_delete().
|
|
*/
|
|
function entity_entity_bundle_delete($entity_type, $bundle) {
|
|
// Remove entity displays of the deleted bundle.
|
|
$entity_info = entity_get_info('entity_display');
|
|
$ids = config_get_storage_names_with_prefix('entity.display.' . $entity_type . '.' . $bundle);
|
|
foreach ($ids as &$id) {
|
|
$id = ConfigStorageController::getIDFromConfigName($id, $entity_info['config_prefix']);
|
|
}
|
|
entity_delete_multiple('entity_display', $ids);
|
|
|
|
// Remove entity form displays of the deleted bundle.
|
|
$entity_info = entity_get_info('entity_form_display');
|
|
$ids = config_get_storage_names_with_prefix('entity.form_display.' . $entity_type . '.' . $bundle);
|
|
foreach ($ids as &$id) {
|
|
$id = ConfigStorageController::getIDFromConfigName($id, $entity_info['config_prefix']);
|
|
}
|
|
entity_delete_multiple('entity_form_display', $ids);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_module_preuninstall().
|
|
*/
|
|
function entity_module_preuninstall($module) {
|
|
// Clean up all entity bundles (including field instances) of every entity
|
|
// type provided by the module that is being uninstalled.
|
|
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $entity_info) {
|
|
if ($entity_info['provider'] == $module) {
|
|
foreach (array_keys(entity_get_bundles($entity_type)) as $bundle) {
|
|
entity_invoke_bundle_hook('delete', $entity_type, $bundle);
|
|
}
|
|
}
|
|
}
|
|
}
|