summaryrefslogtreecommitdiff
path: root/modules/field/modules/list/list.install
blob: 2386f0483ae7db586c4094180ef348ddbefc3b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

/**
 * @file
 * Install, update and uninstall functions for the list module.
 */

/**
 * Implements hook_field_schema().
 */
function list_field_schema($field) {
  switch ($field['type']) {
    case 'list_text':
      $columns = array(
        'value' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
      );
      break;
    case 'list_float':
      $columns = array(
        'value' => array(
          'type' => 'float',
          'not null' => FALSE,
        ),
      );
      break;
    case 'list_integer':
    case 'list_boolean':
      $columns = array(
        'value' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
      );
      break;
  }
  return array(
    'columns' => $columns,
    'indexes' => array(
      'value' => array('value'),
    ),
  );
}

/**
 * Rename the list field types and change 'allowed_values' format.
 */
function list_update_7001() {
  $fields = _update_7000_field_read_fields(array('module' => 'list'));
  foreach ($fields as $field) {
    $update = array();

    // Translate the old string format into the new array format.
    $allowed_values = $field['settings']['allowed_values'];
    if (is_string($allowed_values)) {
      $position_keys = ($field['type'] == 'list');
      $allowed_values = _list_update_7001_extract_allowed_values($allowed_values, $position_keys);

      // Additionally, float keys need to be disambiguated ('.5' is '0.5').
      if ($field['type'] == 'list_number' && !empty($allowed_values)) {
        $keys = array_map(create_function('$a', 'return (string) (float) $a;'), array_keys($allowed_values));
        $allowed_values = array_combine($keys, array_values($allowed_values));
      }

      // Place the new setting in the existing serialized 'data' column.
      $data = db_query("SELECT data FROM {field_config} WHERE id = :id", array(':id' => $field['id']))->fetchField();
      $data = unserialize($data);
      $data['settings']['allowed_values'] = $allowed_values;
      $update['data'] = serialize($data);
    }

    // Rename field types.
    $types = array('list' => 'list_integer', 'list_number' => 'list_float');
    if (isset($types[$field['type']])) {
      $update['type'] = $types[$field['type']];
    }

    // Save the new data.
    if ($update) {
      $query = db_update('field_config')
        ->condition('id', $field['id'])
        ->fields($update)
        ->execute();
    }
  }
}

/**
 * Helper function for list_update_7001: extract allowed values from a string.
 *
 * This reproduces the parsing logic in use before D7 RC2.
 */
function _list_update_7001_extract_allowed_values($string, $position_keys) {
  $values = array();

  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');

  foreach ($list as $key => $value) {
    // Check for a manually specified key.
    if (strpos($value, '|') !== FALSE) {
      list($key, $value) = explode('|', $value);
    }
    // Otherwise see if we need to use the value as the key. The "list" type
    // will automatically convert non-keyed lines to integers.
    elseif (!$position_keys) {
      $key = $value;
    }
    $values[$key] = (isset($value) && $value !== '') ? $value : $key;
  }

  return $values;
}

/**
 * @addtogroup updates-7.x-extra
 * @{
 */

/**
 * Re-apply list_update_7001() for deleted fields.
 */
function list_update_7002() {
  // See http://drupal.org/node/1022924: list_update_7001() intitally
  // overlooked deleted fields, which then caused fatal errors when the fields
  // were being purged.
  // list_update_7001() has the required checks to ensure it is reentrant, so
  // it can simply be executed once more..
  list_update_7001();
}

/**
 * @} End of "addtogroup updates-7.x-extra".
 */