View source
<?php
namespace Drupal\invite\Entity;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Database;
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\invite\InviteInterface;
use Drupal\user\UserInterface;
class Invite extends ContentEntityBase implements InviteInterface {
use EntityChangedTrait;
protected $plugin;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
do {
$reg_code = user_password(10);
$result = Database::getConnection()
->query('SELECT reg_code FROM {invite} WHERE reg_code = :reg_code', [
':reg_code' => $reg_code,
])
->fetchField();
} while ($result !== FALSE);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
'created' => \Drupal::time()
->getRequestTime(),
'expires' => \Drupal::time()
->getRequestTime() + \Drupal::config('invite.invite_config')
->get('invite_expiration') * 24 * 60 * 60,
'invitee' => 0,
'type' => !empty($values['type']) ? $values['type'] : '',
'status' => 1,
'reg_code' => $reg_code,
'data' => [],
];
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!empty($this->plugin)) {
$plugin_manager = \Drupal::service('plugin.manager.invite');
$plugin = $plugin_manager
->createInstance($this->plugin);
$plugin
->send($this);
}
}
public function setPlugin($plugin) {
$this->plugin = $plugin;
}
public function getPlugin() {
return $this->plugin;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function getInvitee() {
return $this
->get('invitee')->entity;
}
public function setJoined($time) {
$this
->set('joined', $time);
return $this;
}
public function getStatus() {
return $this
->get('status')->value;
}
public function setStatus($status) {
$this
->set('status', $status);
return $this;
}
public function setInvitee(UserInterface $account) {
$this
->set('invitee', $account
->id());
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getRegCode() {
return $this
->get('reg_code')->value;
}
public function isPublished() {
return (bool) $this
->getEntityKey('status');
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Invite entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Invite entity.'))
->setReadOnly(TRUE);
$fields['reg_code'] = BaseFieldDefinition::create('string')
->setLabel(t('Registration Code'))
->setDescription(t('The invite registration code.'))
->setSettings([
'max_length' => 10,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Invite Type'))
->setDescription(t('The invite type.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Invite entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\node\\Entity\\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'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['invitee'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invitee'))
->setDescription(t('The user id of the person being invited.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'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['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The Unix timestamp when the invite was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['expires'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expiration'))
->setDescription(t('The Unix timestamp when the invite will expire.'));
$fields['joined'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Joined'))
->setDescription(t('Will be filled with the time the invite was accepted upon registration.'));
$fields['data'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Data'))
->setDescription(t('Stores axiliary data.'));
$fields['status'] = BaseFieldDefinition::create('integer')
->setLabel(t('Expiration'))
->setDescription(t('Invitation status'))
->setSettings([
'max_length' => 11,
'text_processing' => 0,
]);
return $fields;
}
}