Projet

Général

Profil

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

root / drupal7 / sites / all / modules / entity / entity.install @ 3aa14731

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
 * Implements hook_uninstall().
18
 */
19
function entity_uninstall() {
20
  // Delete variables.
21
  variable_del('entity_rebuild_on_flush');
22
}
23

    
24
/**
25
 * The entity API modules have been merged into a single module.
26
 */
27
function entity_update_7000() {
28
  // This empty update is required such that all caches are cleared as
29
  // necessary.
30
}
31

    
32
/**
33
 * Remove the deprecated 'entity_defaults_built' variable.
34
 */
35
function entity_update_7001() {
36
  variable_del('entity_defaults_built');
37
}
38

    
39
/**
40
 * Clear caches and rebuild registry.
41
 */
42
function entity_update_7002() {
43
  // Do nothing, update.php clears cache for us in case there is an update.
44
}
45

    
46
/**
47
 * Create cache tables for entities that support Entity cache module.
48
 */
49
function entity_update_7003() {
50
  entity_entitycache_installed_modules();
51
}
52

    
53
/**
54
 * Create cache tables for entities of modules that support Entity cache module.
55
 *
56
 * @param $modules
57
 *   (optional) An array of module names that have been installed.
58
 *   If not specified, try to add cache tables for all modules.
59
 */
60
function entity_entitycache_installed_modules($modules = NULL) {
61
  if (!module_exists('entitycache')) {
62
    return;
63
  }
64

    
65
  // If no modules are specified or if entitycache is being installed,
66
  // try to add entitycache tables for supporting entities of all modules.
67
  if (!isset($modules) || in_array('entitycache', $modules)) {
68
    $modules = module_list();
69
  }
70

    
71
  // Get all installed modules that support entity cache.
72
  $entitycache_module_info = _entity_entitycache_get_module_info($modules);
73

    
74
  // For uninstallation of modules, we need to keep a list of tables we created
75
  // per module providing the entity type.
76
  $tables_created = variable_get('entity_cache_tables_created');
77

    
78
  foreach ($entitycache_module_info as $module => $module_entitycache_entities) {
79
    foreach ($module_entitycache_entities as $entity_type => $entity_info) {
80
      // Do not break modules that create the cache tables for themselves.
81
      if (!db_table_exists('cache_entity_' . $entity_type)) {
82
        $schema = drupal_get_schema_unprocessed('system', 'cache');
83
        $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.';
84
        db_create_table('cache_entity_' . $entity_type, $schema);
85
        $tables_created[$module][] = 'cache_entity_' . $entity_type;
86
      }
87
    }
88
  }
89
  variable_set('entity_cache_tables_created', $tables_created);
90
}
91

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

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