OpignoNotification.php in Opigno notifications 8
File
src/Entity/OpignoNotification.php
View source
<?php
namespace Drupal\opigno_notification\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\opigno_notification\OpignoNotificationInterface;
class OpignoNotification extends ContentEntityBase implements OpignoNotificationInterface {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the OpignoNotification entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the OpignoNotification entity.'))
->setReadOnly(TRUE);
$fields['created'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Creation time'))
->setDescription(t('The creation time of the notification.'))
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User'))
->setDescription(t('The user ID of the notification receiver.'))
->setRequired(TRUE)
->setSettings([
'default_value' => 0,
'target_type' => 'user',
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'entity_reference_label',
'weight' => 0,
]);
$fields['message'] = BaseFieldDefinition::create('string')
->setLabel(t('Message'))
->setDescription(t('The message of the notification.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 1,
]);
$fields['has_read'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Has Read'))
->setDescription(t('The status of the notification.'))
->setRequired(TRUE)
->setSettings([
'default_value' => 0,
]);
$fields['url'] = BaseFieldDefinition::create('string')
->setLabel(t('Url'))
->setDescription(t('The url string for notification entity.'))
->setSettings([
'max_length' => 50,
])
->setInitialValue('/notifications')
->setDefaultValue('/notifications');
return $fields;
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'created' => \Drupal::time()
->getRequestTime(),
'has_read' => FALSE,
];
}
public static function unreadCount($account = NULL) {
if ($account === NULL) {
$account = \Drupal::currentUser();
}
$query = \Drupal::entityQuery('opigno_notification');
$query
->condition('uid', $account
->id());
$query
->condition('has_read', FALSE);
$query
->count();
$result = $query
->execute();
return (int) $result;
}
public function getCreatedTime() {
$value = $this
->get('created')
->getValue();
if (!isset($value)) {
return NULL;
}
return $value[0]['value'];
}
public function getUser() {
$value = $this
->get('uid')
->getValue();
if (!isset($value)) {
return NULL;
}
return $value[0]['target_id'];
}
public function setUser($value) {
$this
->set('uid', $value);
return $this;
}
public function getMessage() {
$value = $this
->get('message')
->getValue();
if (!isset($value)) {
return NULL;
}
return $value[0]['value'];
}
public function setMessage($value) {
$this
->set('message', $value);
return $this;
}
public function getHasRead() {
$value = $this
->get('has_read')
->getValue();
if (!isset($value)) {
return NULL;
}
return $value[0]['value'];
}
public function setHasRead($value) {
$this
->set('has_read', $value);
return $this;
}
public function getUrl() {
$value = $this
->get('url')
->getValue();
if (!isset($value)) {
return '/notifications';
}
return $value[0]['value'];
}
public function setUrl($value) {
$this
->set('url', $value);
return $this;
}
}