drupal/core/modules/entity/entity.module

162 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.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer display modes'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
// 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/%'] = array(
'title' => 'Edit view mode',
'route_name' => 'entity_view_mode.edit',
);
$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/%'] = array(
'title' => 'Edit form mode',
'route_name' => 'entity_form_mode.edit',
);
$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);
}