entity.install 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Install file for the entity API.
  5. */
  6. /**
  7. * Implements hook_enable().
  8. */
  9. function entity_enable() {
  10. // Create cache tables for entities that support Entity cache module.
  11. entity_entitycache_installed_modules();
  12. }
  13. /**
  14. * Implements hook_uninstall().
  15. */
  16. function entity_uninstall() {
  17. // Delete variables.
  18. variable_del('entity_rebuild_on_flush');
  19. }
  20. /**
  21. * The entity API modules have been merged into a single module.
  22. */
  23. function entity_update_7000() {
  24. // This empty update is required such that all caches are cleared as
  25. // necessary.
  26. }
  27. /**
  28. * Remove the deprecated 'entity_defaults_built' variable.
  29. */
  30. function entity_update_7001() {
  31. variable_del('entity_defaults_built');
  32. }
  33. /**
  34. * Clear caches and rebuild registry.
  35. */
  36. function entity_update_7002() {
  37. // Do nothing, update.php clears cache for us in case there is an update.
  38. }
  39. /**
  40. * Create cache tables for entities that support Entity cache module.
  41. */
  42. function entity_update_7003() {
  43. entity_entitycache_installed_modules();
  44. }
  45. /**
  46. * Create cache tables for entities of modules that support Entity cache module.
  47. *
  48. * @param $modules
  49. * (optional) An array of module names that have been installed.
  50. * If not specified, try to add cache tables for all modules.
  51. */
  52. function entity_entitycache_installed_modules($modules = NULL) {
  53. if (!module_exists('entitycache')) {
  54. return;
  55. }
  56. // If no modules are specified or if entitycache is being installed,
  57. // try to add entitycache tables for supporting entities of all modules.
  58. if (!isset($modules) || in_array('entitycache', $modules)) {
  59. $modules = module_list();
  60. }
  61. // Get all installed modules that support entity cache.
  62. $entitycache_module_info = _entity_entitycache_get_module_info($modules);
  63. // For uninstallation of modules, we need to keep a list of tables we created
  64. // per module providing the entity type.
  65. $tables_created = variable_get('entity_cache_tables_created');
  66. foreach ($entitycache_module_info as $module => $module_entitycache_entities) {
  67. foreach ($module_entitycache_entities as $entity_type => $entity_info) {
  68. // Do not break modules that create the cache tables for themselves.
  69. if (!db_table_exists('cache_entity_' . $entity_type)) {
  70. $schema = drupal_get_schema_unprocessed('system', 'cache');
  71. $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.';
  72. db_create_table('cache_entity_' . $entity_type, $schema);
  73. $tables_created[$module][] = 'cache_entity_' . $entity_type;
  74. }
  75. }
  76. }
  77. variable_set('entity_cache_tables_created', $tables_created);
  78. }
  79. /**
  80. * Remove entity cache tables for entity types of uninstalled modules.
  81. *
  82. * @param $modules
  83. * (optional) An array of uninstalled modules. If not specified, try to remove
  84. * cache tables for all modules.
  85. */
  86. function entity_entitycache_uninstalled_modules($modules = NULL) {
  87. // If no modules are specified or if entitycache is being uninstalled,
  88. // try to remove entitycache tables for supporting entities of all modules.
  89. if (!isset($modules) || in_array('entitycache', $modules)) {
  90. $modules = module_list();
  91. }
  92. $tables_created = variable_get('entity_cache_tables_created');
  93. foreach ($modules as $module) {
  94. if (!empty($tables_created[$module])) {
  95. foreach ($tables_created[$module] as $table) {
  96. db_drop_table($table);
  97. }
  98. unset($tables_created[$module]);
  99. }
  100. }
  101. variable_set('entity_cache_tables_created', $tables_created);
  102. }
  103. /**
  104. * Helper to fetch entity info about entity types that use caching.
  105. */
  106. function _entity_entitycache_get_module_info($modules) {
  107. // Prepare a keyed array of all modules with their entity types and infos.
  108. // Structure: [module][entity][info]
  109. $entity_crud_info = entity_crud_get_info();
  110. $info = array();
  111. foreach ($entity_crud_info as $entity_name => $entity_info) {
  112. // Make sure that the entity info specifies a module and supports entitycache.
  113. if (!isset($entity_info['module']) || empty($entity_info['entity cache'])) {
  114. continue;
  115. }
  116. $module = $entity_info['module'];
  117. // Only treat installed modules.
  118. if (!in_array($module, $modules)) {
  119. continue;
  120. }
  121. // Add the entity info to the module key.
  122. if (!isset($info[$module])) {
  123. $info[$module] = array();
  124. }
  125. $info[$module][$entity_name] = $entity_info;
  126. }
  127. return $info;
  128. }