To store the field data in a separate database table, you need to override ContentEntityBase::bundleFieldDefinitions()
, which is the method for adding bundle fields or overriding base fields. That does not allow to decide the database table name, but only to avoid using a shared database table that otherwise Drupal would use.
For an entity that depends from the Entity API module, Defining bundle fields in code shows how to define bundle fields for an entity. In particular, it shows how to define the storage for the added bundle fields.
Supposing that the entity class is MyEntity
and that the machine name of the module that implements that class is mymodule, the code to use in your case would be the following one.
/**
* Entity class.
*/
class MyEntity /* extends … */ {
/**
* Class properties and other methods
*/
/**
* {@inheritdoc}
*/
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
if ($bundle == 'mybundle') {
$fields = parent::bundleFieldDefinitions($entity_type, $bundle, array $base_field_definitions);
$fields['shipping_address'] = BundleFieldDefinition::create('address')
->setLabel(t('Shipping Address'))
->setDescription(t('The shipping address.'))
->setCardinality(1)
->setSetting('field_overrides', [AddressField::ADDITIONAL_NAME => ['override' => FieldOverride::HIDDEN]])
->setDisplayOptions('form', ['type' => 'address_default', 'weight' => 4])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}
/**
* Other methods
*/
}
/**
* mymodule.module
*/
/**
* Implements hook_entity_field_storage_info().
*/
function mymodule_entity_field_storage_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'myentity') {
return MyEntity::bundleFieldDefinitions($entity_type, 'mybundle', []);
}
}