field_sql_storage.install 4.38 KB
Newer Older
Dries's avatar
   
Dries committed
1
2
<?php

3
4
/**
 * @file
5
 * Install, update, and uninstall functions for the Field SQL Storage module.
6
7
 */

8
9
use Drupal\field\Plugin\Core\Entity\Field;

10
/**
11
 * Writes field data directly to SQL storage.
12
 *
13
 * @ingroup update_api
14
 */
15
function _update_8000_field_sql_storage_write($entity_type, $bundle, $entity_id, $revision_id, $field_name, $data) {
16
17
18
19
  $table_name = "field_data_{$field_name}";
  $revision_name = "field_revision_{$field_name}";

  db_delete($table_name)
20
    ->condition('entity_type', $entity_type)
21
22
23
    ->condition('entity_id', $entity_id)
    ->execute();
  db_delete($revision_name)
24
    ->condition('entity_type', $entity_type)
25
26
27
28
29
30
31
32
    ->condition('entity_id', $entity_id)
    ->condition('revision_id', $revision_id)
    ->execute();

  $columns = array();
  foreach ($data as $langcode => $items) {
    foreach ($items as $delta => $item) {
      $record = array(
33
        'entity_type' => $entity_type,
34
35
36
37
        'entity_id' => $entity_id,
        'revision_id' => $revision_id,
        'bundle' => $bundle,
        'delta' => $delta,
38
        'langcode' => $langcode,
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      );
      foreach ($item as $column => $value) {
        $record[_field_sql_storage_columnname($field_name, $column)] = $value;
      }

      $records[] = $record;
      // Record the columns used.
      $columns += $record;
    }
  }

  if ($columns) {
    $query = db_insert($table_name)->fields(array_keys($columns));
    $revision_query = db_insert($revision_name)->fields(array_keys($columns));
    foreach ($records as $record) {
      $query->values($record);
      if ($revision_id) {
        $revision_query->values($record);
      }
    }
    $query->execute();
    $revision_query->execute();
  }
}
63
64

/**
65
66
67
68
69
70
71
72
73
74
75
76
77
 * Implements hook_update_dependencies().
 */
function field_sql_storage_update_dependencies() {
  // Convert storage tables after field definitions have moved to
  // ConfigEntities.
  $dependencies['field_sql_storage'][8000] = array(
    'field' => 8003,
  );
  return $dependencies;
}

/**
 * Renames the 'language' column to 'langcode' in field data tables.
78
79
 */
function field_sql_storage_update_8000(&$sandbox) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  // Get field definitions from config, and deleted fields from state().
  $config_names = config_get_storage_names_with_prefix('field.field');
  $deleted_fields = Drupal::state()->get('field.field.deleted') ?: array();
  // Ditch UUID keys, we will iterate through deleted fields using a numeric
  // index.
  $deleted_fields = array_values($deleted_fields);

  if (empty($config_names) && empty($deleted_fields)) {
    return;
  }

  if (!isset($sandbox['index'])) {
    $sandbox['index'] = 0;
    $sandbox['max'] = count($config_names) + count($deleted_fields);
94
  }
95

96
97
98
99
100
101
102
103
  // Retrieve the next field definition. When the index exceeds the number of
  // 'configuration' fields, use it to iterate on deleted fields.
  if (isset($config_names[$sandbox['index']])) {
    $field_config = config($config_names[$sandbox['index']])->get();
  }
  else {
    $field_config = $deleted_fields[$sandbox['index'] - count($config_names)];
  }
104

105
106
  if ($field_config['storage']['type'] == 'field_sql_storage') {
    $field = new Field($field_config);
107
108

    // Prepare updated schema data structures.
109
    $primary_key_data = array(
110
111
112
113
114
115
      'entity_type',
      'entity_id',
      'deleted',
      'delta',
      'langcode',
    );
116
    $primary_key_revision = array(
117
118
119
120
121
122
123
124
125
126
127
128
129
      'entity_type',
      'entity_id',
      'revision_id',
      'deleted',
      'delta',
      'langcode',
    );
    $langcode_index = array(
      'langcode',
    );
    $field_langcode = array(
      'type' => 'varchar',
      'length' => 32,
130
      'not null' => TRUE,
131
132
      'default' => '',
    );
133

134
135
136
137
    $table_info = array(
      _field_sql_storage_tablename($field) => $primary_key_data,
      _field_sql_storage_revision_tablename($field) => $primary_key_revision,
    );
138
    foreach ($table_info as $table => $primary_key) {
139
140
      // Do not update tables which already have the langcode column,
      // created during the upgrade before this update function.
141
142
143
144
145
146
147
      if (db_field_exists($table, 'language')) {
        db_drop_primary_key($table);
        db_drop_index($table, 'language');
        db_change_field($table, 'language', 'langcode', $field_langcode);
        db_add_primary_key($table, $primary_key);
        db_add_index($table, 'langcode', $langcode_index);
      }
148
149
    }
  }
150

151
152
  $sandbox['index']++;
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['index'] / $sandbox['max']);
153
}