field_collection.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the field_collection module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function field_collection_schema() {
  10. $schema['field_collection_item'] = array(
  11. 'description' => 'Stores information about field collection items.',
  12. 'fields' => array(
  13. 'item_id' => array(
  14. 'type' => 'serial',
  15. 'not null' => TRUE,
  16. 'description' => 'Primary Key: Unique field collection item ID.',
  17. ),
  18. 'revision_id' => array(
  19. 'type' => 'int',
  20. 'not null' => TRUE,
  21. 'description' => 'Default revision ID.',
  22. ),
  23. 'field_name' => array(
  24. 'description' => 'The name of the field on the host entity embedding this entity.',
  25. 'type' => 'varchar',
  26. 'length' => 32,
  27. 'not null' => TRUE,
  28. ),
  29. 'archived' => array(
  30. 'description' => 'Boolean indicating whether the field collection item is archived.',
  31. 'type' => 'int',
  32. 'not null' => TRUE,
  33. 'default' => 0,
  34. ),
  35. ),
  36. 'primary key' => array('item_id'),
  37. );
  38. $schema['field_collection_item_revision'] = array(
  39. 'description' => 'Stores revision information about field collection items.',
  40. 'fields' => array(
  41. 'revision_id' => array(
  42. 'type' => 'serial',
  43. 'not null' => TRUE,
  44. 'description' => 'Primary Key: Unique revision ID.',
  45. ),
  46. 'item_id' => array(
  47. 'type' => 'int',
  48. 'not null' => TRUE,
  49. 'description' => 'Field collection item ID.',
  50. ),
  51. ),
  52. 'primary key' => array('revision_id'),
  53. 'indexes' => array(
  54. 'item_id' => array('item_id'),
  55. ),
  56. 'foreign keys' => array(
  57. 'versioned_field_collection_item' => array(
  58. 'table' => 'field_collection_item',
  59. 'columns' => array('item_id' => 'item_id'),
  60. ),
  61. ),
  62. );
  63. return $schema;
  64. }
  65. /**
  66. * Implements hook_field_schema().
  67. */
  68. function field_collection_field_schema($field) {
  69. $columns = array(
  70. 'value' => array(
  71. 'type' => 'int',
  72. 'not null' => FALSE,
  73. 'description' => 'The field collection item id.',
  74. ),
  75. 'revision_id' => array(
  76. 'type' => 'int',
  77. 'not null' => FALSE,
  78. 'description' => 'The field collection item revision id.',
  79. ),
  80. );
  81. return array(
  82. 'columns' => $columns,
  83. 'indexes' => array(
  84. 'revision_id' => array('revision_id'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Update the administer field collection permission machine name.
  90. */
  91. function field_collection_update_7000() {
  92. db_update('role_permission')
  93. ->fields(array('permission' => 'administer field collections'))
  94. ->condition('permission', 'administer field-collections')
  95. ->execute();
  96. }
  97. /**
  98. * Add revision support.
  99. */
  100. function field_collection_update_7001() {
  101. // Add revision_id column to field_collection_item table.
  102. $revision_id_spec = array(
  103. 'type' => 'int',
  104. 'not null' => TRUE,
  105. 'description' => 'Default revision ID.',
  106. // Set default to 0 temporarily.
  107. 'initial' => 0,
  108. );
  109. // Field may already exist due to bug in 7.x-1.0-beta5.
  110. if (!db_field_exists('field_collection_item', 'revision_id')) {
  111. db_add_field('field_collection_item', 'revision_id', $revision_id_spec);
  112. }
  113. // Initialize the revision_id to be the same as the item_id.
  114. db_update('field_collection_item')
  115. ->expression('revision_id', 'item_id')
  116. ->execute();
  117. // Add the archived column
  118. $archived_spec = array(
  119. 'description' => 'Boolean indicating whether the field collection item is archived.',
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. 'default' => 0,
  123. );
  124. // Field may already exist due to bug in 7.x-1.0-beta5.
  125. if (!db_field_exists('field_collection_item', 'archived')) {
  126. db_add_field('field_collection_item', 'archived', $archived_spec);
  127. }
  128. // Create the new table. It is important to explicitly define the schema here
  129. // rather than use the hook_schema definition: http://drupal.org/node/150220.
  130. $schema['field_collection_item_revision'] = array(
  131. 'description' => 'Stores revision information about field collection items.',
  132. 'fields' => array(
  133. 'revision_id' => array(
  134. 'type' => 'serial',
  135. 'not null' => TRUE,
  136. 'description' => 'Primary Key: Unique revision ID.',
  137. ),
  138. 'item_id' => array(
  139. 'type' => 'int',
  140. 'not null' => TRUE,
  141. 'description' => 'Field collection item ID.',
  142. ),
  143. ),
  144. 'primary key' => array('revision_id'),
  145. 'indexes' => array(
  146. 'item_id' => array('item_id'),
  147. ),
  148. 'foreign keys' => array(
  149. 'versioned_field_collection_item' => array(
  150. 'table' => 'field_collection_item',
  151. 'columns' => array('item_id' => 'item_id'),
  152. ),
  153. ),
  154. );
  155. // Table may already exist due to bug in 7.x-1.0-beta5.
  156. if (db_table_exists('field_collection_item_revision')) {
  157. db_drop_table('field_collection_item_revision');
  158. }
  159. db_create_table('field_collection_item_revision', $schema['field_collection_item_revision']);
  160. // Fill the new table with the correct data.
  161. $items = db_select('field_collection_item', 'fci')
  162. ->fields('fci')
  163. ->execute();
  164. foreach ($items as $item) {
  165. // Update field_collection_item_revision table.
  166. db_insert('field_collection_item_revision')
  167. ->fields(array(
  168. 'revision_id' => $item->item_id,
  169. 'item_id' => $item->item_id,
  170. ))
  171. ->execute();
  172. }
  173. // Update the field_collection_field_schema columns for all tables.
  174. // Add a revision_id column.
  175. $revision_id_spec['description'] = 'The field collection item revision id.';
  176. // Because $value_column below can be null, so must $revision_id_column.
  177. $revision_id_spec['not null'] = FALSE;
  178. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  179. $table_prefixes = array('field_data', 'field_revision');
  180. foreach ($table_prefixes as $table_prefix) {
  181. $table = sprintf('%s_%s', $table_prefix, $field_name);
  182. $value_column = sprintf('%s_value', $field_name);
  183. $revision_id_column = sprintf('%s_revision_id', $field_name);
  184. // Field may already exist due to bug in 7.x-1.0-beta5.
  185. if (!db_field_exists($table, $revision_id_column)) {
  186. db_add_field($table, $revision_id_column, $revision_id_spec);
  187. }
  188. else {
  189. db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec);
  190. }
  191. // Initialize the revision_id to be the same as the item_id.
  192. db_update($table)
  193. ->expression($revision_id_column, $value_column)
  194. ->execute();
  195. }
  196. }
  197. // Need to get the system up-to-date so drupal_schema_fields_sql() will work.
  198. $schema = drupal_get_schema('field_collection_item_revision', TRUE);
  199. }
  200. /**
  201. * Remove orphaned field collection item entities.
  202. */
  203. function field_collection_update_7002() {
  204. // Loop over all fields and delete any orphaned field collection items.
  205. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  206. $select = db_select('field_collection_item', 'fci')
  207. ->fields('fci', array('item_id'))
  208. ->condition('field_name', $field_name)
  209. ->condition('archived', 0);
  210. $select->leftJoin('field_data_' . $field_name, 'field', "field.{$field_name}_value = fci.item_id ");
  211. $select->isNull('field.entity_id');
  212. $ids = $select->execute()->fetchCol(0);
  213. entity_delete_multiple('field_collection_item', $ids);
  214. drupal_set_message(t('Deleted @count orphaned field collection items.', array('@count' => count($ids))));
  215. }
  216. }
  217. /**
  218. * Update field_collection_field_schema columns for all tables.
  219. */
  220. function field_collection_update_7003() {
  221. // Revision_id column.
  222. $revision_id_spec = array(
  223. 'type' => 'int',
  224. 'not null' => FALSE,
  225. 'description' => 'The field collection item revision id.',
  226. 'initial' => 0,
  227. );
  228. // Update the field_collection_field_schema columns for all tables,
  229. // in case the buggy beta5 version of field_collection_update_7001()
  230. // completed without complaint.
  231. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  232. $table_prefixes = array('field_data', 'field_revision');
  233. foreach ($table_prefixes as $table_prefix) {
  234. $table = sprintf('%s_%s', $table_prefix, $field_name);
  235. $value_column = sprintf('%s_value', $field_name);
  236. $revision_id_column = sprintf('%s_revision_id', $field_name);
  237. db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec);
  238. }
  239. }
  240. // Need to get the system up-to-date so drupal_schema_fields_sql() will work.
  241. $schema = drupal_get_schema('field_collection_item_revision', TRUE);
  242. }
  243. /**
  244. * Add index on {$field_collection_field}_revision_id column for all tables.
  245. */
  246. function field_collection_update_7004() {
  247. // Update the field_collection_field_schema columns for all tables.
  248. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  249. $table_prefixes = array('field_data', 'field_revision');
  250. foreach ($table_prefixes as $table_prefix) {
  251. $table = sprintf('%s_%s', $table_prefix, $field_name);
  252. $revision_id_column = sprintf('%s_revision_id', $field_name);
  253. // Add index on revision_id column.
  254. if (!db_index_exists($table, $revision_id_column)) {
  255. db_add_index($table, $revision_id_column, array($revision_id_column));
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * Force the creation of the table cache_entity_field_collection_item.
  262. *
  263. * entity_update_7003 will attempt to install entitycache tables for existing
  264. * modules, but it uses module_list() to get the list of available modules,
  265. * which, when called from a database update, may not return field_collection
  266. * since drupal is bootstrapped at a lower level.
  267. */
  268. function field_collection_update_7005() {
  269. if (module_exists('entitycache')) {
  270. $entity_type = 'field_collection_item';
  271. $table = 'cache_entity_' . $entity_type;
  272. if (!db_table_exists($table)) {
  273. $schema = drupal_get_schema_unprocessed('system', 'cache');
  274. $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.';
  275. db_create_table($table, $schema);
  276. }
  277. }
  278. }
  279. /**
  280. * Ensures revision_id indexes are present at field_config table.
  281. */
  282. function field_collection_update_7006() {
  283. $result = db_query("SELECT id, field_name, data FROM {field_config} WHERE type = 'field_collection'");
  284. foreach ($result as $field_config) {
  285. $data = unserialize($field_config->data);
  286. // Skip this record if the revision_id index is already present.
  287. if (isset($data['indexes']['revision_id'])) {
  288. continue;
  289. }
  290. // Otherwise, add the revision_id index and update the record.
  291. $data['indexes']['revision_id'] = array('revision_id');
  292. $data = serialize($data);
  293. $num_updated = db_update('field_config')
  294. ->fields(array('data' => $data))
  295. ->condition('id', $field_config->id)
  296. ->execute();
  297. // If for some reason the update failed, throw an exception.
  298. if ($num_updated != 1) {
  299. $t_args['@field'] = $field_config->field_name;
  300. throw new DrupalUpdateException(t('An error was detected when attempting to update field configuration for field @field.', $t_args));
  301. }
  302. }
  303. }