replicate.api.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @file
  4. * API documentation for the Replicate module.
  5. */
  6. /**
  7. * Alter the replica of type ENTITY_TYPE.
  8. *
  9. * Use this function to make specific changes to an entity of a given type.
  10. * Usefull to "clean" custom entities, ie reset their id to be able to save
  11. * the new copy for example.
  12. *
  13. * @param object $replica
  14. * Reference to the fully loaded entity object being saved (the clone) that
  15. * can be altered as needed.
  16. *
  17. * @see replicate_clone_entity()
  18. */
  19. function hook_replicate_entity_ENTITY_TYPE(&$replica) {
  20. // Clean the custom entity so Drupal will create a new entry
  21. // and not update the old one when saving.
  22. $replica->entity_id = NULL;
  23. // Do something specific to this type of entity.
  24. $wrapper = entity_metadata_wrapper('ENTITY_TYPE', $replica);
  25. $wrapper->field_my_field->set('This is a replica of a ENTITY_TYPE');
  26. }
  27. /**
  28. * Alter the replica before returning it.
  29. *
  30. * This hook is called at the end of the operations
  31. * of replicate_clone_entity() function, allowing to alter the replicate
  32. * before it is return to the caller. This function will apply to all
  33. * replicated entities.
  34. *
  35. * @param object $replica
  36. * Reference to the fully loaded entity object being saved (the clone) that
  37. * can be altered as needed.
  38. * @param string $entity_type
  39. * Type of the entity containing the field.
  40. * @param object $original
  41. * The fully loaded original entity object.
  42. *
  43. * @see replicate_clone_entity()
  44. * @see drupal_alter()
  45. */
  46. function hook_replicate_entity_alter(&$replica, $entity_type, $original) {
  47. // Do something common to all entities that are replicated.
  48. $wrapper = entity_metadata_wrapper($entity_type, $replica);
  49. $wrapper->field_is_replica->set(TRUE);
  50. }
  51. /**
  52. * Manage the replication of a specific field type.
  53. *
  54. * May be used to manage the replication of custom field type,
  55. * for example node references.
  56. *
  57. * @param object $replica
  58. * Reference to the fully loaded entity object being saved (the clone) that
  59. * can be altered as needed.
  60. * @param string $entity_type
  61. * Type of the entity containing the field.
  62. * @param string $field_name
  63. * Name of the field that is going to be processed.
  64. *
  65. * @see replicate_clone_entity()
  66. */
  67. function hook_replicate_field_FIELD_TYPE(&$replica, $entity_type, $field_name) {
  68. // Simplified example from Replicate Field Collection module.
  69. // Manage the replication of a Field Collection field with a custom function.
  70. foreach ($entity->$field_name as $language => $values) {
  71. my_custom_function_to_clone_field_collections($entity, $entity_type, $field_name, $language);
  72. }
  73. }