You are here

class Profile in Profile 2 7

Same name and namespace in other branches
  1. 7.2 profile2.module \Profile

The class used for profile entities.

Hierarchy

Expanded class hierarchy of Profile

2 string references to 'Profile'
hook_default_profile2_type in ./profile2.api.php
Define default profile type configurations.
profile2_entity_info in ./profile2.module
Implements hook_entity_info().

File

./profile2.module, line 867
Support for configurable user profiles.

View source
class Profile extends Entity {

  /**
   * The profile id.
   *
   * @var integer
   */
  public $pid;

  /**
   * The name of the profile type.
   *
   * @var string
   */
  public $type;

  /**
   * The profile label.
   *
   * @var string
   */
  public $label;

  /**
   * The user id of the profile owner.
   *
   * @var integer
   */
  public $uid;

  /**
   * The Unix timestamp when the profile was created.
   *
   * @var integer
   */
  public $created;

  /**
   * The Unix timestamp when the profile was most recently saved.
   *
   * @var integer
   */
  public $changed;
  public function __construct($values = array()) {
    if (isset($values['user'])) {
      $this
        ->setUser($values['user']);
      unset($values['user']);
    }
    if (isset($values['type']) && is_object($values['type'])) {
      $values['type'] = $values['type']->type;
    }
    if (!isset($values['label']) && isset($values['type']) && ($type = profile2_get_types($values['type']))) {

      // Initialize the label with the type label, so newly created profiles
      // have that as interim label.
      $values['label'] = $type->label;
    }
    parent::__construct($values, 'profile2');
  }

  /**
   * Returns the user owning this profile.
   */
  public function user() {
    return user_load($this->uid);
  }

  /**
   * Sets a new user owning this profile.
   *
   * @param $account
   *   The user account object or the user account id (uid).
   */
  public function setUser($account) {
    $this->uid = is_object($account) ? $account->uid : $account;
  }

  /**
   * Gets the associated profile type object.
   *
   * @return ProfileType
   */
  public function type() {
    return profile2_get_types($this->type);
  }

  /**
   * Returns the full url() for the profile.
   */
  public function url() {
    $uri = $this
      ->uri();
    return url($uri['path'], $uri);
  }

  /**
   * Returns the drupal path to this profile.
   */
  public function path() {
    $uri = $this
      ->uri();
    return $uri['path'];
  }
  public function defaultUri() {
    return array(
      'path' => 'user/' . $this->uid,
      'options' => array(
        'fragment' => 'profile-' . $this->type,
      ),
    );
  }
  public function defaultLabel() {
    $type = profile2_get_types($this->type);
    if (!empty($type->data['edit_label'])) {
      return $this->label;
    }
    else {

      // Return a label that combines the type name and user name, translatable.
      return t('@type profile for @user', array(
        '@type' => $type
          ->getTranslation('label'),
        '@user' => format_username($this
          ->user()),
      ));
    }
  }
  public function buildContent($view_mode = 'full', $langcode = NULL) {
    $content = array();

    // Assume newly create objects are still empty.
    if (!empty($this->is_new)) {
      $content['empty']['#markup'] = '<em class="profile2-no-data">' . t('There is no profile data yet.') . '</em>';
    }
    return entity_get_controller($this->entityType)
      ->buildContent($this, $view_mode, $langcode, $content);
  }
  public function save() {

    // Don't create a new profile if the user already has one of the same type.
    $existing_profile = profile2_load_by_user($this->uid, $this->type);
    if (empty($this->pid) && !empty($existing_profile)) {
      watchdog('profile2_create', '@type profile already exists for user @uid', array(
        '@uid' => $this->uid,
        '@type' => $this->type,
      ), WATCHDOG_WARNING);
      return;
    }

    // Care about setting created and changed values. But do not automatically
    // set a created values for already existing profiles.
    if (empty($this->created) && (!empty($this->is_new) || !$this->pid)) {
      $this->created = REQUEST_TIME;
    }
    $this->changed = REQUEST_TIME;

    // Clear the static cache from profile2_load_by_user() before saving, so
    // that profiles are correctly loaded in insert/update hooks.
    $cache =& drupal_static('profile2_load_by_user', array());
    unset($cache[$this->uid]);
    return parent::save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
Profile::$changed public property The Unix timestamp when the profile was most recently saved.
Profile::$created public property The Unix timestamp when the profile was created.
Profile::$label public property The profile label.
Profile::$pid public property The profile id.
Profile::$type public property The name of the profile type.
Profile::$uid public property The user id of the profile owner.
Profile::buildContent public function Builds a structured array representing the entity's content. Overrides Entity::buildContent
Profile::defaultLabel public function Defines the entity label if the 'entity_class_label' callback is used. Overrides Entity::defaultLabel
Profile::defaultUri public function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info(). Overrides Entity::defaultUri
Profile::path public function Returns the drupal path to this profile.
Profile::save public function Permanently saves the entity. Overrides Entity::save
Profile::setUser public function Sets a new user owning this profile.
Profile::type public function Gets the associated profile type object.
Profile::url public function Returns the full url() for the profile.
Profile::user public function Returns the user owning this profile.
Profile::__construct public function Overrides Entity::__construct