closedquestion-8.x-3.x-dev/src/Entity/ClosedQuestion.php

src/Entity/ClosedQuestion.php
<?php

namespace Drupal\closedquestion\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the closed question entity class.
 *
 * @ContentEntityType(
 *   id = "closedquestion",
 *   label = @Translation("Closed Question"),
 *   label_collection = @Translation("Closed Question"),
 *   label_singular = @Translation("closed question"),
 *   label_plural = @Translation("closed questions"),
 *   label_count = @PluralTranslation(
 *     singular = "@count closed question",
 *     plural = "@count closed questions"
 *   ),
 *   handlers = {
 *     "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
 *     "storage_schema" = "Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema",
 *     "view_builder" = "Drupal\closedquestion\ClosedQuestionViewBuilder",
 *     "access" = "Drupal\entity\EntityAccessControlHandler",
 *     "views_data" = "Drupal\closedquestion\ClosedQuestionViewsData",
 *     "form" = {
 *       "default" = "Drupal\closedquestion\Form\ClosedQuestionForm",
 *       "delete" = "Drupal\closedquestion\Form\ClosedQuestionDeleteForm",
 *       "edit" = "Drupal\closedquestion\Form\ClosedQuestionForm",
 *       "delete-multiple-confirm" = "Drupal\closedquestion\Form\ClosedQuestionDeleteMultiple"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\entity\Routing\DefaultHtmlRouteProvider",
 *     },
 *     "list_builder" = "Drupal\closedquestion\ClosedQuestionListBuilder",
 *     "translation" = "Drupal\closedquestion\ClosedQuestionTranslationHandler",
 *     "permission_provider" = "Drupal\entity\EntityPermissionProvider",
 *     "local_task_provider" = {
 *       "default" = "Drupal\entity\Menu\DefaultEntityLocalTaskProvider",
 *     },
 *     "local_action_provider" = {
 *       "collection" = "Drupal\closedquestion\Menu\ClosedQuestionCollectionLocalActionProvider",
 *     },
 *   },
 *   base_table = "cq",
 *   data_table = "cq__field_data",
 *   show_revision_ui = FALSE,
 *   translatable = TRUE,
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "title",
 *     "langcode" = "langcode",
 *     "uuid" = "uuid",
 *     "status" = "status",
 *     "published" = "status",
 *     "uid" = "uid",
 *     "owner" = "uid",
 *   },
 *   field_ui_base_route = "closedquestion.settings",
 *   common_reference_target = TRUE,
 *   permission_granularity = "entity_type",
 *   links = {
 *     "canonical" = "/closedquestion/{closedquestion}",
 *     "collection" = "/admin/content/closedquestion",
 *     "delete-form" = "/admin/content/closedquestion/{closedquestion}/delete",
 *     "delete-multiple-form" = "/admin/content/closedquestion/delete",
 *     "edit-form" = "/admin/content/closedquestion/{closedquestion}/edit",
 *     "add-form" = "/admin/content/closedquestion/add",
 *   }
 * )
 */
class ClosedQuestion extends ContentEntityBase implements ClosedQuestionInterface {

  use EntityChangedTrait;
  use EntityPublishedTrait;
  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    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.
      if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);

    // Add the published field.
    $fields += static::publishedBaseFieldDefinitions($entity_type);

    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setRequired(TRUE)
      ->setTranslatable(TRUE)
      ->setRevisionable(FALSE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['uid']
      ->setLabel(t('Authored by'))
      ->setDescription(t('The username of the closed question author.'))
      ->setRevisionable(FALSE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'author',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => 5,
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'placeholder' => '',
        ],
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['status']
      ->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 closed question was created.'))
      ->setRevisionable(FALSE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'timestamp',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
        'type' => 'datetime_timestamp',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

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

    return $fields;
  }

}

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

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