entityreference.install 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Implements hook_uninstall().
  4. */
  5. function entityreference_uninstall() {
  6. variable_del('entityreference:base-tables');
  7. }
  8. /**
  9. * Implements hook_field_schema().
  10. */
  11. function entityreference_field_schema($field) {
  12. if ($field['type'] == 'entityreference') {
  13. // Load the base table configuration from the cache.
  14. $base_tables = variable_get('entityreference:base-tables', array());
  15. $schema = array(
  16. 'columns' => array(
  17. 'target_id' => array(
  18. 'description' => 'The id of the target entity.',
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => TRUE,
  22. ),
  23. ),
  24. 'indexes' => array(
  25. 'target_id' => array('target_id'),
  26. ),
  27. 'foreign keys' => array(),
  28. );
  29. // Create a foreign key to the target entity type base type, if available.
  30. $entity_type = $field['settings']['target_type'];
  31. if (isset($base_tables[$entity_type])) {
  32. list($base_table, $id_column) = $base_tables[$entity_type];
  33. $schema['foreign keys'][$base_table] = array(
  34. 'table' => $base_table,
  35. 'columns' => array('target_id' => $id_column),
  36. );
  37. }
  38. // Invoke the behaviors to allow them to change the schema.
  39. module_load_include('module', 'entityreference');
  40. foreach (entityreference_get_behavior_handlers($field) as $handler) {
  41. $handler->schema_alter($schema, $field);
  42. }
  43. return $schema;
  44. }
  45. }
  46. /**
  47. * Update the field configuration to the new plugin structure.
  48. */
  49. function entityreference_update_7000() {
  50. // Enable ctools.
  51. if (!module_enable(array('ctools'))) {
  52. throw new DrupalUpdateException('This version of Entity Reference requires ctools, but it could not be enabled.');
  53. }
  54. // Get the list of fields of type 'entityreference'.
  55. $fields = array();
  56. foreach (field_info_fields() as $field_name => $field) {
  57. // Update the field configuration.
  58. if ($field['type'] == 'entityreference') {
  59. $settings = &$field['settings'];
  60. if (!isset($settings['handler'])) {
  61. $settings['handler'] = 'base';
  62. $settings['handler_settings']['target_bundles'] = $settings['target_bundles'];
  63. unset($settings['target_bundles']);
  64. field_update_field($field);
  65. }
  66. }
  67. // Update the instance configurations.
  68. foreach ($field['bundles'] as $entity_type => $bundles) {
  69. foreach ($bundles as $bundle) {
  70. $instance = field_info_instance($entity_type, $field_name, $bundle);
  71. $save = FALSE;
  72. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  73. $instance['widget']['type'] = 'entityreference_autocomplete_tags';
  74. $save = TRUE;
  75. }
  76. // When the autocomplete path is the default value, remove it from
  77. // the configuration.
  78. if (isset($instance['widget']['settings']['path']) && $instance['widget']['settings']['path'] == 'entityreference/autocomplete') {
  79. unset($instance['widget']['settings']['path']);
  80. $save = TRUE;
  81. }
  82. if ($save) {
  83. field_update_instance($instance);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * Drop "target_type" from the field schema.
  91. */
  92. function entityreference_update_7001() {
  93. if (!module_exists('field_sql_storage')) {
  94. return;
  95. }
  96. foreach (field_info_fields() as $field_name => $field) {
  97. if ($field['type'] != 'entityreference') {
  98. // Not an entity reference field.
  99. continue;
  100. }
  101. // Update the field settings.
  102. $field = field_info_field($field_name);
  103. unset($field['indexes']['target_entity']);
  104. $field['indexes']['target_id'] = array('target_id');
  105. field_update_field($field);
  106. if ($field['storage']['type'] !== 'field_sql_storage') {
  107. // Field doesn't use SQL storage, we cannot modify the schema.
  108. continue;
  109. }
  110. $table_name = _field_sql_storage_tablename($field);
  111. $revision_name = _field_sql_storage_revision_tablename($field);
  112. db_drop_index($table_name, $field_name . '_target_entity');
  113. db_drop_index($table_name, $field_name . '_target_id');
  114. db_drop_field($table_name, $field_name . '_target_type');
  115. db_add_index($table_name, $field_name . '_target_id', array($field_name . '_target_id'));
  116. db_drop_index($revision_name, $field_name . '_target_entity');
  117. db_drop_index($revision_name, $field_name . '_target_id');
  118. db_drop_field($revision_name, $field_name . '_target_type');
  119. db_add_index($revision_name, $field_name . '_target_id', array($field_name . '_target_id'));
  120. }
  121. }
  122. /**
  123. * Make the target_id column NOT NULL.
  124. */
  125. function entityreference_update_7002() {
  126. if (!module_exists('field_sql_storage')) {
  127. return;
  128. }
  129. foreach (field_info_fields() as $field_name => $field) {
  130. if ($field['type'] != 'entityreference') {
  131. // Not an entity reference field.
  132. continue;
  133. }
  134. if ($field['storage']['type'] !== 'field_sql_storage') {
  135. // Field doesn't use SQL storage, we cannot modify the schema.
  136. continue;
  137. }
  138. $table_name = _field_sql_storage_tablename($field);
  139. $revision_name = _field_sql_storage_revision_tablename($field);
  140. db_change_field($table_name, $field_name . '_target_id', $field_name . '_target_id', array(
  141. 'description' => 'The id of the target entity.',
  142. 'type' => 'int',
  143. 'unsigned' => TRUE,
  144. 'not null' => TRUE,
  145. ));
  146. }
  147. }
  148. /**
  149. * Implements hook_update_N().
  150. *
  151. * Remove duplicate rows in the taxonomy_index table.
  152. */
  153. function entityreference_update_7100() {
  154. if (db_table_exists('taxonomy_index')) {
  155. if (db_table_exists('taxonomy_index_tmp')) {
  156. db_drop_table('taxonomy_index_tmp');
  157. }
  158. $tx_schema = drupal_get_schema('taxonomy_index');
  159. db_create_table('taxonomy_index_tmp', $tx_schema);
  160. $select = db_select('taxonomy_index', 'tx');
  161. $select->fields('tx', array('nid', 'tid'));
  162. $select->groupBy('tx.nid');
  163. $select->groupBy('tx.tid');
  164. $select->addExpression('MAX(sticky)', 'sticky');
  165. $select->addExpression('MAX(created)', 'created');
  166. db_insert('taxonomy_index_tmp')->from($select)->execute();
  167. db_drop_table('taxonomy_index');
  168. db_rename_table('taxonomy_index_tmp', 'taxonomy_index');
  169. }
  170. }