node_export_features.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function node_export_features_features_api() {
  6. $features = array();
  7. $features['node_export_features'] = array(
  8. 'name' => t('Node export'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'node_export_features_default',
  11. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  12. );
  13. return $features;
  14. }
  15. /**
  16. * Implements hook_features_export_options().
  17. */
  18. function node_export_features_features_export_options() {
  19. $options = array();
  20. $query = db_select('node', 'n')
  21. ->fields('n', array('nid', 'title', 'type'))
  22. ->orderBy('type')
  23. ->orderBy('title')
  24. ->range(0, 250)
  25. ->addTag('node_export_features');
  26. $result = $query->execute();
  27. foreach ($result as $row) {
  28. $uuid = uuid_get_uuid('node', 'nid', $row->nid);
  29. if (empty($uuid)) {
  30. drupal_set_message(
  31. t('Some nodes are <strong>not</strong> available for export' .
  32. ' because of missing UUIDs. Ensure UUIDs are being generated for' .
  33. ' all content types and click the <em>Create missing UUIDs</em>' .
  34. ' button on the <a href="!url">UUID settings page</a> to help' .
  35. ' resolve this issue.',
  36. array('!url' => url('admin/settings/uuid'))
  37. ),
  38. 'warning',
  39. FALSE
  40. );
  41. }
  42. else {
  43. $options[$uuid] = t('@type: @title', array(
  44. '@type' => node_type_get_name($row->type),
  45. '@title' => $row->title,
  46. ));
  47. }
  48. }
  49. if (count($options) == 250) {
  50. drupal_set_message(
  51. t('Due to limitations in Features only the first 250 nodes are available.'
  52. . ' The query is tagged <em>node_export_features</em> if you want to'
  53. . ' alter it.'),
  54. 'warning'
  55. );
  56. }
  57. return $options;
  58. }
  59. /**
  60. * Implements hook_features_export().
  61. */
  62. function node_export_features_features_export($data, &$export, $module_name = '') {
  63. $pipe = array();
  64. $export['dependencies']['module'] = 'node_export_features';
  65. foreach ($data as $uuid) {
  66. $query = db_select('node', 'n')
  67. ->fields('n', array('type'))
  68. ->condition('n.uuid', $uuid);
  69. $type = $query->execute()->fetchField();
  70. $export['features']['node_export_features'][$uuid] = $uuid;
  71. $pipe['node'][$type] = $type;
  72. }
  73. return $pipe;
  74. }
  75. /**
  76. * Implements hook_node_export_alter().
  77. */
  78. function node_export_features_node_export_alter(&$nodes, $format) {
  79. if (_node_export_features_currently_exporting()) {
  80. foreach ($nodes as $key => $node) {
  81. // Perform cleaning of the node before creating the export for features.
  82. // This can help strip volatile attributes like 'created' and 'changed'.
  83. $nodes[$key] = node_export_node_clone($node);
  84. }
  85. }
  86. }
  87. /**
  88. * Check if the code is currently running a feature export.
  89. */
  90. function _node_export_features_currently_exporting($set = FALSE, $value = NULL) {
  91. $exporting = &drupal_static(__FUNCTION__, FALSE);
  92. if ($set) {
  93. $exporting = $value;
  94. }
  95. return $exporting;
  96. }
  97. /**
  98. * Implements hook_features_export_render().
  99. */
  100. function node_export_features_features_export_render($module, $data, $export = NULL) {
  101. $nids = entity_get_id_by_uuid('node', $data);
  102. _node_export_features_currently_exporting(TRUE, TRUE);
  103. $result = node_export($nids);
  104. _node_export_features_currently_exporting(TRUE, FALSE);
  105. if ($result['success']) {
  106. $node_export['code_string'] = $result['output'];
  107. $node_export_code = ' $node_export = ' . features_var_export($node_export) . ';';
  108. }
  109. else {
  110. foreach ($result['output'] as $error) {
  111. $node_export_code = ' // ' . $error . PHP_EOL;
  112. }
  113. $node_export_code .= ' $node_export = array();';
  114. }
  115. $node_export_code .= PHP_EOL . ' return $node_export;';
  116. return array('node_export_features_default' => $node_export_code);
  117. }
  118. /**
  119. * Implements hook_features_revert().
  120. */
  121. function node_export_features_features_revert($module = NULL) {
  122. node_export_features_features_rebuild($module);
  123. }
  124. /**
  125. * Implements hook_features_rebuild().
  126. */
  127. function node_export_features_features_rebuild($module) {
  128. $node_export = features_get_default('node_export_features', $module);
  129. if (!empty($node_export)) {
  130. $result = node_export_import($node_export['code_string']);
  131. if (!$result['success']) {
  132. foreach ($result['output'] as $error) {
  133. drupal_set_message($error, 'error');
  134. }
  135. }
  136. else {
  137. if (!isset($_GET['profile'])) {
  138. foreach ($result['output'] as $status) {
  139. drupal_set_message($status);
  140. }
  141. }
  142. }
  143. }
  144. }