content_taxonomy.install 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * (Un-) installation tasks for content taxonomy.
  5. */
  6. /**
  7. * Implements hook_disable().
  8. *
  9. * Allow uninstall of content taxonomy by removing the callback in field
  10. * configuration. Re-enabling requires to save the field configs to re-insert
  11. * the callback.
  12. */
  13. function content_taxonomy_disable() {
  14. $fields = field_read_fields();
  15. foreach ($fields as $field) {
  16. if (isset($field['settings']['options_list_callback']) && $field['settings']['options_list_callback'] == 'content_taxonomy_allowed_values') {
  17. // We cannot unset this value, because field_update_field() merges in
  18. // prior settings before saving. Setting it to NULL works.
  19. $field['settings']['options_list_callback'] = NULL;
  20. }
  21. field_update_field($field);
  22. }
  23. }
  24. /**
  25. * Implementations of hook_update_N().
  26. */
  27. /**
  28. * Fix the default values converted from D6. These values had an empty 'value'
  29. * value which could cause errors on D7.
  30. */
  31. function content_taxonomy_update_7100() {
  32. $params = array(
  33. 'type' => 'taxonomy_term_reference',
  34. );
  35. foreach (field_read_fields($params) as $field) {
  36. foreach (field_read_instances(array('field_name' => $field['field_name'])) as $instance) {
  37. // If the 'default_value' item doesn't exist there's no point in
  38. // continuing.
  39. if (!isset($instance['default_value'])) {
  40. continue;
  41. }
  42. // Keep track of whether fields are actually changed.
  43. $updated = FALSE;
  44. // Fix each of the default values.
  45. foreach ($instance['default_value'] as $key => $defaults) {
  46. // Need to check isset() and is_null() because the value could be NULL.
  47. if (isset($instance['default_value'][$key]['value']) || is_null($instance['default_value'][$key]['value'])) {
  48. // Remove any empty 'value' strings.
  49. if (empty($instance['default_value'][$key]['value'])) {
  50. unset($instance['default_value'][$key]['value']);
  51. $updated = TRUE;
  52. }
  53. // Rename the 'value' string to 'tid'.
  54. elseif (!isset($instance_value['default_value'][$key]['tid'])) {
  55. $instance_value['default_value'][$key]['tid'] = $instance_value['default_value'][$key]['value'];
  56. unset($instance_value['default_value'][$key]['value']);
  57. $updated = TRUE;
  58. }
  59. // Look for a junk value carried over from D6.
  60. if (isset($instance['default_value'][$key]['_error_element'])) {
  61. unset($instance['default_value'][$key]['_error_element']);
  62. $updated = TRUE;
  63. }
  64. // If the array is empty, just remove it.
  65. if (empty($instance['default_value'][$key])) {
  66. unset($instance['default_value'][$key]);
  67. $updated = TRUE;
  68. }
  69. }
  70. }
  71. // If there are no default values left, just remove it.
  72. if (empty($instance['default_value'])) {
  73. unset($instance['default_value']);
  74. $updated = TRUE;
  75. }
  76. // If the field's definition was changed, save it.
  77. if ($updated) {
  78. field_update_instance($instance);
  79. drupal_set_message(t('Fixed configuration of the "@field_name" field ("@type" content type).', array('@field_name' => $instance['field_name'], '@type' => $instance['bundle'])));
  80. }
  81. }
  82. }
  83. }