list.install

Install, update and uninstall functions for the list module.

File

drupal-7.x/modules/field/modules/list/list.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the list module.
  5. */
  6. /**
  7. * Implements hook_field_schema().
  8. */
  9. function list_field_schema($field) {
  10. switch ($field['type']) {
  11. case 'list_text':
  12. $columns = array(
  13. 'value' => array(
  14. 'type' => 'varchar',
  15. 'length' => 255,
  16. 'not null' => FALSE,
  17. ),
  18. );
  19. break;
  20. case 'list_float':
  21. $columns = array(
  22. 'value' => array(
  23. 'type' => 'float',
  24. 'not null' => FALSE,
  25. ),
  26. );
  27. break;
  28. case 'list_integer':
  29. case 'list_boolean':
  30. $columns = array(
  31. 'value' => array(
  32. 'type' => 'int',
  33. 'not null' => FALSE,
  34. ),
  35. );
  36. break;
  37. }
  38. return array(
  39. 'columns' => $columns,
  40. 'indexes' => array(
  41. 'value' => array('value'),
  42. ),
  43. );
  44. }
  45. /**
  46. * Rename the list field types and change 'allowed_values' format.
  47. */
  48. function list_update_7001() {
  49. $fields = _update_7000_field_read_fields(array('module' => 'list'));
  50. foreach ($fields as $field) {
  51. $update = array();
  52. // Translate the old string format into the new array format.
  53. $allowed_values = $field['settings']['allowed_values'];
  54. if (is_string($allowed_values)) {
  55. $position_keys = ($field['type'] == 'list');
  56. $allowed_values = _list_update_7001_extract_allowed_values($allowed_values, $position_keys);
  57. // Additionally, float keys need to be disambiguated ('.5' is '0.5').
  58. if ($field['type'] == 'list_number' && !empty($allowed_values)) {
  59. $keys = array_map(create_function('$a', 'return (string) (float) $a;'), array_keys($allowed_values));
  60. $allowed_values = array_combine($keys, array_values($allowed_values));
  61. }
  62. // Place the new setting in the existing serialized 'data' column.
  63. $data = db_query("SELECT data FROM {field_config} WHERE id = :id", array(':id' => $field['id']))->fetchField();
  64. $data = unserialize($data);
  65. $data['settings']['allowed_values'] = $allowed_values;
  66. $update['data'] = serialize($data);
  67. }
  68. // Rename field types.
  69. $types = array('list' => 'list_integer', 'list_number' => 'list_float');
  70. if (isset($types[$field['type']])) {
  71. $update['type'] = $types[$field['type']];
  72. }
  73. // Save the new data.
  74. if ($update) {
  75. $query = db_update('field_config')
  76. ->condition('id', $field['id'])
  77. ->fields($update)
  78. ->execute();
  79. }
  80. }
  81. }
  82. /**
  83. * Helper function for list_update_7001: extract allowed values from a string.
  84. *
  85. * This reproduces the parsing logic in use before D7 RC2.
  86. */
  87. function _list_update_7001_extract_allowed_values($string, $position_keys) {
  88. $values = array();
  89. $list = explode("\n", $string);
  90. $list = array_map('trim', $list);
  91. $list = array_filter($list, 'strlen');
  92. foreach ($list as $key => $value) {
  93. // Check for a manually specified key.
  94. if (strpos($value, '|') !== FALSE) {
  95. list($key, $value) = explode('|', $value);
  96. }
  97. // Otherwise see if we need to use the value as the key. The "list" type
  98. // will automatically convert non-keyed lines to integers.
  99. elseif (!$position_keys) {
  100. $key = $value;
  101. }
  102. $values[$key] = (isset($value) && $value !== '') ? $value : $key;
  103. }
  104. return $values;
  105. }
  106. /**
  107. * @addtogroup updates-7.x-extra
  108. * @{
  109. */
  110. /**
  111. * Re-apply list_update_7001() for deleted fields.
  112. */
  113. function list_update_7002() {
  114. // See http://drupal.org/node/1022924: list_update_7001() intitally
  115. // overlooked deleted fields, which then caused fatal errors when the fields
  116. // were being purged.
  117. // list_update_7001() has the required checks to ensure it is reentrant, so
  118. // it can simply be executed once more..
  119. list_update_7001();
  120. }
  121. /**
  122. * @} End of "addtogroup updates-7.x-extra".
  123. */