uuid_node.features.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Features hooks for the uuid_node features component.
  5. */
  6. /**
  7. * Implements hook_features_export_options().
  8. */
  9. function uuid_node_features_export_options() {
  10. $options = array();
  11. $types = node_type_get_names();
  12. $query = 'SELECT n.nid, n.title, n.type, n.uuid
  13. FROM {node} n ORDER BY n.type, n.title ASC';
  14. $results = db_query($query);
  15. foreach ($results as $node) {
  16. $options[$node->uuid] = t('@type: @title', array(
  17. '@type' => $types[$node->type],
  18. '@title' => $node->title,
  19. ));
  20. }
  21. return $options;
  22. }
  23. /**
  24. * Implements hook_features_export().
  25. */
  26. function uuid_node_features_export($data, &$export, $module_name = '') {
  27. $pipe = array();
  28. $export['dependencies']['uuid_features'] = 'uuid_features';
  29. uuid_features_load_module_includes();
  30. $nids = entity_get_id_by_uuid('node', $data);
  31. foreach ($nids as $uuid => $nid) {
  32. // Load the existing node, with a fresh cache.
  33. $node = node_load($nid, NULL, TRUE);
  34. $export['features']['uuid_node'][$uuid] = $uuid;
  35. $pipe['node'][$node->type] = $node->type;
  36. // drupal_alter() normally supports just one byref parameter. Using
  37. // the __drupal_alter_by_ref key, we can store any additional parameters
  38. // that need to be altered, and they'll be split out into additional params
  39. // for the hook_*_alter() implementations. The hook_alter signature is
  40. // hook_uuid_node_features_export_alter(&$export, &$pipe, $node)
  41. $data = &$export;
  42. $data['__drupal_alter_by_ref'] = array(&$pipe);
  43. drupal_alter('uuid_node_features_export', $data, $node);
  44. }
  45. return $pipe;
  46. }
  47. /**
  48. * Implements hook_features_export_render().
  49. */
  50. function uuid_node_features_export_render($module, $data) {
  51. $translatables = $code = array();
  52. uuid_features_load_module_includes();
  53. $code[] = ' $nodes = array();';
  54. $code[] = '';
  55. $nids = entity_get_id_by_uuid('node', $data);
  56. foreach ($nids as $uuid => $nid) {
  57. // Only export the node if it exists.
  58. if ($nid === FALSE) {
  59. continue;
  60. }
  61. // Attempt to load the node, using a fresh cache.
  62. $node = node_load($nid, NULL, TRUE);
  63. if (empty($node)) {
  64. continue;
  65. }
  66. if (!empty($node->path)) {
  67. $node->pathauto_perform_alias = FALSE;
  68. }
  69. $export = $node;
  70. // Use date instead of created, in the same format used by node_object_prepare.
  71. $export->date = format_date($export->created, 'custom', 'Y-m-d H:i:s O');
  72. // Don't cause conflicts with nid/vid/revision_timestamp/changed fields.
  73. unset($export->nid);
  74. unset($export->vid);
  75. unset($export->revision_timestamp);
  76. unset($export->changed);
  77. unset($export->last_comment_timestamp);
  78. // The hook_alter signature is:
  79. // hook_uuid_node_features_export_render_alter(&$export, $node, $module);
  80. drupal_alter('uuid_node_features_export_render', $export, $node, $module);
  81. $code[] = ' $nodes[] = ' . features_var_export($export) . ';';
  82. }
  83. if (!empty($translatables)) {
  84. $code[] = features_translatables_export($translatables, ' ');
  85. }
  86. $code[] = ' return $nodes;';
  87. $code = implode("\n", $code);
  88. return array('uuid_features_default_content' => $code);
  89. }
  90. /**
  91. * Implements hook_features_revert().
  92. */
  93. function uuid_node_features_revert($module) {
  94. uuid_node_features_rebuild($module);
  95. }
  96. /**
  97. * Implements hook_features_rebuild().
  98. * Rebuilds nodes based on UUID from code defaults.
  99. */
  100. function uuid_node_features_rebuild($module) {
  101. $nodes = module_invoke($module, 'uuid_features_default_content');
  102. if (!empty($nodes)) {
  103. module_load_include('inc', 'node', 'node.pages');
  104. foreach ($nodes as $data) {
  105. $node = (object) $data;
  106. node_object_prepare($node);
  107. // Find the matching UUID, with a fresh cache.
  108. $nids = entity_get_id_by_uuid('node', array($node->uuid));
  109. if (isset($nids[$node->uuid])) {
  110. $nid = array_key_exists($node->uuid, $nids) ? $nids[$node->uuid] : FALSE;
  111. $existing = node_load($nid, NULL, TRUE);
  112. if (!empty($existing)) {
  113. $node->nid = $existing->nid;
  114. $node->vid = $existing->vid;
  115. }
  116. }
  117. // The hook_alter signature is:
  118. // hook_uuid_node_features_rebuild_alter(&node, $module);
  119. drupal_alter('uuid_node_features_rebuild', $node, $module);
  120. $node = node_submit($node);
  121. node_save($node);
  122. }
  123. }
  124. }