node_export_relation.taxonomy.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @file
  4. * The Node export relation OG include.
  5. *
  6. * Helps maintain organic groups relationships between nodes during node export operations.
  7. */
  8. /**
  9. * Add taxonomy term reference UUID.
  10. */
  11. function node_export_relation_taxonomy_build_term_reference($node) {
  12. $reference_fields = node_export_relation_taxonomy_term_reference_fields($node);
  13. // Iterate through the class properties
  14. foreach ($reference_fields as $field => $val) {
  15. // There may be multiple languages
  16. foreach ($val as $lang => $values) {
  17. // Multi-value fields
  18. foreach ($values as $pos => $value) {
  19. $term = taxonomy_term_load($value['tid']);
  20. $new_field = $node->$field;
  21. $new_field[$lang][$pos]['uuid'] = $term->uuid;
  22. $node->$field = $new_field;
  23. }
  24. }
  25. }
  26. }
  27. /**
  28. * Restore the tid based on the term UUID.
  29. */
  30. function node_export_relation_taxonomy_restore_term_reference($node) {
  31. $reference_fields = node_export_relation_taxonomy_term_reference_fields($node);
  32. // Iterate through the class properties
  33. foreach ($reference_fields as $field => $val) {
  34. // There may be multiple languages
  35. foreach ($val as $lang => $values) {
  36. // Multi-value fields
  37. foreach ($values as $pos => $value) {
  38. if (isset($value['uuid'])) {
  39. // @TODO load_by_uuid
  40. $tid = db_select('taxonomy_term_data', 'ttd')
  41. ->fields('ttd', array('tid'))
  42. ->condition('ttd.uuid', $value['uuid'])
  43. ->execute()
  44. ->fetchField();
  45. if ($tid) {
  46. $new_field[$lang][$pos]['tid'] = $tid;
  47. $node->$field = $new_field;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. /**
  55. * Get the term reference fields for a node.
  56. */
  57. function node_export_relation_taxonomy_term_reference_fields($node) {
  58. $reference_fields = array();
  59. $instances = field_info_instances('node', $node->type);
  60. if (!empty($instances)) {
  61. foreach ($instances as $instance) {
  62. $field = field_info_field($instance['field_name']);
  63. if (isset($field['type']) && $field['type'] == 'taxonomy_term_reference') {
  64. $field_name = $instance['field_name'];
  65. $reference_fields[$field_name] = $node->$field_name;
  66. }
  67. }
  68. }
  69. return $reference_fields;
  70. }