features.taxonomy.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $pipe['field_instance'][] = "taxonomy_term-{$field['bundle']}-{$field['field_name']}";
  47. }
  48. }
  49. }
  50. return $pipe;
  51. }
  52. /**
  53. * Implements hook_features_export_render().
  54. */
  55. function taxonomy_features_export_render($module, $data) {
  56. $vocabularies = taxonomy_get_vocabularies();
  57. $code = array();
  58. foreach ($data as $machine_name) {
  59. foreach ($vocabularies as $vocabulary) {
  60. if ($vocabulary->machine_name == $machine_name) {
  61. // We don't want to break the entity cache, so we need to clone the
  62. // vocabulary before unsetting the id.
  63. $vocabulary = clone $vocabulary;
  64. unset($vocabulary->vid);
  65. $code[$machine_name] = $vocabulary;
  66. }
  67. }
  68. }
  69. $code = " return ". features_var_export($code, ' ') .";";
  70. return array('taxonomy_default_vocabularies' => $code);
  71. }
  72. /**
  73. * Implements hook_features_revert().
  74. */
  75. function taxonomy_features_revert($module) {
  76. taxonomy_features_rebuild($module);
  77. }
  78. /**
  79. * Implements hook_features_rebuild().
  80. *
  81. * Rebuilds Taxonomy vocabularies from code defaults.
  82. */
  83. function taxonomy_features_rebuild($module) {
  84. if ($vocabularies = features_get_default('taxonomy', $module)) {
  85. $existing = taxonomy_get_vocabularies();
  86. foreach ($vocabularies as $vocabulary) {
  87. $vocabulary = (object) $vocabulary;
  88. foreach ($existing as $existing_vocab) {
  89. if ($existing_vocab->machine_name === $vocabulary->machine_name) {
  90. $vocabulary->vid = $existing_vocab->vid;
  91. }
  92. }
  93. taxonomy_vocabulary_save($vocabulary);
  94. }
  95. }
  96. }