You are here

class CerProfile2Field in Corresponding Entity References 7.3

@file Contains a CER field plugin for referencing Profile2 entities. These references are not actually fields at all, so this plugin is really more of a bridge to trick CER into thinking that Profile2 is a reference field.

Hierarchy

Expanded class hierarchy of CerProfile2Field

1 string reference to 'CerProfile2Field'
cer_profile2_cer_fields in extensions/cer_profile2/cer_profile2.cer.inc
Implements hook_cer_fields().

File

extensions/cer_profile2/profile2.inc, line 10
Contains a CER field plugin for referencing Profile2 entities. These references are not actually fields at all, so this plugin is really more of a bridge to trick CER into thinking that Profile2 is a reference field.

View source
class CerProfile2Field extends CerField implements CerEntityContainerInterface {
  protected $profileType;

  /**
   * @override CerField::__construct().
   */
  public function __construct(array $plugin) {
    $this->plugin = $plugin;
    list($this->entityType, $this->bundle, $this->name) = explode(':', $plugin['identifier']);
    $info = entity_get_info($this->entityType);

    // These "fields" can only be instantiated on user accounts, which are
    // normally not bundleable. However, there could be a module in the wild
    // which makes accounts bundleable, so let's not be presumptuous here.
    $this->isBundleable = (bool) $info['entity keys']['bundle'];
    $this->entityTypeLabel = $info['label'];
    $this->bundleLabel = $info['bundles'][$this->bundle]['label'];

    // An account can only have one profile of this type.
    $this->cardinality = 1;
    $this->fieldTypeLabel = t('Profile');

    // Load the Profile2 type information
    $this->profileType = profile2_get_types(subStr($this->name, 8));
    $this->label = $this->profileType->label;
  }

  /**
   * Implements CerField::getTargetType().
   */
  public function getTargetType() {

    // In effect, this "field" will "point to" Profile2 entities.
    return 'profile2';
  }

  /**
   * @override CerField::getTargetBundles().
   */
  public function getTargetBundles() {

    // This field can only "reference" one type of profile. There'll be
    // a separate instance of this plugin for each Profile2 type.
    return (array) $this->profileType->type;
  }

  /**
   * Implements CerEntityContainerInterface::createInnerEntity().
   */
  public function createInnerEntity(EntityDrupalWrapper $owner) {
    $init = array(
      'user' => $owner
        ->value(),
      'type' => $this->profileType,
    );

    // Create a blank profile for this user, and save it. The "reference" is implied
    // by the existence of the profile.
    $profile = profile2_create($init);
    $profile
      ->save();
    return $profile;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CerField::$fieldTypeLabel public property
CerField::$plugin protected property The plugin definition.
CerField::$settings protected property
CerField::getHandler public function Returns a CerFieldHandler subclass instance for the given entity.
CerField::getParents public function Overridden. Overrides FieldInstance::getParents
CerField::getPlugin public static function Returns a single field plugin instance, by its identifier. All plugin instances are statically cached.
CerField::getPluginInfo public static function Returns information about a particular field plugin by its identifier, or all available plugins (i.e., defined by hook_cer_fields()) if no identifier is given. The aggregated result of hook_cer_fields() is statically cached.
CerField::requireParent public function Overridden. Overrides FieldInstance::requireParent
CerProfile2Field::$profileType protected property
CerProfile2Field::createInnerEntity public function Implements CerEntityContainerInterface::createInnerEntity(). Overrides CerEntityContainerInterface::createInnerEntity
CerProfile2Field::getTargetBundles public function @override CerField::getTargetBundles(). Overrides CerField::getTargetBundles
CerProfile2Field::getTargetType public function Implements CerField::getTargetType(). Overrides CerField::getTargetType
CerProfile2Field::__construct public function @override CerField::__construct(). Overrides CerField::__construct
FieldInstance::$bundle public property The instance bundle.
FieldInstance::$bundleLabel public property The human-readable label of the instance's bundle.
FieldInstance::$cardinality public property The cardinality (maximum values) the field supports, or FIELD_CARDINALITY_UNLIMITED.
FieldInstance::$child protected property The child of this instance, if any.
FieldInstance::$entityType public property The instance's entity type.
FieldInstance::$entityTypeLabel public property The human-readable label of the instance's entity type.
FieldInstance::$isBundleable public property Whether or not this instance's entity type supports bundles.
FieldInstance::$label public property The instance's label.
FieldInstance::$name public property The field's machine name.
FieldInstance::$parent protected property The parent of this instance, if any.
FieldInstance::child public function Get or set the child of this field instance.
FieldInstance::parent public function Get or set the parent of this field instance.
FieldInstance::__toString public function