list.install 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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('_list_update_7001_float_string_cast', 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 callback function to cast the array element.
  84. */
  85. function _list_update_7001_float_string_cast($element) {
  86. return (string) (float) $element;
  87. }
  88. /**
  89. * Helper function for list_update_7001: extract allowed values from a string.
  90. *
  91. * This reproduces the parsing logic in use before D7 RC2.
  92. */
  93. function _list_update_7001_extract_allowed_values($string, $position_keys) {
  94. $values = array();
  95. $list = explode("\n", $string);
  96. $list = array_map('trim', $list);
  97. $list = array_filter($list, 'strlen');
  98. foreach ($list as $key => $value) {
  99. // Check for a manually specified key.
  100. if (strpos($value, '|') !== FALSE) {
  101. list($key, $value) = explode('|', $value);
  102. }
  103. // Otherwise see if we need to use the value as the key. The "list" type
  104. // will automatically convert non-keyed lines to integers.
  105. elseif (!$position_keys) {
  106. $key = $value;
  107. }
  108. $values[$key] = (isset($value) && $value !== '') ? $value : $key;
  109. }
  110. return $values;
  111. }
  112. /**
  113. * @addtogroup updates-7.x-extra
  114. * @{
  115. */
  116. /**
  117. * Re-apply list_update_7001() for deleted fields.
  118. */
  119. function list_update_7002() {
  120. // See http://drupal.org/node/1022924: list_update_7001() intitally
  121. // overlooked deleted fields, which then caused fatal errors when the fields
  122. // were being purged.
  123. // list_update_7001() has the required checks to ensure it is reentrant, so
  124. // it can simply be executed once more..
  125. list_update_7001();
  126. }
  127. /**
  128. * @} End of "addtogroup updates-7.x-extra".
  129. */