uuid.core.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of UUID hooks for all core modules.
  5. *
  6. * @todo
  7. * Replace all these hook implementations with a generic solution that uses
  8. * info from hook_entity_field_info() and hook_entity_property_info(). That
  9. * info should contain info about how UUIDs are mapped.
  10. */
  11. /**
  12. * @defgroup uuid_property Propery implementations
  13. * @{
  14. */
  15. /**
  16. * Implements hook_entity_uuid_load().
  17. */
  18. function node_entity_uuid_load(&$entities, $entity_type) {
  19. if ($entity_type == 'node') {
  20. entity_property_id_to_uuid($entities, 'user', array('uid', 'revision_uid'));
  21. entity_property_id_to_uuid($entities, 'node', 'tnid');
  22. }
  23. }
  24. /**
  25. * Implements hook_entity_uuid_presave().
  26. */
  27. function node_entity_uuid_presave(&$entity, $entity_type) {
  28. if ($entity_type == 'node') {
  29. entity_property_uuid_to_id($entity, 'user', array('uid', 'revision_uid'));
  30. entity_property_uuid_to_id($entity, 'node', 'tnid');
  31. }
  32. }
  33. /**
  34. * Implements hook_entity_uuid_load().
  35. */
  36. function book_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
  37. if ($entity_type == 'node') {
  38. if (!empty($entity->book)) {
  39. $entity->book['bid'] = current(entity_get_uuid_by_id($entity_type, array($entity->book['bid'])));
  40. }
  41. }
  42. }
  43. /**
  44. * Implements hook_entity_uuid_presave().
  45. */
  46. function book_entity_uuid_presave(&$entity, $entity_type) {
  47. if ($entity_type == 'node') {
  48. if (!empty($entity->book)) {
  49. $entity->book['bid'] = current(entity_get_id_by_uuid($entity_type, array($entity->book['bid'])));
  50. if (!$entity->book['bid']) {
  51. $entity->book['bid'] = 'new';
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * Implements hook_entity_uuid_presave().
  58. */
  59. function user_entity_uuid_presave(&$entity, $entity_type) {
  60. if ($entity_type == 'user') {
  61. if (!empty($entity->picture)) {
  62. $uuids = entity_get_id_by_uuid('file', array($entity->picture['uuid']));
  63. $fid = current($uuids);
  64. if (!$entity->is_new) {
  65. $entity->picture = file_load($fid);
  66. }
  67. else {
  68. $entity->picture = $fid;
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Implements hook_entity_uuid_load().
  75. */
  76. function comment_entity_uuid_load(&$entities, $entity_type) {
  77. switch ($entity_type) {
  78. case 'node':
  79. entity_property_id_to_uuid($entities, 'user', 'last_comment_uid');
  80. break;
  81. case 'comment':
  82. entity_property_id_to_uuid($entities, 'user', array('uid', 'u_uid'));
  83. entity_property_id_to_uuid($entities, 'node', 'nid');
  84. break;
  85. }
  86. }
  87. /**
  88. * Implements hook_entity_uuid_presave().
  89. */
  90. function comment_entity_uuid_presave(&$entity, $entity_type) {
  91. switch ($entity_type) {
  92. case 'node':
  93. entity_property_uuid_to_id($entity, 'user', 'last_comment_uid');
  94. break;
  95. case 'comment':
  96. entity_property_uuid_to_id($entity, 'user', array('uid', 'u_uid'));
  97. entity_property_uuid_to_id($entity, 'node', 'nid');
  98. break;
  99. }
  100. }
  101. /**
  102. * Implements hook_entity_uuid_load().
  103. */
  104. function file_entity_uuid_load(&$entities, $entity_type) {
  105. if ($entity_type == 'file') {
  106. entity_property_id_to_uuid($entities, 'user', 'uid');
  107. }
  108. }
  109. /**
  110. * Implements hook_entity_uuid_presave().
  111. */
  112. function file_entity_uuid_presave(&$entity, $entity_type) {
  113. if ($entity_type == 'file') {
  114. entity_property_uuid_to_id($entity, 'user', 'uid');
  115. if (isset($entity->file_contents)) {
  116. $directory = drupal_dirname($entity->uri);
  117. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  118. file_unmanaged_save_data(base64_decode($entity->file_contents), $entity->uri, FILE_EXISTS_REPLACE);
  119. }
  120. }
  121. }
  122. /**
  123. * Implements hook_entity_uuid_load().
  124. */
  125. function taxonomy_entity_uuid_load(&$entities, $entity_type) {
  126. if ($entity_type == 'taxonomy_term') {
  127. foreach ($entities as &$entity) {
  128. if (isset($entity->parent)) {
  129. if (!is_array($entity->parent)) {
  130. $entity->parent = array($entity->parent);
  131. }
  132. $uuids = entity_get_uuid_by_id('taxonomy_term', $entity->parent);
  133. $entity->parent = array_values($uuids);
  134. }
  135. unset($entity->vid);
  136. }
  137. }
  138. }
  139. /**
  140. * Implements hook_entity_uuid_presave().
  141. */
  142. function taxonomy_entity_uuid_presave(&$entity, $entity_type) {
  143. if ($entity_type == 'taxonomy_term') {
  144. if (isset($entity->parent)) {
  145. if (!is_array($entity->parent)) {
  146. $entity->parent = array($entity->parent);
  147. }
  148. $ids = entity_get_id_by_uuid('taxonomy_term', $entity->parent);
  149. $entity->parent = array_values($ids);
  150. }
  151. $vocabulary = taxonomy_vocabulary_machine_name_load($entity->vocabulary_machine_name);
  152. $entity->vid = $vocabulary->vid;
  153. }
  154. }
  155. /**
  156. * Implements hook_entity_uuid_load().
  157. */
  158. function field_entity_uuid_load(&$entities, $entity_type) {
  159. foreach ($entities as $i => $entity) {
  160. list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
  161. $instances = field_info_instances($entity_type, $bundle_name);
  162. foreach ($instances as $field_name => $instance) {
  163. $field = field_info_field($field_name);
  164. if (!empty($field) && isset($entity->{$field_name})) {
  165. foreach ($entity->{$field_name} as $langcode => &$items) {
  166. // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
  167. // that is not passing by reference.
  168. $function = $field['module'] . '_field_uuid_load';
  169. if (function_exists($function)) {
  170. $function($entity_type, $entity, $field, $instance, $langcode, $items);
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. /**
  178. * Implements hook_entity_uuid_presave().
  179. */
  180. function field_entity_uuid_presave(&$entity, $entity_type) {
  181. list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
  182. $instances = field_info_instances($entity_type, $bundle_name);
  183. foreach ($instances as $field_name => $instance) {
  184. $field = field_info_field($field_name);
  185. if (!empty($field) && isset($entity->{$field_name})) {
  186. foreach ($entity->{$field_name} as $langcode => &$items) {
  187. // Invoke 'hook_field_uuid_load'. We can't use module_invoke() since
  188. // that is not passing by reference.
  189. $function = $field['module'] . '_field_uuid_presave';
  190. if (function_exists($function)) {
  191. $function($entity_type, $entity, $field, $instance, $langcode, $items);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * @} End of "Property implementations"
  199. */
  200. /**
  201. * @defgroup uuid_field Field implementations
  202. * @{
  203. */
  204. /**
  205. * Implements hook_field_uuid_load().
  206. */
  207. function taxonomy_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  208. entity_property_id_to_uuid($items, 'taxonomy_term', 'tid');
  209. }
  210. /**
  211. * Implements hook_field_uuid_presave().
  212. */
  213. function taxonomy_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  214. entity_property_uuid_to_id($items, 'taxonomy_term', 'tid');
  215. }
  216. /**
  217. * Implements hook_field_uuid_load().
  218. */
  219. function file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  220. entity_property_id_to_uuid($items, 'file', 'fid');
  221. entity_property_id_to_uuid($items, 'user', 'uid');
  222. }
  223. /**
  224. * Implements hook_field_uuid_presave().
  225. */
  226. function file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  227. entity_property_uuid_to_id($items, 'file', 'fid');
  228. entity_property_uuid_to_id($items, 'user', 'uid');
  229. }
  230. /**
  231. * Implements hook_field_uuid_load().
  232. */
  233. function image_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  234. file_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, $items);
  235. }
  236. /**
  237. * Implements hook_field_uuid_presave().
  238. */
  239. function image_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  240. file_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, $items);
  241. }
  242. /**
  243. * Implements hook_field_uuid_load().
  244. */
  245. function node_reference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  246. entity_property_id_to_uuid($items, 'node', 'nid');
  247. }
  248. /**
  249. * Implements hook_field_uuid_presave().
  250. */
  251. function node_reference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  252. entity_property_uuid_to_id($items, 'node', 'nid');
  253. }
  254. /**
  255. * Implements hook_field_uuid_load().
  256. */
  257. function user_reference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  258. entity_property_id_to_uuid($items, 'user', 'uid');
  259. }
  260. /**
  261. * Implements hook_field_uuid_presave().
  262. */
  263. function user_reference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  264. entity_property_uuid_to_id($items, 'user', 'uid');
  265. }
  266. /**
  267. * Implements hook_field_uuid_load().
  268. */
  269. function entityreference_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  270. // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
  271. // can't handle a single $item.
  272. entity_property_id_to_uuid($items, $field['settings']['target_type'], 'target_id');
  273. }
  274. /**
  275. * Implements hook_field_uuid_presave().
  276. */
  277. function entityreference_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  278. // TODO: This is not really good, but as of now 'entity_property_id_to_uuid()'
  279. // can't handle a single $item.
  280. entity_property_uuid_to_id($items, $field['settings']['target_type'], 'target_id');
  281. }
  282. /**
  283. * Implements hook_entity_uuid_load().
  284. */
  285. function field_collection_entity_uuid_load(&$entities, $entity_type) {
  286. if ($entity_type == 'field_collection_item') {
  287. entity_property_id_to_uuid($entities, 'field_collection_item', 'value');
  288. }
  289. }
  290. /**
  291. * Implements hook_entity_uuid_presave().
  292. */
  293. function field_collection_entity_uuid_presave(&$entity, $entity_type) {
  294. if ($entity_type == 'field_collection_item') {
  295. entity_property_uuid_to_id($entity, 'field_collection_item', 'value');
  296. }
  297. }
  298. /**
  299. * Implements hook_field_uuid_load().
  300. */
  301. function field_collection_field_uuid_load($entity_type, $entity, $field, $instance, $langcode, &$items) {
  302. entity_property_id_to_uuid($items, 'field_collection_item', 'value');
  303. }
  304. /**
  305. * Implements hook_field_uuid_presave().
  306. */
  307. function field_collection_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  308. entity_property_uuid_to_id($items, 'field_collection_item', 'value');
  309. }
  310. /**
  311. * @} End of "Field implementations"
  312. */
  313. /**
  314. * @defgroup uuid_export Export alterations
  315. * @{
  316. */
  317. /**
  318. * Implements hook_uuid_entities_features_export_entity_alter().
  319. */
  320. function node_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
  321. if ($entity_type == 'node') {
  322. foreach (array('data', 'name', 'picture', 'revision_uid', 'last_comment_timestamp') as $property) {
  323. if (property_exists($entity, $property)) {
  324. unset($entity->{$property});
  325. }
  326. }
  327. }
  328. }
  329. /**
  330. * Implementation of hook_uuid_entities_features_export_entity_alter().
  331. */
  332. function user_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
  333. if ($entity_type == 'user') {
  334. foreach (array('data', 'access', 'login') as $property) {
  335. if (property_exists($entity, $property)) {
  336. unset($entity->{$property});
  337. }
  338. }
  339. }
  340. }
  341. /**
  342. * Implements hook_uuid_entities_features_export_alter().
  343. */
  344. function file_uuid_entities_features_export_field_alter($entity_type, $entity, $field, $instance, $langcode, &$items) {
  345. foreach ($items as &$item) {
  346. if (isset($item['timestamp'])) {
  347. unset($item['timestamp']);
  348. }
  349. }
  350. }
  351. /**
  352. * Implements hook_uuid_entities_features_export_entity_alter().
  353. */
  354. function workbench_uuid_entities_features_export_entity_alter(&$entity, $entity_type) {
  355. foreach (array('workbench_moderation', 'my_revision', 'workbench_access', 'workbench_access_scheme', 'workbench_access_by_role') as $property) {
  356. if (isset($entity->{$property})) {
  357. unset($entity->{$property});
  358. }
  359. }
  360. }
  361. /**
  362. * @} End of "Export alterations"
  363. */