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: ...@@ -5,6 +5,7 @@ services:
commerce.configurable_field_manager: commerce.configurable_field_manager:
class: Drupal\commerce\ConfigurableFieldManager class: Drupal\commerce\ConfigurableFieldManager
arguments: ['@entity_type.manager']
commerce.credentials_check_flood: commerce.credentials_check_flood:
class: Drupal\commerce\CredentialsCheckFlood class: Drupal\commerce\CredentialsCheckFlood
...@@ -58,3 +59,7 @@ services: ...@@ -58,3 +59,7 @@ services:
arguments: ['@commerce.current_country'] arguments: ['@commerce.current_country']
tags: tags:
- { name: cache.context} - { 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: field.value.commerce_remote_id:
type: mapping type: mapping
label: 'Default value' label: 'Default value'
......
...@@ -3,6 +3,7 @@ status: true ...@@ -3,6 +3,7 @@ status: true
label: Default label: Default
id: default id: default
workflow: order_default workflow: order_default
traits: { }
refresh_mode: customer refresh_mode: customer
refresh_frequency: 300 refresh_frequency: 300
sendReceipt: true sendReceipt: true
......
commerce_order.commerce_order_type.*: commerce_order.commerce_order_type.*:
type: config_entity type: commerce_config_entity_bundle
label: 'Order type' label: 'Order type'
mapping: mapping:
label:
type: label
label: 'Label'
id:
type: string
label: 'Machine-readable name'
workflow: workflow:
type: string type: string
label: 'Workflow' label: 'Workflow'
...@@ -25,15 +19,9 @@ commerce_order.commerce_order_type.*: ...@@ -25,15 +19,9 @@ commerce_order.commerce_order_type.*:
label: 'The receipt BCC email' label: 'The receipt BCC email'
commerce_order.commerce_order_item_type.*: commerce_order.commerce_order_item_type.*:
type: config_entity type: commerce_config_entity_bundle
label: 'order item type' label: 'Order item type'
mapping: mapping:
label:
type: label
label: 'Label'
id:
type: string
label: 'Machine-readable name'
purchasableEntityType: purchasableEntityType:
type: string type: string
label: 'Purchasable entity type' label: 'Purchasable entity type'
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\commerce_order\Entity; namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\commerce\Entity\CommerceBundleEntityBase;
/** /**
* Defines the order item type entity class. * Defines the order item type entity class.
...@@ -39,7 +39,8 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -39,7 +39,8 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label", * "label",
* "id", * "id",
* "purchasableEntityType", * "purchasableEntityType",
* "orderType" * "orderType",
* "traits",
* }, * },
* links = { * links = {
* "add-form" = "/admin/commerce/config/order-item-types/add", * "add-form" = "/admin/commerce/config/order-item-types/add",
...@@ -49,21 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -49,21 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* } * }
* ) * )
*/ */
class OrderItemType extends ConfigEntityBundleBase implements OrderItemTypeInterface { class OrderItemType extends CommerceBundleEntityBase implements OrderItemTypeInterface {
/**
* The order item type ID.
*
* @var string
*/
protected $id;
/**
* The order item type label.
*
* @var string
*/
protected $label;
/** /**
* The purchasable entity type ID. * The purchasable entity type ID.
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
namespace Drupal\commerce_order\Entity; namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/** /**
* Defines the interface for order item types. * 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. * Gets the order item type's purchasable entity type ID.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\commerce_order\Entity; namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\commerce\Entity\CommerceBundleEntityBase;
/** /**
* Defines the order type entity class. * Defines the order type entity class.
...@@ -39,6 +39,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -39,6 +39,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label", * "label",
* "id", * "id",
* "workflow", * "workflow",
* "traits",
* "refresh_mode", * "refresh_mode",
* "refresh_frequency", * "refresh_frequency",
* "sendReceipt", * "sendReceipt",
...@@ -52,21 +53,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -52,21 +53,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* } * }
* ) * )
*/ */
class OrderType extends ConfigEntityBundleBase implements OrderTypeInterface { class OrderType extends CommerceBundleEntityBase implements OrderTypeInterface {
/**
* The order type ID.
*
* @var string
*/
protected $id;
/**
* The order type label.
*
* @var string
*/
protected $label;
/** /**
* The order type workflow ID. * The order type workflow ID.
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
namespace Drupal\commerce_order\Entity; namespace Drupal\commerce_order\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/** /**
* Defines the interface for order types. * Defines the interface for order types.
*/ */
interface OrderTypeInterface extends ConfigEntityInterface { interface OrderTypeInterface extends CommerceBundleEntityInterface {
// Refresh modes. // Refresh modes.
const REFRESH_ALWAYS = 'always'; const REFRESH_ALWAYS = 'always';
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
namespace Drupal\commerce_order\Form; namespace Drupal\commerce_order\Form;
use Drupal\Core\Entity\BundleEntityFormBase; use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
class OrderItemTypeForm extends BundleEntityFormBase { class OrderItemTypeForm extends CommerceBundleEntityFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -35,7 +35,6 @@ class OrderItemTypeForm extends BundleEntityFormBase { ...@@ -35,7 +35,6 @@ class OrderItemTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Label'), '#title' => $this->t('Label'),
'#maxlength' => 255, '#maxlength' => 255,
'#default_value' => $order_item_type->label(), '#default_value' => $order_item_type->label(),
'#description' => $this->t('Label for the order item type.'),
'#required' => TRUE, '#required' => TRUE,
]; ];
$form['id'] = [ $form['id'] = [
...@@ -62,15 +61,25 @@ class OrderItemTypeForm extends BundleEntityFormBase { ...@@ -62,15 +61,25 @@ class OrderItemTypeForm extends BundleEntityFormBase {
'#options' => $order_types, '#options' => $order_types,
'#required' => TRUE, '#required' => TRUE,
]; ];
$form = $this->buildTraitForm($form, $form_state);
return $this->protectBundleIdElement($form); return $this->protectBundleIdElement($form);
} }
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function save(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) {
$this->entity->save(); $this->entity->save();
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('Saved the %label order item type.', [ drupal_set_message($this->t('Saved the %label order item type.', [
'%label' => $this->entity->label(), '%label' => $this->entity->label(),
])); ]));
......
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
namespace Drupal\commerce_order\Form; namespace Drupal\commerce_order\Form;
use Drupal\commerce_order\Entity\OrderType; use Drupal\commerce_order\Entity\OrderType;
use Drupal\Core\Entity\BundleEntityFormBase; use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
/** /**
* Provides an order type form. * Provides an order type form.
*/ */
class OrderTypeForm extends BundleEntityFormBase { class OrderTypeForm extends CommerceBundleEntityFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -28,7 +28,6 @@ class OrderTypeForm extends BundleEntityFormBase { ...@@ -28,7 +28,6 @@ class OrderTypeForm extends BundleEntityFormBase {
'#title' => $this->t('Label'), '#title' => $this->t('Label'),
'#maxlength' => 255, '#maxlength' => 255,
'#default_value' => $order_type->label(), '#default_value' => $order_type->label(),
'#description' => $this->t('Label for the order type.'),
'#required' => TRUE, '#required' => TRUE,
]; ];
$form['id'] = [ $form['id'] = [
...@@ -47,6 +46,7 @@ class OrderTypeForm extends BundleEntityFormBase { ...@@ -47,6 +46,7 @@ class OrderTypeForm extends BundleEntityFormBase {
'#default_value' => $order_type->getWorkflowId(), '#default_value' => $order_type->getWorkflowId(),
'#description' => $this->t('Used by all orders of this type.'), '#description' => $this->t('Used by all orders of this type.'),
]; ];
$form = $this->buildTraitForm($form, $form_state);
$form['refresh'] = [ $form['refresh'] = [
'#type' => 'details', '#type' => 'details',
...@@ -129,6 +129,7 @@ class OrderTypeForm extends BundleEntityFormBase { ...@@ -129,6 +129,7 @@ class OrderTypeForm extends BundleEntityFormBase {
'@workflow' => $workflow->getLabel(), '@workflow' => $workflow->getLabel(),
])); ]));
} }
$this->validateTraitForm($form, $form_state);
} }
/** /**
...@@ -136,6 +137,8 @@ class OrderTypeForm extends BundleEntityFormBase { ...@@ -136,6 +137,8 @@ class OrderTypeForm extends BundleEntityFormBase {
*/ */
public function save(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) {
$status = $this->entity->save(); $status = $this->entity->save();
$this->submitTraitForm($form, $form_state);
drupal_set_message($this->t('Saved the %label order type.', ['%label' => $this->entity->label()])); drupal_set_message($this->t('Saved the %label order type.', ['%label' => $this->entity->label()]));
$form_state->setRedirect('entity.commerce_order_type.collection'); $form_state->setRedirect('entity.commerce_order_type.collection');
......
...@@ -6,3 +6,4 @@ label: Default ...@@ -6,3 +6,4 @@ label: Default
description: '' description: ''
variationType: default variationType: default
injectVariationFields: true injectVariationFields: true
traits: { }
...@@ -5,3 +5,4 @@ id: default ...@@ -5,3 +5,4 @@ id: default
label: Default label: Default
orderItemType: default orderItemType: default
generateTitle: true generateTitle: true
traits: { }
...@@ -8,3 +8,4 @@ label: 'Default' ...@@ -8,3 +8,4 @@ label: 'Default'
id: default id: default
purchasableEntityType: commerce_product_variation purchasableEntityType: commerce_product_variation
orderType: default orderType: default
traits: { }
commerce_product.commerce_product_type.*: commerce_product.commerce_product_type.*:
type: config_entity type: commerce_config_entity_bundle
label: 'Product type' label: 'Product type'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
description: description:
type: text type: text
label: 'Description' label: 'Description'
...@@ -19,18 +13,12 @@ commerce_product.commerce_product_type.*: ...@@ -19,18 +13,12 @@ commerce_product.commerce_product_type.*:
label: 'Inject product variation fields into the rendered product' label: 'Inject product variation fields into the rendered product'
commerce_product.commerce_product_variation_type.*: commerce_product.commerce_product_variation_type.*:
type: config_entity type: commerce_config_entity_bundle
label: 'Product variation type' label: 'Product variation type'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
orderItemType: orderItemType:
type: string type: string
label: 'order item type' label: 'Order item type'
generateTitle: generateTitle:
type: boolean type: boolean
label: 'Generate variation titles based on attribute values' label: 'Generate variation titles based on attribute values'
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\commerce_product\Entity; namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\commerce\Entity\CommerceBundleEntityBase;
/** /**
* Defines the product type entity class. * Defines the product type entity class.
...@@ -41,6 +41,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -41,6 +41,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "description", * "description",
* "variationType", * "variationType",
* "injectVariationFields", * "injectVariationFields",
* "traits",
* }, * },
* links = { * links = {
* "add-form" = "/admin/commerce/config/product-types/add", * "add-form" = "/admin/commerce/config/product-types/add",
...@@ -50,21 +51,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -50,21 +51,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* } * }
* ) * )
*/ */
class ProductType extends ConfigEntityBundleBase implements ProductTypeInterface { class ProductType extends CommerceBundleEntityBase implements ProductTypeInterface {
/**
* The product type ID.
*
* @var string
*/
protected $id;
/**
* The product type label.
*
* @var string
*/
protected $label;
/** /**
* The product type description. * The product type description.
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
namespace Drupal\commerce_product\Entity; namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\commerce\Entity\CommerceBundleEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface; use Drupal\Core\Entity\EntityDescriptionInterface;
/** /**
* Defines the interface for product types. * 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. * Gets the product type's matching variation type ID.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\commerce_product\Entity; namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\commerce\Entity\CommerceBundleEntityBase;
/** /**
* Defines the product variation type entity class. * Defines the product variation type entity class.
...@@ -40,6 +40,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -40,6 +40,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* "label", * "label",
* "orderItemType", * "orderItemType",
* "generateTitle", * "generateTitle",
* "traits",
* }, * },
* links = { * links = {
* "add-form" = "/admin/commerce/config/product-variation-types/add", * "add-form" = "/admin/commerce/config/product-variation-types/add",
...@@ -49,14 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; ...@@ -49,14 +50,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* } * }
* ) * )
*/ */
class ProductVariationType extends ConfigEntityBundleBase implements ProductVariationTypeInterface { class ProductVariationType extends CommerceBundleEntityBase implements ProductVariationTypeInterface {
/**
* The product variation type ID.
*
* @var string
*/
protected $id;
/** /**
* The order item type ID. * The order item type ID.
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
namespace Drupal\commerce_product\Entity; namespace Drupal\commerce_product\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\commerce\Entity\CommerceBundleEntityInterface;
/** /**
* Defines the interface for product variation types. * 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. * Gets the product variation type's order item type ID.
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
namespace Drupal\commerce_product\Form; 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\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
...@@ -10,7 +11,7 @@ use Drupal\Core\Form\FormStateInterface; ...@@ -10,7 +11,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\language\Entity\ContentLanguageSettings; use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductTypeForm extends BundleEntityFormBase { class ProductTypeForm extends CommerceBundleEntityFormBase {
/** /**
* The variation type storage. * The variation type storage.
...@@ -29,12 +30,16 @@ class ProductTypeForm extends BundleEntityFormBase { ...@@ -29,12 +30,16 @@ class ProductTypeForm extends BundleEntityFormBase {
/** /**
* Creates a new ProductTypeForm object. * Creates a new ProductTypeForm object.
* *
* @param \Drupal\commerce\EntityTraitManagerInterface $trait_manager
* The entity trait manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager. * The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The 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->variationTypeStorage = $entity_type_manager->getStorage('commerce_product_variation_type');
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
} }
...@@ -44,6 +49,7 @@ class ProductTypeForm extends BundleEntityFormBase { ...@@ -44,6 +49,7 @@ class ProductTypeForm extends BundleEntityFormBase {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('plugin.manager.commerce_entity_trait'),
$container->get('entity_type.manager'), $container->get('entity_type.manager'),
$container->get('entity_field.manager') $container->get('entity_field.manager')
); );
...@@ -108,6 +114,7 @@ class ProductTypeForm extends BundleEntityFormBase { ...@@ -108,6 +114,7 @@ class ProductTypeForm extends BundleEntityFormBase {
'#title' => t('Publish new products of this type by default.'), '#title' => t('Publish new products of this type by default.'),
'#default_value' => $product->isPublished(), '#default_value' => $product->isPublished(),
]; ];
$form = $this->buildTraitForm($form, $form_state);
if ($this->moduleHandler->moduleExists('language')) { if ($this->moduleHandler->moduleExists('language')) {
$form['language'] = [ $form['language'] = [
...@@ -129,6 +136,13 @@ class ProductTypeForm extends BundleEntityFormBase { ...@@ -129,6 +136,13 @@ class ProductTypeForm extends BundleEntityFormBase {
return $this->protectBundleIdElement($form); return $this->protectBundleIdElement($form);
} }
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/** /**
* {@inheritdoc} * {@inheritdoc}