devel_wizard-2.x-dev/templates/spell/entity_type/content/class.php.twig

templates/spell/entity_type/content/class.php.twig
{%
  include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
  with {
    'namespace': module.namespace ~ '\\Entity',
  }
%}

use {{ content.interface_fqn }};
{%-
  include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
  with {
    'useStatements': {
      'Drupal\\Core\\Entity\\EditorialContentEntityBase': '',
      'Drupal\\Core\\Entity\\EntityStorageInterface': '',
      'Drupal\\Core\\Entity\\EntityTypeInterface': '',
      'Drupal\\Core\\Field\\BaseFieldDefinition': '',
      'Drupal\\user\\EntityOwnerTrait': '',
    },
  }
%}

/**
 * @ContentEntityType(
 *   id = "{{ content.id }}",
 *   label            = @Translation("{{ content.label }}"),
 *   label_collection = @Translation("{{ content.label }}"),
 *   label_singular   = @Translation("{{ content.label }}"),
 *   label_plural     = @Translation("{{ content.label_plural }}"),
 *   label_count = @PluralTranslation(
 *     singular = "@count {{ content.label }}",
 *     plural   = "@count {{ content.label_plural }}",
 *   ),
{% if goal == 'bundleable' %}
 *   bundle_entity_type = "{{ config.id }}",
 *   bundle_label = @Translation("{{ config.label }}"),
{% endif %}
 *   handlers = {
 *     "storage"         = "{{ content.namespace }}\Storage",
 *     "storage_schema"  = "{{ content.namespace }}\StorageSchema",
 *     "access"          = "{{ content.namespace }}\AccessControlHandler",
 *     "translation"     = "{{ content.namespace }}\TranslationHandler",
 *     "view_builder"    = "{{ content.namespace }}\ViewBuilder",
 *     "view_controller" = "{{ content.namespace }}\ViewController",
 *     "list_builder"    = "{{ content.namespace }}\ListBuilder",
 *     "views_data"      = "{{ content.namespace }}\ViewsData",
 *     "form" = {
 *       "default" = "{{ content.namespace }}\AddForm",
 *       "add"     = "{{ content.namespace }}\AddForm",
 *       "edit"    = "{{ content.namespace }}\EditForm",
 *       "delete"  = "{{ content.namespace }}\DeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "{{ content.namespace }}\RouteProvider",
 *     },
 *   },
 *   base_table          = "{{ content.id }}_c_base",
 *   data_table          = "{{ content.id }}_c_data",
 *   revision_table      = "{{ content.id }}_r_base",
 *   revision_data_table = "{{ content.id }}_r_data",
 *   show_revision_ui = TRUE,
 *   fieldable = TRUE,
{% if goal == 'bundleable' %}
 *   field_ui_base_route = "entity.{{ config.id }}.edit_form",
{% else %}
 *   field_ui_base_route = "entity.{{ content.id }}.settings_form",
{% endif %}
 *   translatable = TRUE,
 *   common_reference_target = FALSE,
 *   admin_permission = "{{ module.machineName }}.{{ content.id }}._all",
 *   permission_granularity = "{{ goal == 'bundleable' ? 'bundle' : 'entity_type' }}",
 *   entity_keys = {
 *     "uuid"                          = "uuid",
 *     "id"                            = "id",
 *     "revision"                      = "revision_id",
 *     "bundle"                        = "{{ goal == 'bundleable' ? 'bundle' : '' }}",
 *     "langcode"                      = "langcode",
 *     "default_langcode"              = "default_langcode",
 *     "revision_translation_affected" = "revision_translation_affected",
 *     "label"                         = "label",
 *     "uid"                           = "uid",
 *     "owner"                         = "uid",
 *     "status"                        = "status",
 *     "published"                     = "published",
 *   },
 *   revision_metadata_keys = {
 *     "revision_created"     = "revision_created",
 *     "revision_user"        = "revision_uid",
 *     "revision_log_message" = "revision_log",
 *     "revision_default"     = "revision_default",
 *   },
 *   links = {
 *     "collection"                       = "/admin/content/{{ content.id }}",
 *     "create"                           = "/admin/content/{{ content.id }}/add",
 *     "canonical"                        = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}",
 *     "edit-form"                        = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/edit",
 *     "delete-form"                      = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/delete",
 *     "revision-history"                 = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/revision",
 *     "revision-view"                    = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/revision/{{ '{' }}{{ content.id }}_revision{{ '}' }}",
 *     "revision-revert-form"             = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/revision/{{ '{' }}{{ content.id }}_revision{{ '}' }}/revert",
 *     "revision-revert-translation-form" = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/revision/{{ '{' }}{{ content.id }}_revision{{ '}' }}/revert/{langcode}",
 *     "revision-delete-form"             = "/admin/content/{{ content.id }}/manage/{{ '{' }}{{ content.id }}{{ '}' }}/revision/{{ '{' }}{{ content.id }}_revision{{ '}' }}/delete",
 *   }
 * )
 */
