View source
<?php
namespace Drupal\push_notifications\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\push_notifications\PushNotificationsTokenInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
class PushNotificationsToken extends ContentEntityBase implements PushNotificationsTokenInterface {
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += array(
'uid' => \Drupal::currentUser()
->id(),
);
if (!array_key_exists('langcode', $values)) {
$values += array(
'langcode' => \Drupal::languageManager()
->getCurrentLanguage()
->getId(),
);
}
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function getToken() {
return $this
->get('token')->value;
}
public function getNetwork() {
return $this
->get('network')->value;
}
public function getLanguageCode() {
return $this
->get('langcode')->value;
}
public function getCreatedTimestamp() {
return $this
->get('created')->value;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function setNetwork($network) {
$this
->set('network', $network);
return $this;
}
public function getCreatedTime($type = 'short') {
return \Drupal::service('date.formatter')
->format($this
->getCreatedTimestamp(), $type);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('Push notifications ID.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the push notifications token.'))
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User Name'))
->setDescription(t('The token owner.'))
->setSetting('target_type', 'user')
->setDisplayOptions('view', array(
'label' => 'inline',
'type' => 'string',
'weight' => 0,
))
->setDisplayConfigurable('view', TRUE);
$fields['token'] = BaseFieldDefinition::create('string')
->setLabel(t('Token'))
->setDescription(t('Device Token'))
->setRequired(TRUE)
->setSettings(array(
'max_length' => 255,
))
->setDisplayOptions('view', array(
'label' => 'inline',
'type' => 'string',
'weight' => 1,
))
->setDisplayConfigurable('view', TRUE)
->addConstraint('PushNotificationsTokenUnique');
$fields['network'] = BaseFieldDefinition::create('string')
->setLabel(t('Network'))
->setDescription(t('Network Type'))
->setRequired(TRUE)
->setSettings(array(
'max_length' => 255,
))
->setDisplayOptions('view', array(
'label' => 'inline',
'type' => 'string',
'weight' => 2,
))
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Created'))
->setDescription(t('Timestamp the token was added.'))
->setRequired(TRUE)
->setDefaultValue(REQUEST_TIME)
->setDisplayOptions('view', array(
'label' => 'inline',
'type' => 'string',
'weight' => 3,
))
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('string')
->setLabel(t('Language'))
->setDescription(t('The language associated with this token.'))
->setSettings(array(
'max_length' => 255,
))
->setRequired(TRUE)
->addConstraint('PushNotificationsTokenLanguage')
->setDisplayOptions('view', array(
'label' => 'inline',
'type' => 'string',
'weight' => 4,
))
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}