entity.install 4.2 KB

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