public static function License::baseFieldDefinitions in Commerce License 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/ License.php, line 314
Class
- License
- Defines the License entity.
Namespace
Drupal\commerce_license\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['type']
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 0,
]);
$fields['uid']
->setLabel(t('Owner'))
->setDescription(t('The user ID of the license owner.'))
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => 2,
'settings' => [
'link' => TRUE,
],
])
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('state')
->setLabel(t('State'))
->setDescription(t('The license state.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 50,
])
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', [
'\\Drupal\\commerce_license\\Entity\\License',
'getWorkflowId',
]);
$fields['product_variation'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Licensed product variation'))
->setDescription(t('The licensed product variation.'))
->setRequired(TRUE)
->setSetting('target_type', 'commerce_product_variation')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => 1,
'settings' => [
'link' => TRUE,
],
])
->setDisplayConfigurable('view', TRUE);
$fields['expiration_type'] = BaseFieldDefinition::create('commerce_plugin_item:recurring_period')
->setLabel(t('Expiration type'))
->setDescription(t("The configuration for calculating the license's expiry."))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_plugin_select',
'weight' => 21,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'commerce_plugin_item_default',
'weight' => 25,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the license was created.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
// Start date-type weights at 20, to leave plenty of space for
// license type plugin fields to go before them.
'weight' => 20,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['granted'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Granted'))
->setDescription(t('The time that the license was first granted or activated.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 21,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['renewed'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Renewed'))
->setDescription(t('The time that the license was most recently renewed.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 22,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the license was last modified.'));
$fields['expires'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expires'))
->setDescription(t('The time that the license will expire, if any.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'commerce_license_expiration',
'weight' => 26,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE)
->setDefaultValue(0);
$fields['originating_order'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Originating order'))
->setDescription(t('The order that originated the license creation.'))
->setSetting('target_type', 'commerce_order')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
return $fields;
}