Projet

Général

Profil

Paste
Télécharger (4,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / entity.install @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Install file for the entity API.
6
 */
7

    
8
/**
9
 * Implements hook_enable().
10
 */
11
function entity_enable() {
12
  // Create cache tables for entities that support Entity cache module.
13
  entity_entitycache_installed_modules();
14
}
15

    
16
/**
17
 * The entity API modules have been merged into a single module.
18
 */
19
function entity_update_7000() {
20
  // This empty update is required such that all caches are cleared as
21
  // necessary.
22
}
23

    
24
/**
25
 * Remove the deprecated 'entity_defaults_built' variable.
26
 */
27
function entity_update_7001() {
28
  variable_del('entity_defaults_built');
29
}
30

    
31
/**
32
 * Clear caches and rebuild registry.
33
 */
34
function entity_update_7002() {
35
  // Do nothing, update.php clears cache for us in case there is an update.
36
}
37

    
38
/**
39
 * Create cache tables for entities that support Entity cache module.
40
 */
41
function entity_update_7003() {
42
  entity_entitycache_installed_modules();
43
}
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

    
57
  // If no modules are specified or if entitycache is being installed,
58
  // try to add entitycache tables for supporting entities of all modules.
59
  if (!isset($modules) || in_array('entitycache', $modules)) {
60
    $modules = module_list();
61
  }
62

    
63
  // Get all installed modules that support entity cache.
64
  $entitycache_module_info = _entity_entitycache_get_module_info($modules);
65

    
66
  // For uninstallation of modules, we need to keep a list of tables we created
67
  // per module providing the entity type.
68
  $tables_created = variable_get('entity_cache_tables_created');
69

    
70
  foreach ($entitycache_module_info as $module => $module_entitycache_entities) {
71
    foreach ($module_entitycache_entities as $entity_type => $entity_info) {
72
      // Do not break modules that create the cache tables for themselves.
73
      if (!db_table_exists('cache_entity_' . $entity_type)) {
74
        $schema = drupal_get_schema_unprocessed('system', 'cache');
75
        $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.';
76
        db_create_table('cache_entity_' . $entity_type, $schema);
77
        $tables_created[$module][] = 'cache_entity_' . $entity_type;
78
      }
79
    }
80
  }
81
  variable_set('entity_cache_tables_created', $tables_created);
82
}
83

    
84
/**
85
 * Remove entity cache tables for entity types of uninstalled modules.
86
 *
87
 * @param $modules
88
 *   (optional) An array of uninstalled modules. If not specified, try to remove
89
 *   cache tables for all modules.
90
 */
91
function entity_entitycache_uninstalled_modules($modules = NULL) {
92
  // If no modules are specified or if entitycache is being uninstalled,
93
  // try to remove entitycache tables for supporting entities of all modules.
94
  if (!isset($modules) || in_array('entitycache', $modules)) {
95
    $modules = module_list();
96
  }
97
  $tables_created = variable_get('entity_cache_tables_created');
98
  foreach ($modules as $module) {
99
    if (!empty($tables_created[$module])) {
100
      foreach ($tables_created[$module] as $table) {
101
        db_drop_table($table);
102
      }
103
      unset($tables_created[$module]);
104
    }
105
  }
106
  variable_set('entity_cache_tables_created', $tables_created);
107
}
108

    
109
/**
110
 * Helper to fetch entity info about entity types that use caching.
111
 */
112
function _entity_entitycache_get_module_info($modules) {
113
  // Prepare a keyed array of all modules with their entity types and infos.
114
  // Structure: [module][entity][info]
115
  $entity_crud_info = entity_crud_get_info();
116
  $info = array();
117
  foreach ($entity_crud_info as $entity_name => $entity_info) {
118
    // Make sure that the entity info specifies a module and supports entitycache.
119
    if (!isset($entity_info['module']) || empty($entity_info['entity cache'])) {
120
      continue;
121
    }
122
    $module = $entity_info['module'];
123
    // Only treat installed modules.
124
    if (!in_array($module, $modules)) {
125
      continue;
126
    }
127
    // Add the entity info to the module key.
128
    if (!isset($info[$module])) {
129
      $info[$module] = array();
130
    }
131
    $info[$module][$entity_name] = $entity_info;
132
  }
133
  return $info;
134
}