You are here

public static function InvoiceItem::baseFieldDefinitions in Commerce Invoice 8.2

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/InvoiceItem.php, line 361

Class

InvoiceItem
Defines the invoice item entity class.

Namespace

Drupal\commerce_invoice\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);

  // The invoice backreference, populated by Invoice::postSave().
  $fields['invoice_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Invoice'))
    ->setDescription(t('The parent invoice.'))
    ->setSetting('target_type', 'commerce_invoice')
    ->setReadOnly(TRUE);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The invoice item title.'))
    ->setSettings([
    'default_value' => '',
    'max_length' => 512,
  ])
    ->setTranslatable(TRUE)
    ->setRequired(TRUE);
  $fields['description'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Description'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'text_default',
    'weight' => 0,
  ])
    ->setTranslatable(TRUE);
  $fields['quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Quantity'))
    ->setDescription(t('The number of purchased units.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setSetting('min', 0)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'commerce_quantity',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['unit_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Unit price'))
    ->setDescription(t('The price of a single unit.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'commerce_unit_price',
    'weight' => 2,
    'settings' => [
      'require_confirmation' => FALSE,
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['total_price'] = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Total price'))
    ->setDescription(t('The total price of the invoice item.'))
    ->setReadOnly(TRUE);
  $fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
    ->setLabel(t('Adjustments'))
    ->setRequired(FALSE)
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('form', [
    'type' => 'commerce_adjustment_default',
    'weight' => 5,
  ]);
  $fields['data'] = BaseFieldDefinition::create('map')
    ->setLabel(t('Data'))
    ->setDescription(t('A serialized array of additional data.'));
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the invoice item was created.'))
    ->setRequired(TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the invoice item was last edited.'))
    ->setRequired(TRUE);
  $fields['order_item_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Order item'))
    ->setDescription(t('The reference to the order item.'))
    ->setSetting('target_type', 'commerce_order_item')
    ->setReadOnly(TRUE);
  return $fields;
}