entity_agree-2.0.x-dev/src/Entity/Agreement.php

src/Entity/Agreement.php
<?php

namespace Drupal\entity_agree\Entity;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_agree\AgreementInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the entity_agree_agreement entity class.
 *
 * @ContentEntityType(
 *   id = "entity_agree_agreement",
 *   label = @Translation("Agreement"),
 *   label_collection = @Translation("Agreements"),
 *   label_singular = @Translation("agreement"),
 *   label_plural = @Translation("agreements"),
 *   handlers = {
 *     "list_builder" = "Drupal\entity_agree\AgreementListBuilder",
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
 *     },
 *     "storage" = "Drupal\entity_agree\AgreementStorage",
 *     "views_data" = "Drupal\entity_agree\AgreementViewsData",
 *   },
 *   entity_keys = {
 *     "id" = "agreement_id",
 *     "uuid" = "uuid",
 *     "owner" = "uid",
 *     "uid" = "uid",
 *   },
 *   links = {
 *     "collection" = "/admin/people/agreements",
 *   },
 *   admin_permission =  "administer entity agreements",
 *   base_table = "entity_agree_agreement",
 * )
 */
class Agreement extends ContentEntityBase implements AgreementInterface {

  use EntityOwnerTrait;

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

    $fields['entity_type_id'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Entity type'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 63)
      ->setDisplayOptions('view', [
        'label' => 'inline',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('form', TRUE);

    $fields['entity_id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID of entity agreed to.'))
      ->setRequired(TRUE)
      ->setSetting('unsigned', TRUE);

    $fields['entity_vid'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Revision ID of entity agreed to.'))
      ->setRequired(TRUE)
      ->setSetting('unsigned', TRUE);

    $fields['uid']->setLabel(t('User'))
      ->setDescription(t('The agreeing user.'))
      ->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);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Agreed at'))
      ->setDescription(t('The time that the agreement was agreed to.'))
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'timestamp',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
        'type' => 'datetime_timestamp',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE);

    $fields['ip_address'] = BaseFieldDefinition::create('string')
      ->setLabel(t('IP address of the user agreeing.'))
      ->setSetting('max_length', 63)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    return $fields;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getTargetEntityTypeId() {
    return $this->entity_type_id->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetEntityId() {
    return $this->entity_id->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetEntityRevisionId() {
    return $this->entity_vid->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getListCacheTagsToInvalidate() {
    return Cache::mergeTags(parent::getListCacheTagsToInvalidate(), [
      "entity_agree:user:{$this->getOwnerId()}",
    ]);
  }

}

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

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