entity.module

You are here

Manage the entity system.

The module is mostly an anchor point for configuration items owned by the entity system.

Functions

File

core/modules/entity/entity.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Manage the entity system.
  5. *
  6. * The module is mostly an anchor point for configuration items owned by the
  7. * entity system.
  8. */
  9. use Drupal\Core\Config\Entity\ConfigEntityStorage;
  10. /**
  11. * Implements hook_help().
  12. */
  13. function entity_help($path, $arg) {
  14. switch ($path) {
  15. case 'admin/help#entity':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Entity module manages various types of content and configuration for the website. This information is collectively know as "entities", which are grouped into "entity types" (such as the main site content, comments, custom blocks, taxonomy terms, user accounts, and views configuration). Some entity types are further grouped into sub-types (for example, you could have article and page content types within the main site content entity type, and tag and category vocabularies within the taxonomy term entity type); other entity types, such as user accounts, do not have sub-types.') . '</p>';
  19. $output .= '<p>' . t('Content entity types store most of their text, file, and other information in fields. See the <a href="!field">Field module help</a> and the <a href="!field_ui">Field UI help</a> pages for general information on fields and how to create and manage them.', array('!field' => \Drupal::url('help.page', array('name' => 'field')), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</p>';
  20. $output .= '<p>' . t('Configuration entity types are used to store configuration information for your site, such as individual views in the Views module, and settings for your main site content types. Configuration stored in this way can be exported, imported, and managed using the Configuration Manager module. See the <a href="!config-help">Configuration Manager module help</a> page for more information.', array('!config-help' => \Drupal::url('help.page', array('name' => 'config')))) . '</p>';
  21. $output .= '<p>' . t('For more information, see the <a href="!entity_documentation">online documentation for the Entity module</a>.', array('!entity_documentation' => 'https://drupal.org/documentation/modules/entity')) . '</p>';
  22. $output .= '<h3>' . t('Uses') . '</h3>';
  23. $output .= '<dl>';
  24. $output .= '<dt>' . t('Managing view modes') . '</dt>';
  25. $output .= '<dd>' . t('Each content entity can have various "modes" for viewing. For instance, a content item could be viewed in full content mode on its own page, teaser mode in a list, or RSS mode in a feed. You can create, edit the names of, and delete view modes on the <a href="!view-modes">View modes page</a>. Once a view mode has been set up, you can choose and format fields for the view mode within each entity sub-type on the Manage display page. See the <a href="!field_ui">Field UI module help page</a> for more information.', array('!view-modes' => \Drupal::url('entity.view_mode_list'), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
  26. $output .= '<dt>' . t('Managing form modes') . '</dt>';
  27. $output .= '<dd>' . t('Each content entity can have various editing forms appropriate for different situations, which are known as "form modes". For instance, you might want to define a quick editing mode that allows users to edit the most important fields, and a full editing mode that gives access to all the fields. You can create, edit the names of, and delete form modes on the <a href="!form-modes">Manage custom form modes page</a>. Once a form mode has been set up, you can choose which fields are available on that form within each entity sub-type on the Manage form display page. See the <a href="!field_ui">Field UI module help page</a> for more information.', array('!form-modes' => \Drupal::url('entity.form_mode_list'), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
  28. $output .= '</dl>';
  29. return $output;
  30. }
  31. }
  32. /**
  33. * Implements hook_permission().
  34. */
  35. function entity_permission() {
  36. return array(
  37. 'administer display modes' => array(
  38. 'title' => t('Add, edit, and delete custom display modes.'),
  39. ),
  40. );
  41. }
  42. /**
  43. * Implements hook_entity_bundle_rename().
  44. */
  45. function entity_entity_bundle_rename($entity_type_id, $bundle_old, $bundle_new) {
  46. // Rename entity displays.
  47. $entity_type = \Drupal::entityManager()->getDefinition('entity_view_display');
  48. if ($bundle_old !== $bundle_new) {
  49. $ids = \Drupal::configFactory()->listAll('entity.view_display.' . $entity_type_id . '.' . $bundle_old . '.');
  50. foreach ($ids as $id) {
  51. $id = ConfigEntityStorage::getIDFromConfigName($id, $entity_type->getConfigPrefix());
  52. $display = entity_load('entity_view_display', $id);
  53. $new_id = $entity_type_id . '.' . $bundle_new . '.' . $display->mode;
  54. $display->set('id', $new_id);
  55. $display->bundle = $bundle_new;
  56. $display->save();
  57. }
  58. }
  59. // Rename entity form displays.
  60. $entity_type = \Drupal::entityManager()->getDefinition('entity_form_display');
  61. if ($bundle_old !== $bundle_new) {
  62. $ids = \Drupal::configFactory()->listAll('entity.form_display.' . $entity_type_id . '.' . $bundle_old . '.');
  63. foreach ($ids as $id) {
  64. $id = ConfigEntityStorage::getIDFromConfigName($id, $entity_type->getConfigPrefix());
  65. $form_display = entity_load('entity_form_display', $id);
  66. $new_id = $entity_type_id . '.' . $bundle_new . '.' . $form_display->mode;
  67. $form_display->set('id', $new_id);
  68. $form_display->bundle = $bundle_new;
  69. $form_display->save();
  70. }
  71. }
  72. }
  73. /**
  74. * Implements hook_entity_bundle_delete().
  75. */
  76. function entity_entity_bundle_delete($entity_type_id, $bundle) {
  77. // Remove entity displays of the deleted bundle.
  78. $entity_type = \Drupal::entityManager()->getDefinition('entity_view_display');
  79. $ids = \Drupal::configFactory()->listAll('entity.view_display.' . $entity_type_id . '.' . $bundle . '.');
  80. foreach ($ids as &$id) {
  81. $id = ConfigEntityStorage::getIDFromConfigName($id, $entity_type->getConfigPrefix());
  82. }
  83. entity_delete_multiple('entity_view_display', $ids);
  84. // Remove entity form displays of the deleted bundle.
  85. $entity_type = \Drupal::entityManager()->getDefinition('entity_form_display');
  86. $ids = \Drupal::configFactory()->listAll('entity.form_display.' . $entity_type_id . '.' . $bundle . '.');
  87. foreach ($ids as &$id) {
  88. $id = ConfigEntityStorage::getIDFromConfigName($id, $entity_type->getConfigPrefix());
  89. }
  90. entity_delete_multiple('entity_form_display', $ids);
  91. }
  92. /**
  93. * Implements hook_module_preuninstall().
  94. */
  95. function entity_module_preuninstall($module) {
  96. // Clean up all entity bundles (including field instances) of every entity
  97. // type provided by the module that is being uninstalled.
  98. foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
  99. if ($entity_type->getProvider() == $module) {
  100. foreach (array_keys(entity_get_bundles($entity_type_id)) as $bundle) {
  101. entity_invoke_bundle_hook('delete', $entity_type_id, $bundle);
  102. }
  103. }
  104. }
  105. }