You are here

public static function OrderItem::bundleFieldDefinitions in Commerce Core 8.2

Provides field definitions for a specific bundle.

This function can return definitions both for bundle fields (fields that are not defined in $base_field_definitions, and therefore might not exist on some bundles) as well as bundle-specific overrides of base fields (fields that are defined in $base_field_definitions, and therefore exist for all bundles). However, bundle-specific base field overrides can also be provided by 'base_field_override' configuration entities, and that is the recommended approach except in cases where an entity type needs to provide a bundle-specific base field override that is decoupled from configuration. Note that for most entity types, the bundles themselves are derived from configuration (e.g., 'node' bundles are managed via 'node_type' configuration entities), so decoupling bundle-specific base field overrides from configuration only makes sense for entity types that also decouple their bundles from configuration. In cases where both this function returns a bundle-specific override of a base field and a 'base_field_override' configuration entity exists, the latter takes precedence.

@todo WARNING: This method will be changed in https://www.drupal.org/node/2346347.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

string $bundle: The bundle.

\Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions: The list of base field definitions.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of bundle field definitions, keyed by field name.

Overrides ContentEntityBase::bundleFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()

File

modules/order/src/Entity/OrderItem.php, line 501

Class

OrderItem
Defines the order item entity class.

Namespace

Drupal\commerce_order\Entity

Code

public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {

  /** @var \Drupal\commerce_order\Entity\OrderItemTypeInterface $order_item_type */
  $order_item_type = OrderItemType::load($bundle);
  if (!$order_item_type) {
    throw new \RuntimeException(sprintf('Could not load the "%s" order item type.', $bundle));
  }
  $purchasable_entity_type = $order_item_type
    ->getPurchasableEntityTypeId();
  $fields = [];
  $fields['purchased_entity'] = clone $base_field_definitions['purchased_entity'];
  if ($purchasable_entity_type) {
    $fields['purchased_entity']
      ->setSetting('target_type', $purchasable_entity_type);
  }
  else {

    // This order item type won't reference a purchasable entity. The field
    // can't be removed here, or converted to a configurable one, so it's
    // hidden instead. https://www.drupal.org/node/2346347#comment-10254087.
    $fields['purchased_entity']
      ->setRequired(FALSE);
    $fields['purchased_entity']
      ->setDisplayOptions('form', [
      'region' => 'hidden',
    ]);
    $fields['purchased_entity']
      ->setDisplayConfigurable('form', FALSE);
    $fields['purchased_entity']
      ->setDisplayConfigurable('view', FALSE);
    $fields['purchased_entity']
      ->setReadOnly(TRUE);

    // Make the title field visible and required.
    $fields['title'] = clone $base_field_definitions['title'];
    $fields['title']
      ->setRequired(TRUE);
    $fields['title']
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -1,
    ]);
    $fields['title']
      ->setDisplayConfigurable('form', TRUE);
    $fields['title']
      ->setDisplayConfigurable('view', TRUE);

    // The unit price is always an override when there's no purchased entity.
    $fields['unit_price'] = clone $base_field_definitions['unit_price'];
    $fields['unit_price']
      ->setDisplayOptions('form', [
      'type' => 'commerce_unit_price',
      'weight' => 2,
      'settings' => [
        'require_confirmation' => FALSE,
      ],
    ]);
  }
  return $fields;
}