class Profile in Profile 2 7
Same name and namespace in other branches
- 7.2 profile2.module \Profile
The class used for profile entities.
Hierarchy
- class \Entity implements EntityInterface
- class \Profile
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Entity:: |
protected | property | 1 | |
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
public | function |
Returns the bundle of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Permanently deletes the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the info of the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Exports the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Gets the raw, translated value of a property or field. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks if the entity has a certain exportable status. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the internal, numeric identifier. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks whether the entity is the default revision. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the label of the entity. Overrides EntityInterface:: |
|
Entity:: |
protected | function | Set up the object instance on construction or unserializiation. | |
Entity:: |
public | function |
Returns the uri of the entity just as entity_uri(). Overrides EntityInterface:: |
|
Entity:: |
public | function |
Generate an array for rendering the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function | Magic method to only serialize what's necessary. | |
Entity:: |
public | function | Magic method to invoke setUp() on unserialization. | |
Profile:: |
public | property | The Unix timestamp when the profile was most recently saved. | |
Profile:: |
public | property | The Unix timestamp when the profile was created. | |
Profile:: |
public | property | The profile label. | |
Profile:: |
public | property | The profile id. | |
Profile:: |
public | property | The name of the profile type. | |
Profile:: |
public | property | The user id of the profile owner. | |
Profile:: |
public | function |
Builds a structured array representing the entity's content. Overrides Entity:: |
|
Profile:: |
public | function |
Defines the entity label if the 'entity_class_label' callback is used. Overrides Entity:: |
|
Profile:: |
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:: |
|
Profile:: |
public | function | Returns the drupal path to this profile. | |
Profile:: |
public | function |
Permanently saves the entity. Overrides Entity:: |
|
Profile:: |
public | function | Sets a new user owning this profile. | |
Profile:: |
public | function | Gets the associated profile type object. | |
Profile:: |
public | function | Returns the full url() for the profile. | |
Profile:: |
public | function | Returns the user owning this profile. | |
Profile:: |
public | function |
Overrides Entity:: |