replicate.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Main methods of Replicate module.
  5. */
  6. /**
  7. * Replicate the entity corresponding to the type and id passed in argument and save it.
  8. *
  9. * @param string $entity_type
  10. * The entity type.
  11. * @param int $id
  12. * The unique entity identifier.
  13. *
  14. * @return mixed
  15. * The newly created entity id if the clone has been created and saved,
  16. * else FALSE.
  17. *
  18. * @see replicate_entity()
  19. */
  20. function replicate_entity_by_id($entity_type, $id) {
  21. $original = entity_load_single($entity_type, $id);
  22. return replicate_entity($entity_type, $original);
  23. }
  24. /**
  25. * Replicate the entity passed in argument and save it.
  26. *
  27. * @param string $entity_type
  28. * The entity type.
  29. * @param object $entity
  30. * The entity to replicate.
  31. *
  32. * @return mixed
  33. * The newly created entity id if the clone has been created and saved,
  34. * else FALSE.
  35. */
  36. function replicate_entity($entity_type, $entity) {
  37. $clone = replicate_clone_entity($entity_type, $entity);
  38. if ($clone) {
  39. entity_save($entity_type, $clone);
  40. list($entity_id) = entity_extract_ids($entity_type, $clone);
  41. if (isset($entity_id)) {
  42. return $entity_id;
  43. }
  44. }
  45. return FALSE;
  46. }
  47. /**
  48. * Replicate the entity corresponding to the type and id passed in argument.
  49. *
  50. * Do not save the replicated entity.
  51. *
  52. * @param string $entity_type
  53. * The entity type.
  54. * @param int $id
  55. * The unique entity identifier.
  56. *
  57. * @return object
  58. * A new replicated entity.
  59. *
  60. * @see replicate_clone_entity()
  61. */
  62. function replicate_clone_entity_by_id($entity_type, $id) {
  63. $original = entity_load_single($entity_type, $id);
  64. return replicate_clone_entity($entity_type, $original);
  65. }
  66. /**
  67. * Replicate the entity passed in argument.
  68. *
  69. * This function do not save the replicated entity.
  70. *
  71. * @param string $entity_type
  72. * The entity type.
  73. * @param object $entity
  74. * The entity to replicate.
  75. *
  76. * @return object
  77. * A new replicated entity.
  78. */
  79. function replicate_clone_entity($entity_type, $entity) {
  80. $clone = clone $entity;
  81. if ($clone) {
  82. // Let other modules manage the cleaning of the entity.
  83. foreach (module_implements('replicate_entity_' . $entity_type) as $module) {
  84. $function = $module . '_replicate_entity_' . $entity_type;
  85. $function($clone);
  86. }
  87. // Set the entity as new entity.
  88. $clone->is_new = TRUE;
  89. // Let other modules do special actions on each field.
  90. replicate_clone_fields($clone, $entity_type);
  91. // Let other modules do special actions on the global entity.
  92. drupal_alter('replicate_entity', $clone, $entity_type, $entity);
  93. }
  94. return $clone;
  95. }
  96. /**
  97. * Replicate the fields of an entity.
  98. *
  99. * @param object $entity
  100. * The entity for which to clone the fields.
  101. * @param string $entity_type
  102. * The entity type.
  103. */
  104. function replicate_clone_fields(&$entity, $entity_type) {
  105. foreach (field_info_fields() as $field_name => $field) {
  106. if (isset($entity->$field_name)) {
  107. // Here call hook functions. Doesn't use module_invoke because we
  108. // want to pass the clone by reference.
  109. foreach (module_implements('replicate_field_' . $field['type']) as $module) {
  110. $function = $module . '_replicate_field_' . $field['type'];
  111. $function($entity, $entity_type, $field_name);
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Implements hook_help().
  118. */
  119. function replicate_help($path, $arg) {
  120. switch ($path) {
  121. case 'admin/help#replicate':
  122. // Return a line-break version of the module README.txt.
  123. return check_markup(file_get_contents(dirname(__FILE__) . "/README.txt"));
  124. }
  125. }
  126. /**
  127. * Implements hook_replicate_entity_ENTITY_TYPE().
  128. */
  129. function replicate_replicate_entity_node(&$entity) {
  130. $entity->nid = NULL;
  131. $entity->tnid = NULL;
  132. $entity->vid = NULL;
  133. $entity->created = NULL;
  134. $entity->changed = NULL;
  135. $entity->path = NULL;
  136. $entity->revision_timestamp = NULL;
  137. }
  138. /**
  139. * Implements hook_replicate_entity_ENTITY_TYPE().
  140. */
  141. function replicate_replicate_entity_taxonomy_vocabulary(&$entity) {
  142. $entity->vid = NULL;
  143. }
  144. /**
  145. * Implements hook_replicate_entity_ENTITY_TYPE().
  146. */
  147. function replicate_replicate_entity_taxonomy_term(&$entity) {
  148. $entity->tid = NULL;
  149. }
  150. /**
  151. * Implements hook_replicate_entity_ENTITY_TYPE().
  152. */
  153. function replicate_replicate_entity_user(&$entity) {
  154. $entity->uid = NULL;
  155. }
  156. /**
  157. * Implements hook_replicate_entity_ENTITY_TYPE().
  158. */
  159. function replicate_replicate_entity_comment(&$entity) {
  160. $entity->cid = NULL;
  161. $entity->created = NULL;
  162. $entity->changed = NULL;
  163. }
  164. /**
  165. * Implements hook_replicate_entity_ENTITY_TYPE().
  166. */
  167. function replicate_replicate_entity_file(&$entity) {
  168. $entity->fid = NULL;
  169. }