Commit d03179eb authored by bojanz's avatar bojanz

Replace the line item product_variations field with a generic source...

Replace the line item product_variations field with a generic source reference, expand the line item type.
parent c80736f9
......@@ -9,8 +9,6 @@ dependencies:
- commerce_product
config_devel:
- commerce_line_item.commerce_line_item_type.product_variation
- field.field.commerce_line_item.product_variation.product_variation
- core.entity_form_display.commerce_line_item.product_variation.default
- field.storage.commerce_line_item.product_variation
- core.entity_view_display.commerce_line_item.product_variation.default
......@@ -3,3 +3,5 @@ status: true
dependencies: { }
label: 'Product variation'
id: product_variation
sourceEntityType: commerce_product_variation
orderType: default
......@@ -3,7 +3,6 @@ status: true
dependencies:
config:
- commerce_line_item.commerce_line_item_type.product_variation
- field.field.commerce_line_item.product_variation.product_variation
module:
- commerce_price
id: commerce_line_item.product_variation.default
......@@ -16,7 +15,7 @@ content:
weight: 3
settings: { }
third_party_settings: { }
product_variation:
source:
type: entity_reference_autocomplete
weight: 0
settings:
......
......@@ -3,7 +3,6 @@ status: true
dependencies:
config:
- commerce_line_item.commerce_line_item_type.product_variation
- field.field.commerce_line_item.product_variation.product_variation
module:
- commerce_price
- user
......@@ -20,7 +19,7 @@ content:
display_currency_code: false
third_party_settings: { }
label: above
product_variation:
source:
type: entity_reference_entity_view
weight: 0
settings:
......
langcode: en
status: true
dependencies:
config:
- commerce_line_item.commerce_line_item_type.product_variation
- field.storage.commerce_line_item.product_variation
module:
- entity_reference
id: commerce_line_item.product_variation.product_variation
field_name: product_variation
entity_type: commerce_line_item
bundle: product_variation
label: Product variation
description: ''
required: true
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:commerce_product_variation'
handler_settings:
target_bundles:
default: default
sort:
field: _none
field_type: entity_reference
langcode: en
status: true
dependencies:
module:
- commerce_line_item
- entity_reference
id: commerce_line_item.product_variation
field_name: product_variation
entity_type: commerce_line_item
type: entity_reference
settings:
target_type: commerce_product_variation
module: entity_reference
locked: false
cardinality: 1
translatable: false
indexes: { }
persist_with_no_fields: false
......@@ -8,3 +8,9 @@ commerce_line_item.commerce_line_item_type.*:
id:
type: string
label: 'Machine-readable name'
sourceEntityType:
type: string
label: 'Source entity type'
orderType:
type: string
label: 'Order type'
......@@ -298,6 +298,11 @@ class LineItem extends ContentEntityBase implements LineItemInterface {
->setRequired(TRUE)
->setRevisionable(TRUE);
$fields['source'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Source'))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['quantity'] = BaseFieldDefinition::create('decimal')
->setLabel(t('Quantity'))
->setDescription(t('The quantity of units.'))
......@@ -363,6 +368,19 @@ class LineItem extends ContentEntityBase implements LineItemInterface {
return $fields;
}
/**
* {@inheritdoc}
*/
public static function bundleFieldDefinitions(EntityTypeInterface $entityType, $bundle, array $baseFieldDefinitions) {
$fields = [];
if ($lineItemType = LineItemType::load($bundle)) {
$fields['source'] = clone $baseFieldDefinitions['source'];
$fields['source']->setSetting('target_type', $lineItemType->getSourceEntityType());
}
return $fields;
}
/**
* Default value callback for 'uid' base field definition.
*
......
......@@ -35,6 +35,8 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* config_export = {
* "label",
* "id",
* "sourceEntityType",
* "orderType"
* },
* links = {
* "edit-form" = "/admin/commerce/config/line-item-types/{commerce_line_item_type}/edit",
......@@ -59,4 +61,48 @@ class LineItemType extends ConfigEntityBundleBase implements LineItemTypeInterfa
*/
protected $label;
/**
* The source entity type.
*
* @var string
*/
protected $sourceEntityType;
/**
* The order type.
*
* @var string
*/
protected $orderType;
/**
* {@inheritdoc}
*/
public function getSourceEntityType() {
return $this->sourceEntityType;
}
/**
* {@inheritdoc}
*/
public function setSourceEntityType($sourceEntityType) {
$this->sourceEntityType = $sourceEntityType;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOrderType() {
return $this->orderType;
}
/**
* {@inheritdoc}
*/
public function setOrderType($orderType) {
$this->orderType = $orderType;
return $this;
}
}
......@@ -8,36 +8,34 @@
namespace Drupal\commerce_line_item\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LineItemTypeForm extends EntityForm {
/**
* The line_item type storage.
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $lineItemTypeStorage;
protected $entityManager;
/**
* Create an LineItemTypeForm object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $lineItemTypeStorage
* The line_item type storage.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
*/
public function __construct(EntityStorageInterface $lineItemTypeStorage) {
$this->lineItemTypeStorage = $lineItemTypeStorage;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entityManager */
$entityManager = $container->get('entity.manager');
return new static($entityManager->getStorage('commerce_line_item_type'));
return new static($container->get('entity.manager'));
}
/**
......@@ -46,6 +44,19 @@ class LineItemTypeForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$lineItemType = $this->entity;
// Prepare the list of source entity types.
$entityTypes = $this->entityManager->getDefinitions();
$sourceEntityTypes = array_filter($entityTypes, function($entityType) {
return $entityType->isSubclassOf('\Drupal\commerce\LineItemSourceInterface');
});
$sourceEntityTypes = array_map(function($entityType) {
return $entityType->getLabel();
}, $sourceEntityTypes);
// Prepare the list of order types.
$orderTypes = $this->entityManager->getStorage('commerce_order_type')->loadMultiple();
$orderTypes = array_map(function($orderType) {
return $orderType->label();
}, $orderTypes);
$form['label'] = [
'#type' => 'textfield',
......@@ -64,6 +75,20 @@ class LineItemTypeForm extends EntityForm {
],
'#disabled' => !$lineItemType->isNew()
];
$form['sourceEntityType'] = [
'#type' => 'select',
'#title' => $this->t('Source entity type'),
'#default_value' => $lineItemType->getSourceEntityType(),
'#options' => $sourceEntityTypes,
'#required' => TRUE,
];
$form['orderType'] = [
'#type' => 'select',
'#title' => $this->t('Order type'),
'#default_value' => $lineItemType->getOrderType(),
'#options' => $orderTypes,
'#required' => TRUE,
];
return $form;
}
......@@ -72,21 +97,11 @@ class LineItemTypeForm extends EntityForm {
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$lineItemType = $this->entity;
try {
$lineItemType->save();
drupal_set_message($this->t('Saved the %label line item type.', [
'%label' => $lineItemType->label(),
]));
$form_state->setRedirect('entity.commerce_line_item_type.collection');
} catch (\Exception $e) {
$this->logger('commerce_line_item')->error($e);
drupal_set_message($this->t('The %label line item type was not saved.', [
'%label' => $lineItemType->label(),
]), 'error');
$form_state->setRebuild();
}
$this->entity->save();
drupal_set_message($this->t('Saved the %label line item type.', [
'%label' => $this->entity->label(),
]));
$form_state->setRedirect('entity.commerce_line_item_type.collection');
}
}
......@@ -13,4 +13,44 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
* Defines the interface for line item types.
*/
interface LineItemTypeInterface extends ConfigEntityInterface {
/**
* Gets the line item type's source entity type.
*
* For example, if line items of this type are populated from product
* variations, the source entity type will be 'commerce_product_variation'.
*
* @return string
* The source entity type.
*/
public function getSourceEntityType();
/**
* Sets the line item type's source entity type.
*
* @param string $sourceEntityType
* The source entity type.
*
* @return $this
*/
public function setSourceEntityType($sourceEntityType);
/**
* Gets the line item type's order type.
*
* @return string
* The order type.
*/
public function getOrderType();
/**
* Sets the line item type's order type.
*
* @param string $orderType
* The order type.
*
* @return $this
*/
public function setOrderType($orderType);
}
......@@ -24,8 +24,9 @@ class OrderAdminTest extends CommerceOrderTestBase {
$this->drupalGet('/admin/commerce/orders');
$this->clickLink('Create a new order');
$source = $this->variation->getSku() . ' (' . $this->variation->id() . ')';
$values = array(
'line_items[form][inline_entity_form][product_variation][0][target_id]' => $this->variation->getSku(),
'line_items[form][inline_entity_form][source][0][target_id]' => $source,
'line_items[form][inline_entity_form][quantity][0][value]' => 1,
'line_items[form][inline_entity_form][unit_price][0][amount]' => '9.99'
);
......
......@@ -7,14 +7,14 @@
namespace Drupal\commerce_product;
use Drupal\commerce\LineItemSourceInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Defines the interface for product variations.
*/
interface ProductVariationInterface extends EntityChangedInterface, EntityInterface, EntityOwnerInterface {
interface ProductVariationInterface extends LineItemSourceInterface, EntityChangedInterface, EntityOwnerInterface {
/**
* Gets the variation type.
......
<?php
/**
* @file
* Contains \Drupal\commerce\LineItemSourceInterface.
*/
namespace Drupal\commerce;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Defines the interface for entity types that can serve as line item sources.
*
* Lives in Drupal\commerce instead of Drupal\commerce_order so that entity
* type providing modules such as commerce_product don't need to depend
* on commerce_order.
*
* @todo Add a getPrice() method.
*/
interface LineItemSourceInterface extends ContentEntityInterface {
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment