field_collection.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. 'value' => array('value'),
  85. 'revision_id' => array('revision_id'),
  86. ),
  87. );
  88. }
  89. /**
  90. * Update the administer field collection permission machine name.
  91. */
  92. function field_collection_update_7000() {
  93. db_update('role_permission')
  94. ->fields(array('permission' => 'administer field collections'))
  95. ->condition('permission', 'administer field-collections')
  96. ->execute();
  97. }
  98. /**
  99. * Add revision support.
  100. */
  101. function field_collection_update_7001() {
  102. // Add revision_id column to field_collection_item table.
  103. $revision_id_spec = array(
  104. 'type' => 'int',
  105. 'not null' => TRUE,
  106. 'description' => 'Default revision ID.',
  107. // Set default to 0 temporarily.
  108. 'initial' => 0,
  109. );
  110. // Field may already exist due to bug in 7.x-1.0-beta5.
  111. if (!db_field_exists('field_collection_item', 'revision_id')) {
  112. db_add_field('field_collection_item', 'revision_id', $revision_id_spec);
  113. }
  114. // Initialize the revision_id to be the same as the item_id.
  115. db_update('field_collection_item')
  116. ->expression('revision_id', 'item_id')
  117. ->execute();
  118. // Add the archived column
  119. $archived_spec = array(
  120. 'description' => 'Boolean indicating whether the field collection item is archived.',
  121. 'type' => 'int',
  122. 'not null' => TRUE,
  123. 'default' => 0,
  124. );
  125. // Field may already exist due to bug in 7.x-1.0-beta5.
  126. if (!db_field_exists('field_collection_item', 'archived')) {
  127. db_add_field('field_collection_item', 'archived', $archived_spec);
  128. }
  129. // Create the new table. It is important to explicitly define the schema here
  130. // rather than use the hook_schema definition: http://drupal.org/node/150220.
  131. $schema['field_collection_item_revision'] = array(
  132. 'description' => 'Stores revision information about field collection items.',
  133. 'fields' => array(
  134. 'revision_id' => array(
  135. 'type' => 'serial',
  136. 'not null' => TRUE,
  137. 'description' => 'Primary Key: Unique revision ID.',
  138. ),
  139. 'item_id' => array(
  140. 'type' => 'int',
  141. 'not null' => TRUE,
  142. 'description' => 'Field collection item ID.',
  143. ),
  144. ),
  145. 'primary key' => array('revision_id'),
  146. 'indexes' => array(
  147. 'item_id' => array('item_id'),
  148. ),
  149. 'foreign keys' => array(
  150. 'versioned_field_collection_item' => array(
  151. 'table' => 'field_collection_item',
  152. 'columns' => array('item_id' => 'item_id'),
  153. ),
  154. ),
  155. );
  156. // Table may already exist due to bug in 7.x-1.0-beta5.
  157. if (db_table_exists('field_collection_item_revision')) {
  158. db_drop_table('field_collection_item_revision');
  159. }
  160. db_create_table('field_collection_item_revision', $schema['field_collection_item_revision']);
  161. // Fill the new table with the correct data.
  162. $items = db_select('field_collection_item', 'fci')
  163. ->fields('fci')
  164. ->execute();
  165. foreach ($items as $item) {
  166. // Update field_collection_item_revision table.
  167. db_insert('field_collection_item_revision')
  168. ->fields(array(
  169. 'revision_id' => $item->item_id,
  170. 'item_id' => $item->item_id,
  171. ))
  172. ->execute();
  173. }
  174. // Update the field_collection_field_schema columns for all tables.
  175. // Add a revision_id column.
  176. $revision_id_spec['description'] = 'The field collection item revision id.';
  177. // Because $value_column below can be null, so must $revision_id_column.
  178. $revision_id_spec['not null'] = FALSE;
  179. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  180. $table_prefixes = array('field_data', 'field_revision');
  181. foreach ($table_prefixes as $table_prefix) {
  182. $table = sprintf('%s_%s', $table_prefix, $field_name);
  183. $value_column = sprintf('%s_value', $field_name);
  184. $revision_id_column = sprintf('%s_revision_id', $field_name);
  185. // Field may already exist due to bug in 7.x-1.0-beta5.
  186. if (!db_field_exists($table, $revision_id_column)) {
  187. db_add_field($table, $revision_id_column, $revision_id_spec);
  188. }
  189. else {
  190. db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec);
  191. }
  192. // Initialize the revision_id to be the same as the item_id.
  193. db_update($table)
  194. ->expression($revision_id_column, $value_column)
  195. ->execute();
  196. }
  197. }
  198. // Need to get the system up-to-date so drupal_schema_fields_sql() will work.
  199. $schema = drupal_get_schema('field_collection_item_revision', TRUE);
  200. }
  201. /**
  202. * Remove orphaned field collection item entities.
  203. */
  204. function field_collection_update_7002() {
  205. // Loop over all fields and delete any orphaned field collection items.
  206. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  207. $select = db_select('field_collection_item', 'fci')
  208. ->fields('fci', array('item_id'))
  209. ->condition('field_name', $field_name)
  210. ->condition('archived', 0);
  211. $select->leftJoin('field_data_' . $field_name, 'field', "field.{$field_name}_value = fci.item_id ");
  212. $select->isNull('field.entity_id');
  213. $ids = $select->execute()->fetchCol(0);
  214. entity_delete_multiple('field_collection_item', $ids);
  215. drupal_set_message(t('Deleted @count orphaned field collection items.', array('@count' => count($ids))));
  216. }
  217. }
  218. /**
  219. * Update field_collection_field_schema columns for all tables.
  220. */
  221. function field_collection_update_7003() {
  222. // Revision_id column.
  223. $revision_id_spec = array(
  224. 'type' => 'int',
  225. 'not null' => FALSE,
  226. 'description' => 'The field collection item revision id.',
  227. 'initial' => 0,
  228. );
  229. // Update the field_collection_field_schema columns for all tables,
  230. // in case the buggy beta5 version of field_collection_update_7001()
  231. // completed without complaint.
  232. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  233. $table_prefixes = array('field_data', 'field_revision');
  234. foreach ($table_prefixes as $table_prefix) {
  235. $table = sprintf('%s_%s', $table_prefix, $field_name);
  236. $value_column = sprintf('%s_value', $field_name);
  237. $revision_id_column = sprintf('%s_revision_id', $field_name);
  238. db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec);
  239. }
  240. }
  241. // Need to get the system up-to-date so drupal_schema_fields_sql() will work.
  242. $schema = drupal_get_schema('field_collection_item_revision', TRUE);
  243. }
  244. /**
  245. * Add index on {$field_collection_field}_revision_id column for all tables.
  246. */
  247. function field_collection_update_7004() {
  248. // Update the field_collection_field_schema columns for all tables.
  249. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  250. $table_prefixes = array('field_data', 'field_revision');
  251. foreach ($table_prefixes as $table_prefix) {
  252. $table = sprintf('%s_%s', $table_prefix, $field_name);
  253. $revision_id_column = sprintf('%s_revision_id', $field_name);
  254. // Add index on revision_id column.
  255. if (!db_index_exists($table, $revision_id_column)) {
  256. db_add_index($table, $revision_id_column, array($revision_id_column));
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * Force the creation of the table cache_entity_field_collection_item.
  263. *
  264. * entity_update_7003 will attempt to install entitycache tables for existing
  265. * modules, but it uses module_list() to get the list of available modules,
  266. * which, when called from a database update, may not return field_collection
  267. * since drupal is bootstrapped at a lower level.
  268. */
  269. function field_collection_update_7005() {
  270. if (module_exists('entitycache')) {
  271. $entity_type = 'field_collection_item';
  272. $table = 'cache_entity_' . $entity_type;
  273. if (!db_table_exists($table)) {
  274. $schema = drupal_get_schema_unprocessed('system', 'cache');
  275. $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.';
  276. db_create_table($table, $schema);
  277. }
  278. }
  279. }
  280. /**
  281. * Ensures revision_id indexes are present at field_config table.
  282. */
  283. function field_collection_update_7006() {
  284. $result = db_query("SELECT id, field_name, data FROM {field_config} WHERE type = 'field_collection'");
  285. foreach ($result as $field_config) {
  286. $data = unserialize($field_config->data);
  287. // Skip this record if the revision_id index is already present.
  288. if (isset($data['indexes']['revision_id'])) {
  289. continue;
  290. }
  291. // Otherwise, add the revision_id index and update the record.
  292. $data['indexes']['revision_id'] = array('revision_id');
  293. $data = serialize($data);
  294. $num_updated = db_update('field_config')
  295. ->fields(array('data' => $data))
  296. ->condition('id', $field_config->id)
  297. ->execute();
  298. // If for some reason the update failed, throw an exception.
  299. if ($num_updated != 1) {
  300. $t_args['@field'] = $field_config->field_name;
  301. throw new DrupalUpdateException(t('An error was detected when attempting to update field configuration for field @field.', $t_args));
  302. }
  303. }
  304. }
  305. /**
  306. * Add index on {$field_collection_field}_value column for all tables.
  307. */
  308. function field_collection_update_7007() {
  309. foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
  310. if (!isset($field['indexes']['value'])) {
  311. // Add index on the value column and update the field.
  312. $field['indexes']['value'] = array('value');
  313. field_update_field($field);
  314. }
  315. $table_prefixes = array('field_data', 'field_revision');
  316. foreach ($table_prefixes as $table_prefix) {
  317. $table = "{$table_prefix}_{$field_name}";
  318. $value_column = "{$field_name}_value";
  319. if (!db_index_exists($table, $value_column)) {
  320. // Add index on the value column.
  321. db_add_index($table, $value_column, array($value_column));
  322. }
  323. }
  324. }
  325. }
  326. /**
  327. * Update fields in field collections already set to use Entity Translation.
  328. */
  329. function field_collection_update_7008() {
  330. // Include FieldCollectionItemEntity class.
  331. module_load_include('inc', 'field_collection', 'field_collection.entity');
  332. $results = array();
  333. foreach (field_info_fields() as $f_name => $field) {
  334. if ($field['translatable'] == 1 && isset($field['bundles']['field_collection_item'])) {
  335. $query = new EntityFieldQuery();
  336. $query->entityCondition('entity_type', 'field_collection_item')
  337. ->fieldLanguageCondition($f_name, LANGUAGE_NONE);
  338. $query_result = $query->execute();
  339. if (isset($query_result['field_collection_item'])) {
  340. $results = $results + $query_result['field_collection_item'];
  341. }
  342. }
  343. }
  344. if (count($results)) {
  345. $ids = array_keys($results);
  346. $field_collection_items = entity_load('field_collection_item', $ids);
  347. foreach ($field_collection_items as $item) {
  348. $item->copyTranslations(LANGUAGE_NONE);
  349. $item->save();
  350. }
  351. }
  352. }