decoupled_json_log-1.0.x-dev/src/Entity/LogJson.php

src/Entity/LogJson.php
<?php

declare(strict_types=1);

namespace Drupal\decoupled_json_log\Entity;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\decoupled_json_log\LogJsonListBuilder;
use Drupal\views\EntityViewsData;
use Drupal\decoupled_json_log\LogJsonAccessControlHandler;
use Drupal\decoupled_json_log\Form\LogJsonForm;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\Form\DeleteMultipleForm;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\decoupled_json_log\LogJsonInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the json log entity class.
 */
#[ContentEntityType(
  id: 'log_json',
  label: new TranslatableMarkup('JSON Log'),
  label_collection: new TranslatableMarkup('JSON Logs'),
  label_singular: new TranslatableMarkup('json log'),
  label_plural: new TranslatableMarkup('json logs'),
  entity_keys: [
    'id' => 'id',
    'bundle' => 'bundle',
    'label' => 'label',
    'uuid' => 'uuid',
    'owner' => 'uid',
  ],
  handlers: [
    'list_builder' => LogJsonListBuilder::class,
    'views_data' => EntityViewsData::class,
    'access' => LogJsonAccessControlHandler::class,
    'form' => [
      'add' => LogJsonForm::class,
      'edit' => LogJsonForm::class,
      'delete' => ContentEntityDeleteForm::class,
      'delete-multiple-confirm' => DeleteMultipleForm::class,
    ],
    'route_provider' => ['html' => AdminHtmlRouteProvider::class],
  ],
  links: [
    'collection' => '/admin/content/log-json',
    'add-form' => '/log-json/add/{log_json_type}',
    'add-page' => '/log-json/add',
    'canonical' => '/log-json/{log_json}',
    'edit-form' => '/log-json/{log_json}/edit',
    'delete-form' => '/log-json/{log_json}/delete',
    'delete-multiple-form' => '/admin/content/log-json/delete-multiple',
  ],
  admin_permission: 'administer log_json types',
  bundle_entity_type: 'log_json_type',
  bundle_label: new TranslatableMarkup('JSON Log type'),
  base_table: 'log_json',
  label_count: [
    'singular' => '@count json logs',
    'plural' => '@count json logs',
  ],
  field_ui_base_route: 'entity.log_json_type.edit_form',
  constraints: ['RateLimitPerUser' => []]
)]
final class LogJson extends ContentEntityBase implements LogJsonInterface {

  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function preSave(EntityStorageInterface $storage): void {
    parent::preSave($storage);
    if ($this->getOwnerId() === NULL) {
      // If no owner has been set explicitly, make the anonymous user the owner.
      $this->setOwnerId(0);
    }
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  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['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Author'))
      ->setSetting('target_type', 'user')
      ->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'author',
        'weight' => 15,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the json log was created.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'timestamp',
        'weight' => 20,
      ])
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['log'] = BaseFieldDefinition::create('json')
      ->setLabel(t('Log entry'))
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'text_textarea',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'type' => 'text_default',
        'label' => 'hidden',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['device_info'] = BaseFieldDefinition::create('json')
      ->setLabel(t('Created device'))
      ->setDescription(t('Tracked information about the device that created the log entry.'))
      ->setDisplayOptions('form', [
        'type' => 'text_textarea',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'type' => 'text_default',
        'label' => 'hidden',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    return $fields;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc