View source
<?php
namespace Drupal\redhen_contact\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\redhen_contact\ContactInterface;
use Drupal\user\UserInterface;
class Contact extends ContentEntityBase implements ContactInterface {
use EntityChangedTrait;
public function label() {
return $this
->getFullName();
}
public function getType() {
return $this
->bundle();
}
public function getFullName() {
$first_name = $this
->get('first_name')->value;
$middle_name = $this
->get('middle_name')->value;
$last_name = $this
->get('last_name')->value;
$name = $first_name . (empty($middle_name) ? '' : ' ') . $middle_name . (empty($first_name) ? '' : ' ') . $last_name;
\Drupal::moduleHandler()
->alter('redhen_contact_name', $name, $this);
return $name;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getEmail() {
return $this
->get('email')->value;
}
public function setEmail($email) {
$this
->set('email', $email);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getUser() {
return $this
->get('uid')->entity;
}
public function getUserId() {
return $this
->get('uid')->target_id;
}
public function setUserId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setUser(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function getOwner() {
return $this
->getUser();
}
public function getOwnerId() {
return $this
->getUserId();
}
public function setOwnerId($uid) {
$this
->setUserId($uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->setOwnerId($account
->id());
return $this;
}
public function isActive() {
return (bool) $this
->getEntityKey('status');
}
public function setActive($active) {
$this
->set('status', $active ? REDHEN_CONTACT_INACTIVE : REDHEN_CONTACT_ACTIVE);
return $this;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$config = \Drupal::config('redhen_contact.settings');
$user = $this
->getUser();
$email = $this
->getEmail();
if ($config
->get('connect_users') && !$user && $email) {
$user = user_load_by_mail($email);
if ($user) {
$this
->setUser($user);
}
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$config = \Drupal::config('redhen_contact.settings');
$user = $this
->getUser();
$email = $this
->getEmail();
if ($config
->get('connect_users') && $user && $email) {
$user
->setEmail($email);
$user
->save();
}
}
public static function loadByUser($account, $status = TRUE) {
$contact =& drupal_static(__FUNCTION__ . $account
->id(), FALSE);
if (!$contact && !empty($account
->id())) {
$query = \Drupal::entityQuery('redhen_contact');
$query
->condition('uid', $account
->id(), '=');
$query
->condition('status', $status);
$results = $query
->execute();
if (!empty($results)) {
$contact = Contact::load(reset($results));
}
}
return $contact;
}
public static function loadByMail($email, $status = TRUE) {
$contacts =& drupal_static(__FUNCTION__ . $email, FALSE);
if (!$contacts) {
$query = \Drupal::entityQuery('redhen_contact');
$query
->condition('email', $email, '=');
$query
->condition('status', $status);
$results = $query
->execute();
if (!empty($results)) {
$contacts = Contact::loadMultiple($results);
}
}
return $contacts;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$config = \Drupal::config('redhen_contact.settings');
$fields = parent::baseFieldDefinitions($entity_type);
$required_names = $config
->get('required_properties');
$fields['first_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First Name'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired($required_names['first_name'])
->setRevisionable(TRUE);
$fields['middle_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Middle Name'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -9,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired($required_names['middle_name'])
->setRevisionable(TRUE);
$fields['last_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Last Name'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -8,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired($required_names['last_name'])
->setRevisionable(TRUE);
$fields['email'] = BaseFieldDefinition::create('email')
->setLabel(t('Email'))
->setDefaultValue('')
->addConstraint('ContactEmailUnique')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'email_mailto',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRevisionable(TRUE)
->setRequired($config
->get('valid_email'));
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Linked user'))
->setDescription(t('The Drupal user this contact is linked to.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setTranslatable(FALSE)
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'settings' => [
'link' => TRUE,
],
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Active'))
->setDescription(t('A boolean indicating whether the Contact is active.'))
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 16,
])
->setDisplayConfigurable('form', TRUE)
->setRevisionable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the contact was created.'))
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the contact was last edited.'))
->setRevisionable(TRUE);
return $fields;
}
}