node_export_dependency.core.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @file
  4. * Contains hook implementations for all relevant core module.
  5. */
  6. /**
  7. * Implements hook_node_export_dependency().
  8. */
  9. function node_node_export_dependency($entity, $entity_type) {
  10. if ($entity_type == 'node') {
  11. $dependencies = array();
  12. // The node has a 'user' dependency through the 'uid' and
  13. // 'revision_uid' properties.
  14. node_export_dependency_add($dependencies, $entity, 'user', array('uid', 'revision_uid'));
  15. // The node has a 'node' dependency through the 'tnid' property.
  16. node_export_dependency_add($dependencies, $entity, 'node', 'tnid');
  17. return $dependencies;
  18. }
  19. }
  20. /**
  21. * Implements hook_node_export_dependency()
  22. */
  23. function taxonomy_node_export_dependency($entity, $entity_type) {
  24. if ($entity_type == 'taxonomy_term') {
  25. $dependencies = array();
  26. $terms = taxonomy_get_parents($entity->tid);
  27. $delta = 0;
  28. foreach ($terms as $tid => $term) {
  29. $dependencies[] = array(
  30. 'type' => 'taxonomy_term',
  31. 'id' => $tid,
  32. 'property' => 'parent',
  33. 'delta' => $delta++,
  34. );
  35. }
  36. return $dependencies;
  37. }
  38. }
  39. /**
  40. * Implements hook_node_export_dependency().
  41. */
  42. function book_node_export_dependency($entity, $entity_type) {
  43. if ($entity_type == 'node' && !empty($entity->book)) {
  44. $dependencies = array();
  45. // Book page's root book node.
  46. if (!empty($entity->book['bid'])) {
  47. $dependencies[] = array(
  48. 'type' => 'node',
  49. 'id' => $entity->book['bid'],
  50. 'property' => array(array('book', 'bid')),
  51. );
  52. }
  53. // Book page's immediate parent.
  54. if (!empty($entity->book['plid'])) {
  55. $parent_nid = db_query(
  56. 'SELECT nid FROM {book} WHERE mlid = :mlid',
  57. array(':mlid' => $entity->book['plid'])
  58. )->fetchField();
  59. $dependencies[] = array(
  60. 'type' => 'node',
  61. 'id' => $parent_nid,
  62. 'property' => array(array('book', 'plid')),
  63. // Recognise the relationship is not done through the entity id key.
  64. 'relationship' => array(
  65. 'key' => array('menu', 'mlid'),
  66. 'value' => $entity->book['plid'],
  67. ),
  68. );
  69. }
  70. // Book page's immediate children.
  71. $flat = book_get_flat_menu($entity->book);
  72. $children = array();
  73. if ($entity->book['has_children']) {
  74. // Walk through the array until we find the current page.
  75. do {
  76. $link = array_shift($flat);
  77. } while ($link && ($link['mlid'] != $entity->book['mlid']));
  78. // Continue through the array and collect the links whose parent is this page.
  79. while (($link = array_shift($flat)) && $link['plid'] == $entity->book['mlid']) {
  80. $matches = array();
  81. if (preg_match('/^node\/([\d]+)$/', $link['href'], $matches)) {
  82. $dependencies[] = array(
  83. 'type' => 'node',
  84. 'id' => $matches[1],
  85. );
  86. }
  87. }
  88. }
  89. return $dependencies;
  90. }
  91. }
  92. /**
  93. * Implements hook_node_export_dependency().
  94. */
  95. function og_node_export_dependency($entity, $entity_type) {
  96. if ($entity_type == 'node') {
  97. $dependencies = array();
  98. if (!empty($entity->og_groups)) {
  99. foreach (array_keys($entity->og_groups) as $delta) {
  100. entity_dependency_add($dependencies, $entity, 'node', array(array('og_groups', $delta)));
  101. }
  102. }
  103. if (!empty($entity->og_parent->nid)) {
  104. entity_dependency_add($dependencies, $entity, 'node', array(array('og_parent', 'nid')));
  105. }
  106. if (!empty($dependencies)) {
  107. return $dependencies;
  108. }
  109. }
  110. }
  111. /**
  112. * Implements hook_node_export_dependency().
  113. */
  114. function field_node_export_dependency($entity, $entity_type) {
  115. $dependencies = array();
  116. list(,, $bundle_name) = entity_extract_ids($entity_type, $entity);
  117. $instances = field_info_instances($entity_type, $bundle_name);
  118. foreach ($instances as $field_name => $instance) {
  119. $field = field_info_field($field_name);
  120. foreach ($entity->{$field_name} as $langcode => $items) {
  121. $field_dependencies = module_invoke($field['module'], 'node_export_dependency_field', $entity_type, $entity, $field, $instance, $langcode, $items);
  122. // Let other modules alter dependencies for this field.
  123. drupal_alter('node_export_dependency_field', $field_dependencies, $entity_type, $entity, $field, $instance, $langcode, $items);
  124. if (!empty($field_dependencies)) {
  125. foreach ($field_dependencies as &$field_dependency) {
  126. if (empty($field_dependency['module'])) {
  127. $field_dependency['module'] = $field['module'];
  128. }
  129. if (empty($field_dependency['field_name'])) {
  130. $field_dependency['field_name'] = $field_name;
  131. }
  132. if (empty($field_dependency['langcode'])) {
  133. $field_dependency['langcode'] = $langcode;
  134. }
  135. }
  136. $dependencies = array_merge_recursive($dependencies, $field_dependencies);
  137. }
  138. }
  139. }
  140. return $dependencies;
  141. }
  142. /**
  143. * Implements hook_node_export_dependency_field().
  144. */
  145. function taxonomy_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  146. // No need to check for the field type here, since this hook is only called
  147. // for the owner of this field. Taxonomy module only owns one field.
  148. $dependencies = array();
  149. node_export_dependency_add($dependencies, $items, 'taxonomy_term', 'tid');
  150. return $dependencies;
  151. }
  152. /**
  153. * Implements hook_node_export_dependency_field().
  154. */
  155. function file_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  156. $dependencies = array();
  157. node_export_dependency_add($dependencies, $items, 'file', 'fid');
  158. node_export_dependency_add($dependencies, $items, 'user', 'uid');
  159. return $dependencies;
  160. }
  161. /**
  162. * Implements hook_node_export_dependency_field().
  163. */
  164. function image_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  165. return file_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items);
  166. }
  167. /**
  168. * Implements hook_node_export_dependency_field().
  169. */
  170. function node_reference_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  171. $dependencies = array();
  172. node_export_dependency_add($dependencies, $items, 'node', 'nid');
  173. return $dependencies;
  174. }
  175. /**
  176. * Implements hook_node_export_dependency_field().
  177. */
  178. function user_reference_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  179. $dependencies = array();
  180. node_export_dependency_add($dependencies, $items, 'user', 'uid');
  181. return $dependencies;
  182. }
  183. /**
  184. * Implements hook_node_export_dependency_field().
  185. */
  186. function entityreference_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  187. $dependencies = array();
  188. node_export_dependency_add($dependencies, $items, $field['settings']['target_type'], 'target_id');
  189. return $dependencies;
  190. }
  191. /**
  192. * Implements hook_node_export_dependency_field().
  193. */
  194. function field_collection_node_export_dependency_field($entity_type, $entity, $field, $instance, $langcode, $items) {
  195. $dependencies = array();
  196. node_export_dependency_add($dependencies, $items, 'field_collection_item', 'value');
  197. // Loop all items to add their data for export.
  198. foreach($items as $index => $item) {
  199. if ($field_collection_item = field_collection_item_load($item['value'])) {
  200. // Add file field data for export.
  201. node_export_file_field_export($field_collection_item,
  202. $field_collection_item);
  203. // Add the field collection item data to the exported data. Mimics
  204. // EntityAPIController::export() without the JSON conversion.
  205. $dependencies[$index]['node_export_field_collection_data']
  206. = get_object_vars($field_collection_item);
  207. // Support nested fields and field collections too.
  208. $dependencies = array_merge($dependencies, field_node_export_dependency(
  209. $field_collection_item, 'field_collection_item'));
  210. }
  211. }
  212. return $dependencies;
  213. }