Commit 43886ec8 authored by bojanz's avatar bojanz

Issue #2841734: Introduce the concept of entity trait plugins

parent 8c2aa636
commerce.entity_trait:
label: Commerce entity trait
provider: commerce
plugin_manager_service_id: plugin.manager.commerce_entity_trait
plugin_definition_decorator_class: Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
......@@ -5,6 +5,7 @@ services:
commerce.configurable_field_manager:
class: Drupal\commerce\ConfigurableFieldManager
arguments: ['@entity_type.manager']
commerce.credentials_check_flood:
class: Drupal\commerce\CredentialsCheckFlood
......@@ -58,3 +59,7 @@ services:
arguments: ['@commerce.current_country']
tags:
- { name: cache.context}
plugin.manager.commerce_entity_trait:
class: Drupal\commerce\EntityTraitManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@commerce.configurable_field_manager']
commerce_config_entity_bundle:
type: config_entity
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
traits:
type: sequence
sequence:
type: string
field.value.commerce_remote_id:
type: mapping
label: 'Default value'
......
......@@ -3,6 +3,7 @@ status: true
label: Default
id: default
workflow: order_default
traits: { }
refresh_mode: customer
refresh_frequency: 300
sendReceipt: true
......
commerce_order.commerce_order_type.*:
type: config_entity
type: commerce_config_entity_bundle
label: 'Order type'
mapping:
label:
type: label
label: 'Label'
id:
type: string
label: 'Machine-readable name'
workflow:
type: string
label: 'Workflow'
......@@ -25,15 +19,9 @@ commerce_order.commerce_order_type.*:
label: 'The receipt BCC email'
commerce_order.commerce_order_item_type.*:
type: config_entity
label: 'order item type'
type: commerce_config_entity_bundle
label: 'Order item type'
mapping:
label:
type: label
label: 'Label'
id:
type: string
label: 'Machine-readable name'
purchasableEntityType:
type: string
label: 'Purchasable entity type'
......
......@@ -2,7 +2,7 @@
namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\commerce\Entity\CommerceBundleEntityBase;
/**
* Defines the order item type entity class.
......@@ -39,7 +39,8 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label",
* "id",
* "purchasableEntityType",
* "orderType"
* "orderType",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/order-item-types/add",
......@@ -49,21 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* }
* )
*/
class OrderItemType extends ConfigEntityBundleBase implements OrderItemTypeInterface {
/**
* The order item type ID.
*
* @var string
*/
protected $id;
/**
* The order item type label.
*
* @var string
*/
protected $label;
class OrderItemType extends CommerceBundleEntityBase implements OrderItemTypeInterface {
/**
* The purchasable entity type ID.
......
......@@ -2,12 +2,12 @@
namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/**
* Defines the interface for order item types.
*/
interface OrderItemTypeInterface extends ConfigEntityInterface {
interface OrderItemTypeInterface extends CommerceBundleEntityInterface {
/**
* Gets the order item type's purchasable entity type ID.
......
......@@ -2,7 +2,7 @@
namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\commerce\Entity\CommerceBundleEntityBase;
/**
* Defines the order type entity class.
......@@ -39,6 +39,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label",
* "id",
* "workflow",
* "traits",
* "refresh_mode",
* "refresh_frequency",
* "sendReceipt",
......@@ -52,21 +53,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* }
* )
*/
class OrderType extends ConfigEntityBundleBase implements OrderTypeInterface {
/**
* The order type ID.
*
* @var string
*/
protected $id;
/**
* The order type label.
*
* @var string
*/
protected $label;
class OrderType extends CommerceBundleEntityBase implements OrderTypeInterface {
/**
* The order type workflow ID.
......
......@@ -2,12 +2,12 @@
namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/**
* Defines the interface for order types.
*/
interface OrderTypeInterface extends ConfigEntityInterface {
interface OrderTypeInterface extends CommerceBundleEntityInterface {
// Refresh modes.
const REFRESH_ALWAYS = 'always';
......
......@@ -2,11 +2,11 @@
namespace Drupal\commerce_order\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
class OrderItemTypeForm extends BundleEntityFormBase {
class OrderItemTypeForm extends CommerceBundleEntityFormBase {
/**
* {@inheritdoc}
......@@ -35,7 +35,6 @@ class OrderItemTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $order_item_type->label(),
'#description' => $this->t('Label for the order item type.'),
'#required' => TRUE,
];
$form['id'] = [
......@@ -62,15 +61,25 @@ class OrderItemTypeForm extends BundleEntityFormBase {
'#options' => $order_types,
'#required' => TRUE,
];
$form = $this->buildTraitForm($form, $form_state);
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('Saved the %label order item type.', [
'%label' => $this->entity->label(),
]));
......
......@@ -3,14 +3,14 @@
namespace Drupal\commerce_order\Form;
use Drupal\commerce_order\Entity\OrderType;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Provides an order type form.
*/
class OrderTypeForm extends BundleEntityFormBase {
class OrderTypeForm extends CommerceBundleEntityFormBase {
/**
* {@inheritdoc}
......@@ -28,7 +28,6 @@ class OrderTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $order_type->label(),
'#description' => $this->t('Label for the order type.'),
'#required' => TRUE,
];
$form['id'] = [
......@@ -47,6 +46,7 @@ class OrderTypeForm extends BundleEntityFormBase {
'#default_value' => $order_type->getWorkflowId(),
'#description' => $this->t('Used by all orders of this type.'),
];
$form = $this->buildTraitForm($form, $form_state);
$form['refresh'] = [
'#type' => 'details',
......@@ -129,6 +129,7 @@ class OrderTypeForm extends BundleEntityFormBase {
'@workflow' => $workflow->getLabel(),
]));
}
$this->validateTraitForm($form, $form_state);
}
/**
......@@ -136,6 +137,8 @@ class OrderTypeForm extends BundleEntityFormBase {
*/
public function save(array $form, FormStateInterface $form_state) {
$status = $this->entity->save();
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('Saved the %label order type.', ['%label' => $this->entity->label()]));
$form_state->setRedirect('entity.commerce_order_type.collection');
......
......@@ -6,3 +6,4 @@ label: Default
description: ''
variationType: default
injectVariationFields: true
traits: { }
......@@ -5,3 +5,4 @@ id: default
label: Default
orderItemType: default
generateTitle: true
traits: { }
......@@ -8,3 +8,4 @@ label: 'Default'
id: default
purchasableEntityType: commerce_product_variation
orderType: default
traits: { }
commerce_product.commerce_product_type.*:
type: config_entity
type: commerce_config_entity_bundle
label: 'Product type'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
description:
type: text
label: 'Description'
......@@ -19,18 +13,12 @@ commerce_product.commerce_product_type.*:
label: 'Inject product variation fields into the rendered product'
commerce_product.commerce_product_variation_type.*:
type: config_entity
type: commerce_config_entity_bundle
label: 'Product variation type'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
orderItemType:
type: string
label: 'order item type'
label: 'Order item type'
generateTitle:
type: boolean
label: 'Generate variation titles based on attribute values'
......
......@@ -2,7 +2,7 @@
namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\commerce\Entity\CommerceBundleEntityBase;
/**
* Defines the product type entity class.
......@@ -41,6 +41,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "description",
* "variationType",
* "injectVariationFields",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/product-types/add",
......@@ -50,21 +51,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* }
* )
*/
class ProductType extends ConfigEntityBundleBase implements ProductTypeInterface {
/**
* The product type ID.
*
* @var string
*/
protected $id;
/**
* The product type label.
*
* @var string
*/
protected $label;
class ProductType extends CommerceBundleEntityBase implements ProductTypeInterface {
/**
* The product type description.
......
......@@ -2,13 +2,13 @@
namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\commerce\Entity\CommerceBundleEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
/**
* Defines the interface for product types.
*/
interface ProductTypeInterface extends ConfigEntityInterface, EntityDescriptionInterface {
interface ProductTypeInterface extends CommerceBundleEntityInterface, EntityDescriptionInterface {
/**
* Gets the product type's matching variation type ID.
......
......@@ -2,7 +2,7 @@
namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\commerce\Entity\CommerceBundleEntityBase;
/**
* Defines the product variation type entity class.
......@@ -40,6 +40,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label",
* "orderItemType",
* "generateTitle",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/product-variation-types/add",
......@@ -49,14 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* }
* )
*/
class ProductVariationType extends ConfigEntityBundleBase implements ProductVariationTypeInterface {
/**
* The product variation type ID.
*
* @var string
*/
protected $id;
class ProductVariationType extends CommerceBundleEntityBase implements ProductVariationTypeInterface {
/**
* The order item type ID.
......
......@@ -2,12 +2,12 @@
namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/**
* Defines the interface for product variation types.
*/
interface ProductVariationTypeInterface extends ConfigEntityInterface {
interface ProductVariationTypeInterface extends CommerceBundleEntityInterface {
/**
* Gets the product variation type's order item type ID.
......
......@@ -2,7 +2,8 @@
namespace Drupal\commerce_product\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\commerce\EntityTraitManagerInterface;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
......@@ -10,7 +11,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductTypeForm extends BundleEntityFormBase {
class ProductTypeForm extends CommerceBundleEntityFormBase {
/**
* The variation type storage.
......@@ -29,12 +30,16 @@ class ProductTypeForm extends BundleEntityFormBase {
/**
* Creates a new ProductTypeForm object.
*
* @param \Drupal\commerce\EntityTraitManagerInterface $trait_manager
* The entity trait manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
public function __construct(EntityTraitManagerInterface $trait_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($trait_manager);
$this->variationTypeStorage = $entity_type_manager->getStorage('commerce_product_variation_type');
$this->entityFieldManager = $entity_field_manager;
}
......@@ -44,6 +49,7 @@ class ProductTypeForm extends BundleEntityFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.commerce_entity_trait'),
$container->get('entity_type.manager'),
$container->get('entity_field.manager')
);
......@@ -108,6 +114,7 @@ class ProductTypeForm extends BundleEntityFormBase {
'#title' => t('Publish new products of this type by default.'),
'#default_value' => $product->isPublished(),
];
$form = $this->buildTraitForm($form, $form_state);
if ($this->moduleHandler->moduleExists('language')) {
$form['language'] = [
......@@ -129,6 +136,13 @@ class ProductTypeForm extends BundleEntityFormBase {
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
......@@ -142,6 +156,7 @@ class ProductTypeForm extends BundleEntityFormBase {
$fields['status']->getConfig($this->entity->id())->setDefaultValue($value)->save();
$this->entityFieldManager->clearCachedFieldDefinitions();
}
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()]));
$form_state->setRedirect('entity.commerce_product_type.collection');
......
......@@ -2,14 +2,15 @@
namespace Drupal\commerce_product\Form;
use Drupal\commerce\EntityTraitManagerInterface;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\commerce_product\ProductAttributeFieldManagerInterface;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductVariationTypeForm extends BundleEntityFormBase {
class ProductVariationTypeForm extends CommerceBundleEntityFormBase {
/**
* The attribute field manager.
......@@ -21,10 +22,14 @@ class ProductVariationTypeForm extends BundleEntityFormBase {
/**
* Constructs a new ProductVariationTypeForm object.
*
* @param \Drupal\commerce\EntityTraitManagerInterface $trait_manager
* The entity trait manager.
* @param \Drupal\commerce_product\ProductAttributeFieldManagerInterface $attribute_field_manager
* The attribute field manager.
*/
public function __construct(ProductAttributeFieldManagerInterface $attribute_field_manager) {
public function __construct(EntityTraitManagerInterface $trait_manager, ProductAttributeFieldManagerInterface $attribute_field_manager) {
parent::__construct($trait_manager);
$this->attributeFieldManager = $attribute_field_manager;
}
......@@ -33,6 +38,7 @@ class ProductVariationTypeForm extends BundleEntityFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.commerce_entity_trait'),
$container->get('commerce_product.attribute_field_manager')
);
}
......@@ -65,6 +71,7 @@ class ProductVariationTypeForm extends BundleEntityFormBase {
'#title' => t('Generate variation titles based on attribute values.'),
'#default_value' => $variation_type->shouldGenerateTitle(),
];
$form = $this->buildTraitForm($form, $form_state);
if ($this->moduleHandler->moduleExists('commerce_order')) {
// Prepare a list of order item types used to purchase product variations.
......@@ -139,6 +146,13 @@ class ProductVariationTypeForm extends BundleEntityFormBase {
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
......@@ -147,6 +161,7 @@ class ProductVariationTypeForm extends BundleEntityFormBase {
drupal_set_message($this->t('Saved the %label product variation type.', ['%label' => $this->entity->label()]));
$form_state->setRedirect('entity.commerce_product_variation_type.collection');
$this->submitTraitForm($form, $form_state);
$attribute_storage = $this->entityTypeManager->getStorage('commerce_product_attribute');
$original_attributes = $form_state->getValue('original_attributes');
$attributes = array_filter($form_state->getValue('attributes'));
......
......@@ -3,4 +3,5 @@ status: true
dependencies: { }
id: online
label: Online
description: null
description: ''
traits: { }
commerce_store.commerce_store_type.*:
type: config_entity
label: 'Store type config'
type: commerce_config_entity_bundle
label: 'Store type'
mapping:
id:
type: string
label: 'Type'
label:
type: label
label: 'Label'
uuid:
type: string
description:
type: text
label: 'Description'
......
......@@ -2,7 +2,7 @@
namespace Drupal\commerce_store\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\commerce\Entity\CommerceBundleEntityBase;
/**
* Defines the store type entity class.
......@@ -40,6 +40,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label",
* "uuid",
* "description",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/store-types/add",
......@@ -49,21 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* }
* )
*/
class StoreType extends ConfigEntityBundleBase implements StoreTypeInterface {
/**
* The store type ID.
*
* @var string
*/
protected $id;
/**
* The store type label.
*
* @var string
*/
protected $label;
class StoreType extends CommerceBundleEntityBase implements StoreTypeInterface {
/**
* A brief description of this store type.
......
......@@ -2,11 +2,11 @@
namespace Drupal\commerce_store\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\commerce\Entity\CommerceBundleEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
/**
* Defines the interface for store types.
*/
interface StoreTypeInterface extends ConfigEntityInterface, EntityDescriptionInterface {
interface StoreTypeInterface extends CommerceBundleEntityInterface, EntityDescriptionInterface {
}
......@@ -2,12 +2,12 @@
namespace Drupal\commerce_store\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\language\Entity\ContentLanguageSettings;
class StoreTypeForm extends BundleEntityFormBase {
class StoreTypeForm extends CommerceBundleEntityFormBase {
/**
* {@inheritdoc}
......@@ -22,7 +22,6 @@ class StoreTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $store_type->label(),
'#description' => $this->t('Label for the store type.'),
'#required' => TRUE,
];
$form['id'] = [
......@@ -38,6 +37,7 @@ class StoreTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Description'),
'#default_value' => $store_type->getDescription(),
];
$form = $this->buildTraitForm($form, $form_state);
if ($this->moduleHandler->moduleExists('language')) {
$form['language'] = [
......@@ -59,11 +59,20 @@ class StoreTypeForm extends BundleEntityFormBase {
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('Saved the %label store type.', [
'%label' => $this->entity->label(),
]));
......
<?php
namespace Drupal\commerce\Annotation;
use Drupal\Component\Annotation\Plugin;