You are here

public static function Wishlist::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/Wishlist.php, line 391

Class

Wishlist
Defines the wishlist entity class.

Namespace

Drupal\commerce_wishlist\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['code'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Code'))
    ->setDescription(t('The wishlist code.'))
    ->setSetting('max_length', 25)
    ->addConstraint('UniqueField', []);
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The wishlist name.'))
    ->setRequired(TRUE)
    ->setDefaultValue('')
    ->setSetting('max_length', 255)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['uid']
    ->setLabel(t('Owner'))
    ->setDescription(t('The wishlist owner.'))
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['shipping_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
    ->setLabel(t('Shipping profile'))
    ->setDescription(t('Shipping profile'))
    ->setSetting('target_type', 'profile')
    ->setSetting('handler', 'default')
    ->setSetting('handler_settings', [
    'target_bundles' => [
      'customer',
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
    'settings' => [],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['wishlist_items'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Wishlist items'))
    ->setDescription(t('The wishlist items.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setSetting('target_type', 'commerce_wishlist_item')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'type' => 'commerce_wishlist_item_table',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['is_default'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Default'))
    ->setDescription(t('A boolean indicating whether the wishlist is the default one.'));
  $fields['is_public'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Public'))
    ->setDescription(t('Whether the wishlist is public.'))
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 19,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['keep_purchased_items'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Keep purchased items in the list'))
    ->setDescription(t('Whether items should remain in the wishlist once purchased.'))
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 20,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time when the wishlist was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time when the wishlist was last edited.'));
  return $fields;
}