Project

General

Profile

Paste
Download (3.73 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field / modules / list / list.install @ db2d93dd

1
<?php
2

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

    
8
/**
9
 * Implements hook_field_schema().
10
 */
11
function list_field_schema($field) {
12
  switch ($field['type']) {
13
    case 'list_text':
14
      $columns = array(
15
        'value' => array(
16
          'type' => 'varchar',
17
          'length' => 255,
18
          'not null' => FALSE,
19
        ),
20
      );
21
      break;
22
    case 'list_float':
23
      $columns = array(
24
        'value' => array(
25
          'type' => 'float',
26
          'not null' => FALSE,
27
        ),
28
      );
29
      break;
30
    case 'list_integer':
31
    case 'list_boolean':
32
      $columns = array(
33
        'value' => array(
34
          'type' => 'int',
35
          'not null' => FALSE,
36
        ),
37
      );
38
      break;
39
  }
40
  return array(
41
    'columns' => $columns,
42
    'indexes' => array(
43
      'value' => array('value'),
44
    ),
45
  );
46
}
47

    
48
/**
49
 * Rename the list field types and change 'allowed_values' format.
50
 */
51
function list_update_7001() {
52
  $fields = _update_7000_field_read_fields(array('module' => 'list'));
53
  foreach ($fields as $field) {
54
    $update = array();
55

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

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

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

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

    
81
    // Save the new data.
82
    if ($update) {
83
      $query = db_update('field_config')
84
        ->condition('id', $field['id'])
85
        ->fields($update)
86
        ->execute();
87
    }
88
  }
89
}
90

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

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

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

    
116
  return $values;
117
}
118

    
119
/**
120
 * @addtogroup updates-7.x-extra
121
 * @{
122
 */
123

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

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