public static function Invoice::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/ Invoice.php, line 684
Class
- Invoice
- Defines the invoice entity class.
Namespace
Drupal\commerce_invoice\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['invoice_number'] = BaseFieldDefinition::create('string')
->setLabel(t('Invoice number'))
->setDescription(t('The invoice number.'))
->setRequired(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayConfigurable('view', TRUE);
$fields['store_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Store'))
->setDescription(t('The store to which the invoice belongs.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('target_type', 'commerce_store')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Customer'))
->setDescription(t('The customer.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\commerce_invoice\\Entity\\Invoice::getCurrentUserId')
->setTranslatable(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['mail'] = BaseFieldDefinition::create('email')
->setLabel(t('Contact email'))
->setDescription(t('The email address associated with the invoice.'))
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
->setLabel(t('Billing information'))
->setDescription(t('Billing profile'))
->setSetting('target_type', 'profile')
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => [
'customer',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['orders'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Orders'))
->setDescription(t('The invoice orders.'))
->setRequired(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_order')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
$fields['invoice_items'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invoice items'))
->setDescription(t('The invoice items.'))
->setRequired(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_invoice_item')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'inline_entity_form_complex',
'weight' => 0,
'settings' => [
'allow_new' => FALSE,
],
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'commerce_invoice_item_table',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
->setLabel(t('Adjustments'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('form', [
'type' => 'commerce_adjustment_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$fields['payment_method'] = BaseFieldDefinition::create('string')
->setLabel(t('Payment method'))
->setDescription(t('The payment method.'))
->setRequired(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255);
$fields['total_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total price'))
->setDescription(t('The total price of the invoice.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'commerce_invoice_total_summary',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['total_paid'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total paid'))
->setDescription(t('The total paid price of the invoice.'))
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('state')
->setLabel(t('State'))
->setDescription(t('The invoice state.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', [
'\\Drupal\\commerce_invoice\\Entity\\Invoice',
'getWorkflowId',
]);
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setTranslatable(TRUE)
->setDescription(t('A serialized array of additional data.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the invoice was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the invoice was last edited.'));
$fields['invoice_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Invoice date'))
->setDescription(t('The invoice date'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['due_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Due date'))
->setDescription(t('The date the invoice is due.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['invoice_file'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invoice PDF'))
->setDescription(t('The invoice PDF file.'))
->setSetting('target_type', 'file')
->setDisplayConfigurable('view', TRUE);
return $fields;
}