Profile.php in Profile 8
File
src/Entity/Profile.php
View source
<?php
namespace Drupal\profile\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\profile\EntityOwnerTrait;
use Drupal\profile\Event\ProfileEvents;
use Drupal\profile\Event\ProfileLabelEvent;
class Profile extends EditorialContentEntityBase implements ProfileInterface {
use EntityOwnerTrait;
use StringTranslationTrait;
public function label() {
$profile_type = ProfileType::load($this
->bundle());
$label = $this
->t('@type #@id', [
'@type' => $profile_type
->getDisplayLabel() ?: $profile_type
->label(),
'@id' => $this
->id(),
]);
$event = new ProfileLabelEvent($this, $label);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->dispatch(ProfileEvents::PROFILE_LABEL, $event);
$label = $event
->getLabel();
return $label;
}
public function isActive() {
return $this
->isPublished();
}
public function setActive($active) {
if ((bool) $active) {
$this
->setPublished();
}
else {
$this
->setUnpublished();
}
return $this;
}
public function isDefault() {
return (bool) $this
->get('is_default')->value;
}
public function setDefault($is_default) {
$this
->set('is_default', (bool) $is_default);
return $this;
}
public function getData($key, $default = NULL) {
$data = [];
if (!$this
->get('data')
->isEmpty()) {
$data = $this
->get('data')
->first()
->getValue();
}
return isset($data[$key]) ? $data[$key] : $default;
}
public function setData($key, $value) {
$this
->get('data')
->__set($key, $value);
return $this;
}
public function unsetData($key) {
if (!$this
->get('data')
->isEmpty()) {
$data = $this
->get('data')
->first()
->getValue();
unset($data[$key]);
$this
->set('data', $data);
}
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getRevisionAuthor() {
return $this
->get('revision_uid')->entity;
}
public function setRevisionAuthorId($uid) {
$this
->set('revision_uid', $uid);
return $this;
}
public function equalToProfile(ProfileInterface $profile, array $field_names = []) {
$field_names = $field_names ?: $this
->getConfigurableFieldNames($profile);
foreach ($field_names as $field_name) {
$profile_field_item_list = $profile
->get($field_name);
if (!$this
->hasField($field_name) || !$this
->get($field_name)
->equals($profile_field_item_list)) {
return FALSE;
}
}
return TRUE;
}
public function populateFromProfile(ProfileInterface $profile, array $field_names = []) {
$field_names = $field_names ?: $this
->getConfigurableFieldNames($profile);
$profile_values = $profile
->toArray();
foreach ($field_names as $field_name) {
if (isset($profile_values[$field_name]) && $this
->hasField($field_name)) {
$this
->set($field_name, $profile_values[$field_name]);
}
}
return $this;
}
protected function getConfigurableFieldNames(ProfileInterface $profile) {
$field_names = [];
foreach ($profile
->getFieldDefinitions() as $field_name => $definition) {
if (!$definition instanceof BaseFieldDefinition) {
$field_names[] = $field_name;
}
}
return $field_names;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->isPublished()) {
$this
->setDefault(FALSE);
}
if ($this
->getOwnerId() && $this
->isPublished() && !$this
->isDefault()) {
$profile = $storage
->loadByUser($this
->getOwner(), $this
->bundle());
if (!$profile || !$profile
->isDefault()) {
$this
->setDefault(TRUE);
}
}
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if ($this
->getOwnerId()) {
$default = $this
->isDefault();
$original_default = $this->original ? $this->original
->isDefault() : FALSE;
if ($default && !$original_default) {
$profiles = $storage
->loadMultipleByUser($this
->getOwner(), $this
->bundle());
foreach ($profiles as $profile) {
if ($profile
->id() != $this
->id() && $profile
->isDefault()) {
$profile
->setDefault(FALSE);
$profile
->save();
}
}
}
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['uid']
->setRevisionable(TRUE)
->setLabel(t('Owner'))
->setDescription(t('The user that owns this profile.'))
->setSetting('handler', 'default');
$fields['status']
->setLabel(t('Active'))
->setDescription(t('Whether the profile is active.'))
->setTranslatable(FALSE);
$fields['is_default'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default'))
->setDescription(t('Whether this is the default profile.'))
->setDefaultValue(FALSE)
->setRevisionable(TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setDescription(t('A serialized array of additional data.'))
->setRevisionable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the profile was created.'))
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the profile was last edited.'))
->setRevisionable(TRUE);
return $fields;
}
}
Classes
Name |
Description |
Profile |
Defines the profile entity class. |