You are here

public static function Transaction::baseFieldDefinitions in Transaction 8

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

src/Entity/Transaction.php, line 73

Class

Transaction
Provides the transaction content entity.

Namespace

Drupal\transaction\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  // Generic base fields.
  $fields = parent::baseFieldDefinitions($entity_type);

  // Creation timestamp.
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the transaction was created.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE);

  // User ID (transaction author).
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of the author.'))
    ->setRequired(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\transaction\\Entity\\Transaction::getCurrentUserId')
    ->setDisplayOptions('view', [
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE);

  // Target entity.
  $fields['target_entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel(t('Target entity'))
    ->setDescription(t('The target entity of the transaction.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // Operation.
  $fields['operation'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Operation'))
    ->setDescription(t('Reference to a transaction operation that describes the transaction.'))
    ->setRequired(FALSE)
    ->setSetting('target_type', 'transaction_operation')
    ->setSetting('handler', 'default:transaction_operation')
    ->setDisplayOptions('view', [
    'type' => 'entity_reference_label',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
    'settings' => [
      'size' => '60',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE);

  // Execution timestamp.
  $fields['executed'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Executed on'))
    ->setDescription(t('The time that the transaction was executed.'))
    ->setRequired(FALSE)
    ->setDisplayOptions('view', [
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // The execution sequence number.
  $fields['execution_sequence'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Execution sequence'))
    ->setDescription(t('The order number on which this transaction was executed.'))
    ->setSetting('unsigned', TRUE)
    ->setReadOnly(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'number_integer',
    'region' => 'hidden',
  ])
    ->setDisplayConfigurable('view', TRUE);

  // The execution result code.
  $fields['result_code'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Result code'))
    ->setReadOnly(TRUE)
    ->setRequired(TRUE);

  // The execution result message.
  $fields['result_message'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Result message'))
    ->setComputed(TRUE)
    ->setClass('\\Drupal\\transaction\\Plugin\\Field\\TransactionResultMessageItemList')
    ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // The user that executes the transaction.
  $fields['executor'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Executed by'))
    ->setDescription(t('The user ID of the user that executed the transaction.'))
    ->setRequired(FALSE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'type' => 'entity_reference_label',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // Properties.
  $fields['properties'] = BaseFieldDefinition::create('map')
    ->setLabel(t('Properties'))
    ->setDescription(t('A name-value map managed by the transactor plugin.'));

  // Description (computed).
  $fields['description'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Description'))
    ->setDescription(t('A human-readable description of the transaction.'))
    ->setComputed(TRUE)
    ->setClass('\\Drupal\\transaction\\Plugin\\Field\\TransactionDescriptionItemList')
    ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);

  // Details (computed).
  $fields['details'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Details'))
    ->setDescription(t('A list of details of the transaction.'))
    ->setComputed(TRUE)
    ->setClass('\\Drupal\\transaction\\Plugin\\Field\\TransactionDetailsItemList')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}