entityreference.install 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. foreach (entityreference_get_behavior_handlers($field) as $handler) {
  40. $handler->schema_alter($schema, $field);
  41. }
  42. return $schema;
  43. }
  44. }
  45. /**
  46. * Update the field configuration to the new plugin structure.
  47. */
  48. function entityreference_update_7000() {
  49. // Enable ctools.
  50. if (!module_enable(array('ctools'))) {
  51. throw new DrupalUpdateException('This version of Entity Reference requires ctools, but it could not be enabled.');
  52. }
  53. // Get the list of fields of type 'entityreference'.
  54. $fields = array();
  55. foreach (field_info_fields() as $field_name => $field) {
  56. // Update the field configuration.
  57. if ($field['type'] == 'entityreference') {
  58. $settings = &$field['settings'];
  59. if (!isset($settings['handler'])) {
  60. $settings['handler'] = 'base';
  61. $settings['handler_settings']['target_bundles'] = $settings['target_bundles'];
  62. unset($settings['target_bundles']);
  63. field_update_field($field);
  64. }
  65. }
  66. // Update the instance configurations.
  67. foreach ($field['bundles'] as $entity_type => $bundles) {
  68. foreach ($bundles as $bundle) {
  69. $instance = field_info_instance($entity_type, $field_name, $bundle);
  70. $save = FALSE;
  71. if ($instance['widget']['type'] == 'entityreference_autocomplete') {
  72. $instance['widget']['type'] = 'entityreference_autocomplete_tags';
  73. $save = TRUE;
  74. }
  75. // When the autocomplete path is the default value, remove it from
  76. // the configuration.
  77. if (isset($instance['widget']['settings']['path']) && $instance['widget']['settings']['path'] == 'entityreference/autocomplete') {
  78. unset($instance['widget']['settings']['path']);
  79. $save = TRUE;
  80. }
  81. if ($save) {
  82. field_update_instance($instance);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * Drop "target_type" from the field schema.
  90. */
  91. function entityreference_update_7001() {
  92. if (!module_exists('field_sql_storage')) {
  93. return;
  94. }
  95. foreach (field_info_fields() as $field_name => $field) {
  96. if ($field['type'] != 'entityreference') {
  97. // Not an entity reference field.
  98. continue;
  99. }
  100. // Update the field settings.
  101. $field = field_info_field($field_name);
  102. unset($field['indexes']['target_entity']);
  103. $field['indexes']['target_id'] = array('target_id');
  104. field_update_field($field);
  105. if ($field['storage']['type'] !== 'field_sql_storage') {
  106. // Field doesn't use SQL storage, we cannot modify the schema.
  107. continue;
  108. }
  109. $table_name = _field_sql_storage_tablename($field);
  110. $revision_name = _field_sql_storage_revision_tablename($field);
  111. db_drop_index($table_name, $field_name . '_target_entity');
  112. db_drop_index($table_name, $field_name . '_target_id');
  113. db_drop_field($table_name, $field_name . '_target_type');
  114. db_add_index($table_name, $field_name . '_target_id', array($field_name . '_target_id'));
  115. db_drop_index($revision_name, $field_name . '_target_entity');
  116. db_drop_index($revision_name, $field_name . '_target_id');
  117. db_drop_field($revision_name, $field_name . '_target_type');
  118. db_add_index($revision_name, $field_name . '_target_id', array($field_name . '_target_id'));
  119. }
  120. }
  121. /**
  122. * Make the target_id column NOT NULL.
  123. */
  124. function entityreference_update_7002() {
  125. if (!module_exists('field_sql_storage')) {
  126. return;
  127. }
  128. foreach (field_info_fields() as $field_name => $field) {
  129. if ($field['type'] != 'entityreference') {
  130. // Not an entity reference field.
  131. continue;
  132. }
  133. if ($field['storage']['type'] !== 'field_sql_storage') {
  134. // Field doesn't use SQL storage, we cannot modify the schema.
  135. continue;
  136. }
  137. $table_name = _field_sql_storage_tablename($field);
  138. $revision_name = _field_sql_storage_revision_tablename($field);
  139. db_change_field($table_name, $field_name . '_target_id', $field_name . '_target_id', array(
  140. 'description' => 'The id of the target entity.',
  141. 'type' => 'int',
  142. 'unsigned' => TRUE,
  143. 'not null' => TRUE,
  144. ));
  145. }
  146. }