profile2.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Contains a CER field plugin for referencing Profile2 entities. These references
  5. * are not actually fields at all, so this plugin is really more of a bridge to
  6. * trick CER into thinking that Profile2 is a reference field.
  7. */
  8. class CerProfile2Field extends CerField implements CerEntityContainerInterface {
  9. protected $profileType;
  10. /**
  11. * @override CerField::__construct().
  12. */
  13. public function __construct(array $plugin) {
  14. $this->plugin = $plugin;
  15. list ($this->entityType, $this->bundle, $this->name) = explode(':', $plugin['identifier']);
  16. $info = entity_get_info($this->entityType);
  17. // These "fields" can only be instantiated on user accounts, which are
  18. // normally not bundleable. However, there could be a module in the wild
  19. // which makes accounts bundleable, so let's not be presumptuous here.
  20. $this->isBundleable = (boolean) $info['entity keys']['bundle'];
  21. $this->entityTypeLabel = $info['label'];
  22. $this->bundleLabel = $info['bundles'][$this->bundle]['label'];
  23. // An account can only have one profile of this type.
  24. $this->cardinality = 1;
  25. $this->fieldTypeLabel = t('Profile');
  26. // Load the Profile2 type information
  27. $this->profileType = profile2_get_types(subStr($this->name, 8));
  28. $this->label = $this->profileType->label;
  29. }
  30. /**
  31. * Implements CerField::getTargetType().
  32. */
  33. public function getTargetType() {
  34. // In effect, this "field" will "point to" Profile2 entities.
  35. return 'profile2';
  36. }
  37. /**
  38. * @override CerField::getTargetBundles().
  39. */
  40. public function getTargetBundles() {
  41. // This field can only "reference" one type of profile. There'll be
  42. // a separate instance of this plugin for each Profile2 type.
  43. return (array) $this->profileType->type;
  44. }
  45. /**
  46. * Implements CerEntityContainerInterface::createInnerEntity().
  47. */
  48. public function createInnerEntity(EntityDrupalWrapper $owner) {
  49. $init = array(
  50. 'user' => $owner->value(),
  51. 'type' => $this->profileType,
  52. );
  53. // Create a blank profile for this user, and save it. The "reference" is implied
  54. // by the existence of the profile.
  55. $profile = profile2_create($init);
  56. $profile->save();
  57. return $profile;
  58. }
  59. }