class {{ content.class }} extends EditorialContentEntityBase implements {{ content.interface }} {

  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   *
   * @return array{{ '<' }}string, \Drupal\Core\Field\FieldDefinitionInterface>
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
    /** @var array{{ '<' }}string, \Drupal\Core\Field\BaseFieldDefinition> $fields */
    $fields = parent::baseFieldDefinitions($entity_type);

    if ($entity_type->hasKey('label')) {
      $fields[$entity_type->getKey('label')] = BaseFieldDefinition::create('string')
        ->setLabel(t('Label'))
        ->setRequired(TRUE)
        ->setTranslatable(TRUE)
        ->setRevisionable(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);
    }

    /* @noinspection PhpUnhandledExceptionInspection */
    $fields += static::ownerBaseFieldDefinitions($entity_type);
    $key = $entity_type->getKey('owner');
    $fields[$key]
      ->setLabel(t('Authored by'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions(
        'form',
        [
          'type' => 'entity_reference_autocomplete',
          'weight' => 5,
          'settings' => [
            'match_operator' => 'CONTAINS',
            'size' => '60',
            'placeholder' => '',
          ],
        ],
      )
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions(
        'view',
        [
          'label' => 'hidden',
          'type' => 'author',
          'weight' => 0,
        ],
      )
      ->setDisplayConfigurable('view', TRUE);

    $key = $entity_type->getKey('status');
    $fields[$key] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Status'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDefaultValue(TRUE)
      ->setDisplayOptions(
        'form',
        [
          'type' => 'boolean_checkbox',
          'settings' => [
            'display_label' => TRUE,
          ],
          'weight' => 120,
        ],
      )
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the {{ content.id }} was created.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions(
        'form',
        [
          'type' => 'datetime_timestamp',
          'weight' => 10,
        ],
      )
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions(
        'view',
        [
          'label' => 'hidden',
          'type' => 'timestamp',
          'weight' => 0,
        ],
      )
      ->setDisplayConfigurable('view', TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the {{ content.id }} was last edited.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);

    /* @noinspection PhpUnhandledExceptionInspection */
    $fields += static::revisionLogBaseFieldDefinitions($entity_type);

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage): void {
    parent::preSave($storage);

    foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
      $translation = $this->getTranslation($langcode);

      // If no owner has been set explicitly, make the anonymous user the owner.
      // @phpstan-ignore-next-line
      if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }

    // If no revision author has been set explicitly, make the entity owner the
    // revision author.
    if (!$this->getRevisionUser()) {
      $this->setRevisionUserId($this->getOwnerId());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record): void {
    parent::preSaveRevision($storage, $record);

    if (!$this->isNewRevision()
      && isset($this->original)
      && (!isset($record->revision_log) || $record->revision_log === '')
    ) {
      // If we are updating an existing instance without adding a new revision,
      // we need to make sure $entity->revision_log is reset whenever it is empty.
      // Therefore, this code allows us to avoid clobbering an existing log
      // entry with an empty one.
      //
      // @todo Find an alternative for ::$revision_log.
      // @phpstan-ignore-next-line
      $record->revision_log = $this->original->revision_log->value;
    }
  }

  public function getCreatedTime(): int {
    return $this->get('created')->value;
  }

  public function setCreatedTime(int $timestamp): static {
    $this->set('created', $timestamp);

    return $this;
  }

}

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

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