You are here

public function PurchasableEntityTypeRepository::getDefaultPurchasableEntityType in Commerce Core 8.2

Returns a sensible default purchasable entity type.

This is primarily needed to set an entity type to target in the base field definition for the purchasable entity field on order items. The core EntityReferenceItem field definition defaults the base field settings array to specify a target_type of node or user, and it is never overridden by bundle specific settings before the Views module uses that target_type to populate the list of view modes when rendering the purchasable entity reference field value as a "Rendered entity" in Views.

As such, our workaround is to determine a "default" purchasable entity type, privileging commerce_product_variation as the dominant use case if it exists and just selecting the first available entity type if not. Sites that need to set a specific default target_type can still do so by decorating the default service and overriding this method to return your purchasable entity type.

Return value

\Drupal\Core\Entity\EntityTypeInterface The default purchasable entity type definition.

Overrides PurchasableEntityTypeRepositoryInterface::getDefaultPurchasableEntityType

File

src/PurchasableEntityTypeRepository.php, line 48

Class

PurchasableEntityTypeRepository

Namespace

Drupal\commerce

Code

public function getDefaultPurchasableEntityType() {
  $purchasable_entity_types = $this
    ->getPurchasableEntityTypes();

  // Privilege commerce_product_variation as the default type if it exists.
  return $purchasable_entity_types['commerce_product_variation'] ?? reset($purchasable_entity_types);
}