You are here

public static function WishlistItem::baseFieldDefinitions in Commerce Wishlist 8.3

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/WishlistItem.php, line 262

Class

WishlistItem
Defines the wishlist item entity class.

Namespace

Drupal\commerce_wishlist\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['type']
    ->setSetting('max_length', EntityTypeInterface::BUNDLE_MAX_LENGTH)
    ->setSetting('is_ascii', TRUE);

  // The wishlist back reference, populated by Wishlist::postSave().
  $fields['wishlist_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Wishlist'))
    ->setDescription(t('The parent wishlist.'))
    ->setSetting('target_type', 'commerce_wishlist')
    ->setReadOnly(TRUE);
  $fields['purchasable_entity'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Purchasable entity'))
    ->setDescription(t('The purchasable entity.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // Provide a default target_type for Views, which uses
  // base field definitions without bundle overrides.
  if (\Drupal::moduleHandler()
    ->moduleExists('commerce_product')) {
    $fields['purchasable_entity']
      ->setSetting('target_type', 'commerce_product_variation');
  }
  $fields['comment'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Comment'))
    ->setDescription(t('The item comment.'))
    ->setDisplayOptions('form', [
    'type' => 'string_textarea',
    'weight' => 25,
    'settings' => [
      'rows' => 4,
    ],
  ])
    ->setDisplayOptions('view', [
    'type' => 'string',
    'label' => 'above',
    'settings' => [],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['priority'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Priority'))
    ->setDescription(t('The item priority.'))
    ->setDefaultValue(0);
  $fields['quantity'] = BaseFieldDefinition::create('decimal')
    ->setLabel(t('Quantity'))
    ->setDescription(t('The number of units.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setDefaultValue(1)
    ->setDisplayOptions('form', [
    'type' => 'number',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['purchases'] = BaseFieldDefinition::create('commerce_wishlist_purchase')
    ->setLabel(t('Purchases'))
    ->setDescription(t('The order ID, quantity and timestamp of each purchase.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the wishlist item was created.'))
    ->setRequired(TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the wishlist item was last edited.'))
    ->setRequired(TRUE);
  return $fields;
}