notification_popin-1.2.2/src/Entity/Notification.php
src/Entity/Notification.php
<?php declare(strict_types = 1);
namespace Drupal\notification_popin\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\notification_popin\NotificationInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the notification entity class.
*
* @ContentEntityType(
* id = "notification",
* label = @Translation("Notification"),
* label_collection = @Translation("Notifications"),
* label_singular = @Translation("notification"),
* label_plural = @Translation("notifications"),
* label_count = @PluralTranslation(
* singular = "@count notifications",
* plural = "@count notifications",
* ),
* handlers = {
* "list_builder" = "Drupal\notification_popin\NotificationListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\notification_popin\Form\NotificationForm",
* "edit" = "Drupal\notification_popin\Form\NotificationForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* },
* },
* base_table = "notification",
* admin_permission = "administer notification",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* "owner" = "uid",
* },
* links = {
* "collection" = "/admin/content/notification",
* "add-form" = "/notification/add",
* "canonical" = "/notification/{notification}",
* "edit-form" = "/notification/{notification}/edit",
* "delete-form" = "/notification/{notification}/delete",
* "delete-multiple-form" = "/admin/content/notification/delete-multiple",
* },
* field_ui_base_route = "entity.notification.settings",
* )
*/
final class Notification extends ContentEntityBase implements NotificationInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage): void {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'text_default',
'label' => 'hidden',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the notification was created.'))
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 30,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the notification was last edited.'));
$customFields = self::_customFieldDefinitions();
return $fields + $customFields;
}
private static function _customFieldDefinitions(): array {
$fields = [];
$fields['start_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Start date'))
->setDescription(t('Date from which the notification will be displayed (optional).'))
->setDisplayOptions('form', [
'weight' => 11,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['end_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('End date'))
->setDescription(t('Date from which the notification will no longer be displayed (optional).'))
->setDisplayOptions('form', [
'weight' => 12,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['roles'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Roles'))
->setSetting('target_type', 'user_role')
->setCardinality(-1)
->setDescription(t('User roles which will see the notification (if none, all users will see the notification).'))
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 13,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['content_types'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Content types'))
->setSetting('target_type', 'node_type')
->setCardinality(-1)
->setDescription(t('Content types on which display the notification (if none, all content types will display the notification).'))
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 14,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['pages'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Pages'))
->setDescription(t('Page path (on per line) on which display the notification (if empty, no page filter will be applied).'))
->setDisplayOptions('form', [
'type' => 'string_long',
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}
