features.taxonomy.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function taxonomy_features_api() {
  6. return array(
  7. 'taxonomy' => array(
  8. 'name' => t('Taxonomy'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'taxonomy_default_vocabularies',
  11. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  12. ),
  13. );
  14. }
  15. /**
  16. * Implements hook_features_export_options().
  17. */
  18. function taxonomy_features_export_options() {
  19. $vocabularies = array();
  20. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  21. $vocabularies[$vocabulary->machine_name] = $vocabulary->name;
  22. }
  23. return $vocabularies;
  24. }
  25. /**
  26. * Implements hook_features_export().
  27. *
  28. * @todo Test adding existing dependencies.
  29. */
  30. function taxonomy_features_export($data, &$export, $module_name = '') {
  31. $pipe = array();
  32. // taxonomy_default_vocabularies integration is provided by Features.
  33. $export['dependencies']['features'] = 'features';
  34. $export['dependencies']['taxonomy'] = 'taxonomy';
  35. // Add dependencies for each vocabulary.
  36. $map = features_get_default_map('taxonomy');
  37. foreach ($data as $machine_name) {
  38. if (isset($map[$machine_name]) && $map[$machine_name] != $module_name) {
  39. $export['dependencies'][$map[$machine_name]] = $map[$machine_name];
  40. }
  41. else {
  42. $export['features']['taxonomy'][$machine_name] = $machine_name;
  43. $fields = field_info_instances('taxonomy_term', $machine_name);
  44. foreach ($fields as $name => $field) {
  45. $pipe['field'][] = "taxonomy_term-{$field['bundle']}-{$field['field_name']}";
  46. }
  47. }
  48. }
  49. return $pipe;
  50. }
  51. /**
  52. * Implements hook_features_export_render().
  53. */
  54. function taxonomy_features_export_render($module, $data) {
  55. $vocabularies = taxonomy_get_vocabularies();
  56. $code = array();
  57. foreach ($data as $machine_name) {
  58. foreach ($vocabularies as $vocabulary) {
  59. if ($vocabulary->machine_name == $machine_name) {
  60. // We don't want to break the entity cache, so we need to clone the
  61. // vocabulary before unsetting the id.
  62. $vocabulary = clone $vocabulary;
  63. unset($vocabulary->vid);
  64. $code[$machine_name] = $vocabulary;
  65. }
  66. }
  67. }
  68. $code = " return ". features_var_export($code, ' ') .";";
  69. return array('taxonomy_default_vocabularies' => $code);
  70. }
  71. /**
  72. * Implements hook_features_revert().
  73. */
  74. function taxonomy_features_revert($module) {
  75. taxonomy_features_rebuild($module);
  76. }
  77. /**
  78. * Implements hook_features_rebuild().
  79. *
  80. * Rebuilds Taxonomy vocabularies from code defaults.
  81. */
  82. function taxonomy_features_rebuild($module) {
  83. if ($vocabularies = features_get_default('taxonomy', $module)) {
  84. $existing = taxonomy_get_vocabularies();
  85. foreach ($vocabularies as $vocabulary) {
  86. $vocabulary = (object) $vocabulary;
  87. foreach ($existing as $existing_vocab) {
  88. if ($existing_vocab->machine_name === $vocabulary->machine_name) {
  89. $vocabulary->vid = $existing_vocab->vid;
  90. }
  91. }
  92. taxonomy_vocabulary_save($vocabulary);
  93. }
  94. }
  95. }