cer.properties.field_collection.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Contains entity property callback functions for the 'cer' struct exposed
  5. * to Entity API on field collections.
  6. *
  7. * @see cer_entity_property_info_alter().
  8. */
  9. /**
  10. * Gets a field collection's lineage as a string, e.g.
  11. * node:page:field_my_collection::field_collection_item:field_my_collection:%
  12. */
  13. function cer_get_field_collection_lineage(array $data, array $options, $property, $data_type, array $info) {
  14. return implode('::', $info['raw getter callback']($data, $options, $property, $data_type, $info));
  15. }
  16. /**
  17. * Gets a field collection's lineage as an array.
  18. */
  19. function cer_get_field_collection_lineage_array(array $data, array $options, $property, $data_type, array $info) {
  20. $collection = $data[1];
  21. // If this is the innermost entity, $options['lineage'] will be empty.
  22. if (! isset($options['lineage'])) {
  23. $options['lineage'][] = "field_collection_item:{$collection->field_name}:";
  24. }
  25. $data[0] = $collection->hostEntityType();
  26. $data[1] = $collection->hostEntity();
  27. list (, , $host_bundle) = entity_extract_IDs($data[0], $data[1]);
  28. array_unshift($options['lineage'], $data[0] . ":{$host_bundle}:{$collection->field_name}");
  29. // If this field collection is hosted in another field collection, recurse
  30. // upward. Otherwise, we're done; return the lineage array.
  31. if ($data[0] == 'field_collection_item') {
  32. return cer_get_field_collection_lineage_array($data, $options, $property, $data_type, $info);
  33. }
  34. else {
  35. return $options['lineage'];
  36. }
  37. }
  38. /**
  39. * Gets the zero-based depth of a field collection.
  40. */
  41. function cer_get_field_collection_depth(array $data, array $options, $property, $data_type, array $info) {
  42. $lineage = cer_get_field_collection_lineage_array($data, $options, $property, $data_type, $info);
  43. return (sizeof($lineage) - 1);
  44. }
  45. /**
  46. * Gets the ultimate owner of a field collection -- that is, the top-level entity
  47. * under which it's embedded. This could be any kind of entity that's not a field
  48. * collection item.
  49. */
  50. function cer_get_field_collection_owner(array $data, array $options, $property, $data_type, array $info) {
  51. // If the entity is a field collection item, recurse upward. Otherwise,
  52. // return the wrapped entity.
  53. if ($data[0] == 'field_collection_item') {
  54. $data[0] = $data[1]->hostEntityType();
  55. $data[1] = $data[1]->hostEntity();
  56. $self = __FUNCTION__;
  57. return $self($data, $options, $property, $data_type, $info);
  58. }
  59. else {
  60. return new EntityDrupalWrapper($data[0], $data[1]);
  61. }
  62. }