Skip to content

Commit

Permalink
Issue #2841734: Introduce the concept of entity trait plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Jan 11, 2017
1 parent 8c2aa63 commit 43886ec
Show file tree
Hide file tree
Showing 42 changed files with 955 additions and 150 deletions.
5 changes: 5 additions & 0 deletions commerce.plugin_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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 changes: 5 additions & 0 deletions commerce.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
14 changes: 14 additions & 0 deletions config/schema/commerce.schema.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ status: true
label: Default
id: default
workflow: order_default
traits: { }
refresh_mode: customer
refresh_frequency: 300
sendReceipt: true
Expand Down
18 changes: 3 additions & 15 deletions modules/order/config/schema/commerce_order.schema.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
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'
Expand All @@ -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'
Expand Down
21 changes: 4 additions & 17 deletions modules/order/src/Entity/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -39,7 +39,8 @@
* "label",
* "id",
* "purchasableEntityType",
* "orderType"
* "orderType",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/order-item-types/add",
Expand All @@ -49,21 +50,7 @@
* }
* )
*/
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.
Expand Down
4 changes: 2 additions & 2 deletions modules/order/src/Entity/OrderItemTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 3 additions & 16 deletions modules/order/src/Entity/OrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -39,6 +39,7 @@
* "label",
* "id",
* "workflow",
* "traits",
* "refresh_mode",
* "refresh_frequency",
* "sendReceipt",
Expand All @@ -52,21 +53,7 @@
* }
* )
*/
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.
Expand Down
4 changes: 2 additions & 2 deletions modules/order/src/Entity/OrderTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
15 changes: 12 additions & 3 deletions modules/order/src/Form/OrderItemTypeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -35,7 +35,6 @@ public function form(array $form, FormStateInterface $form_state) {
'#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'] = [
Expand All @@ -62,15 +61,25 @@ public function form(array $form, FormStateInterface $form_state) {
'#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(),
]));
Expand Down
9 changes: 6 additions & 3 deletions modules/order/src/Form/OrderTypeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -28,7 +28,6 @@ public function form(array $form, FormStateInterface $form_state) {
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $order_type->label(),
'#description' => $this->t('Label for the order type.'),
'#required' => TRUE,
];
$form['id'] = [
Expand All @@ -47,6 +46,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#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',
Expand Down Expand Up @@ -129,13 +129,16 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
'@workflow' => $workflow->getLabel(),
]));
}
$this->validateTraitForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ label: Default
description: ''
variationType: default
injectVariationFields: true
traits: { }
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ id: default
label: Default
orderItemType: default
generateTitle: true
traits: { }
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ label: 'Default'
id: default
purchasableEntityType: commerce_product_variation
orderType: default
traits: { }
18 changes: 3 additions & 15 deletions modules/product/config/schema/commerce_product.schema.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
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'
Expand All @@ -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'
Expand Down
19 changes: 3 additions & 16 deletions modules/product/src/Entity/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -41,6 +41,7 @@
* "description",
* "variationType",
* "injectVariationFields",
* "traits",
* },
* links = {
* "add-form" = "/admin/commerce/config/product-types/add",
Expand All @@ -50,21 +51,7 @@
* }
* )
*/
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.
Expand Down
4 changes: 2 additions & 2 deletions modules/product/src/Entity/ProductTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 43886ec

Please sign in to comment.