cc-1.0.x-dev/modules/cc_account/src/Entity/CcAccount.php

modules/cc_account/src/Entity/CcAccount.php
<?php

namespace Drupal\cc_account\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\user\EntityOwnerTrait;
use Drupal\user\UserInterface;

/**
 * Defines the cryptocurrency account entity class.
 *
 * @ContentEntityType(
 *   id = "cc_account",
 *   label = @Translation("Cryptocurrency Account"),
 *   label_collection = @Translation("Cryptocurrency Accounts"),
 *   label_singular = @Translation("cryptocurrency account"),
 *   label_plural = @Translation("cryptocurrency accounts"),
 *   label_count = @PluralTranslation(
 *     singular = "@count cryptocurrency account",
 *     plural = "@count cryptocurrency accounts",
 *   ),
 *   bundle_label = @Translation("Account type"),
 *   handlers = {
 *     "list_builder" = "Drupal\cc_account\CcAccountListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "access" = "Drupal\cc_account\Access\CcAccountAccessControlHandler",
 *     "form" = {
 *       "add" = "Drupal\cc_account\Form\CcAccountForm",
 *       "edit" = "Drupal\cc_account\Form\CcAccountForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
 *       "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "cc_account",
 *   admin_permission = "administer cc accounts",
 *   entity_keys = {
 *     "id" = "id",
 *     "bundle" = "type",
 *     "published" = "status",
 *     "address" = "address",
 *     "owner" = "uid",
 *     "label" = "label",
 *   },
 *   links = {
 *     "collection" = "/admin/content/accounts",
 *     "add-form" = "/account/add/{cc_account_type}",
 *     "add-page" = "/account/add",
 *     "canonical" = "/account/{cc_account}",
 *     "edit-form" = "/account/{cc_account}/edit",
 *     "delete-form" = "/account/{cc_account}/delete",
 *     "delete-multiple-form" = "/admin/content/accounts/delete-multiple",
 *   },
 *   bundle_entity_type = "cc_account_type",
 *   field_ui_base_route = "entity.cc_account_type.edit_form",
 * )
 */
class CcAccount extends ContentEntityBase implements CcAccountInterface {

  use EntityChangedTrait;
  use EntityPublishedTrait;
  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    if (!$this->getOwnerId()) {
      $this->setOwnerId(0);
    }
    if (!$this->getLabel()) {
      $this->setLabel();
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function setLabel() {
    $address = $this->get('address')->value;
    if (strlen($address) > 20) {
      $short_address = substr($address, 0, 10) . '...' . substr($address, -6);
      $this->set('label', $short_address);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getAddress(): string {
    return $this->get('address')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getTarget(): string {
    return $this->get('qr_target')->value;
  }

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

    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Label'))
      ->setDescription(t('An optional human-friendly label of the account (leave empty to generate a shortened address).'))
      ->setRequired(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['network'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Network'))
      ->setDescription(t('The network/authority on/under which this account lives.'))
      ->setRequired(FALSE)
      ->setCardinality(1)
      ->setSetting('target_type', 'taxonomy_term')
      ->setSetting('handler_settings', [
        'target_bundles' => ['network' => 'network'],
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['address'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Address'))
      ->setDescription(t('The blockchain address of the cryptocurrency account.'))
      ->setRequired(TRUE)
      ->setReadOnly(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -3,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->addConstraint('UniqueField');

    $fields['balance'] = BaseFieldDefinition::create('decimal')
      ->setLabel(t('Balance'))
      ->setSettings([
        'type' => 'numeric',
        'unsigned' => TRUE,
        'min' => 0,
        'precision' => 39,
        'scale' => 0,
        'not null' => FALSE,
        'description' => t('The native coin balance in base denomination (integer with max 39 digits)'),
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['receivable'] = BaseFieldDefinition::create('decimal')
      ->setLabel(t('Receivable'))
      ->setSettings([
        'type' => 'numeric',
        'unsigned' => TRUE,
        'min' => 0,
        'precision' => 39,
        'scale' => 0,
        'not null' => FALSE,
        'description' => t('The native coin receivable amount in base denomination (integer with max 39 digits)'),
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['qr_target'] = BaseFieldDefinition::create('string')
      ->setLabel(t('QR Target'))
      ->setSetting('max_length', 1855)
      ->setDescription(t('Optional qrcode target for deep links or preconfigured transactions, eg. payto:[network]/[address]?amount=1000).'))
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -3,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the cryptocurrency account was created.'));

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the cryptocurrency account was last edited.'));

    return $fields;
  }

}

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

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