Skip to content

Commit

Permalink
Issue #2886642 by bojanz: Create a new condition UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz authored and bojanz committed Jun 28, 2017
1 parent 893361c commit adf3dd2
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 11 deletions.
5 changes: 5 additions & 0 deletions commerce.libraries.yml
@@ -1,3 +1,8 @@
conditions:
version: VERSION
js:
js/conditions.js: {}

toolbar:
version: VERSION
css:
Expand Down
10 changes: 10 additions & 0 deletions config/schema/commerce.schema.yml
Expand Up @@ -29,6 +29,16 @@ field.value.commerce_remote_id:
type: string
label: 'Remote ID'

field.widget.settings.commerce_conditions:
type: mapping
label: 'Conditions widget settings'
mapping:
entity_types:
type: sequence
label: 'Entity types'
sequence:
type: string

field.widget.settings.commerce_entity_select:
type: mapping
label: 'Entity select widget settings'
Expand Down
33 changes: 33 additions & 0 deletions js/conditions.js
@@ -0,0 +1,33 @@
/**
* @file
* Condition UI behaviors.
*/

(function ($, window, Drupal) {

'use strict';

/**
* Provides the summary information for the condition vertical tabs.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches the behavior for the condition summaries.
*/
Drupal.behaviors.conditionSummary = {
attach: function () {
$('.vertical-tabs__pane').each(function () {
$(this).drupalSetSummary(function (context) {
if ($(context).find('input.enable:checked').length) {
return Drupal.t('Restricted');
}
else {
return Drupal.t('Not restricted');
}
});
});
}
};

})(jQuery, window, Drupal);
5 changes: 4 additions & 1 deletion modules/promotion/src/Entity/Promotion.php
Expand Up @@ -556,8 +556,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(FALSE)
->setDisplayOptions('form', [
'type' => 'commerce_plugin_select',
'type' => 'commerce_conditions',
'weight' => 3,
'settings' => [
'entity_types' => ['commerce_order', 'commerce_order_item'],
],
]);

$fields['coupons'] = BaseFieldDefinition::create('entity_reference')
Expand Down
33 changes: 25 additions & 8 deletions modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php
Expand Up @@ -48,7 +48,6 @@ public function testCreatePromotion() {
$this->assertSession()->fieldExists('name[0][value]');
$name = $this->randomMachineName(8);
$this->getSession()->getPage()->fillField('name[0][value]', $name);

$this->getSession()->getPage()->selectFieldOption('offer[0][target_plugin_id]', 'commerce_promotion_product_percentage_off');
$this->waitForAjaxToFinish();
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][commerce_promotion_product_percentage_off][amount]', '10.0');
Expand All @@ -59,9 +58,17 @@ public function testCreatePromotion() {
$this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][commerce_promotion_order_percentage_off][amount]', '10.0');
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][commerce_promotion_order_percentage_off][amount]', '10.0');

$this->getSession()->getPage()->selectFieldOption('conditions[0][target_plugin_id]', 'commerce_promotion_order_total_price');
// Confirm the integrity of the conditions UI.
foreach (['order', 'product', 'customer'] as $condition_group) {
$tab_matches = $this->xpath('//a[@href="#edit-conditions-form-' . $condition_group . '"]');
$this->assertNotEmpty($tab_matches);
}
$vertical_tab_elements = $this->xpath('//a[@href="#edit-conditions-form-order"]');
$vertical_tab_element = reset($vertical_tab_elements);
$vertical_tab_element->click();
$this->getSession()->getPage()->checkField('Limit by total price');
$this->waitForAjaxToFinish();
$this->getSession()->getPage()->fillField('conditions[0][target_plugin_configuration][commerce_promotion_order_total_price][amount][number]', '50.00');
$this->getSession()->getPage()->fillField('conditions[form][order][commerce_promotion_order_total_price][configuration][form][amount][number]', '50.00');

// Confirm that the usage limit widget works properly.
$this->getSession()->getPage()->hasCheckedField(' Unlimited');
Expand Down Expand Up @@ -111,11 +118,6 @@ public function testCreatePromotionWithEndDate() {
'offer[0][target_plugin_configuration][commerce_promotion_order_percentage_off][amount]' => '10.0',
];

$this->getSession()->getPage()->fillField('conditions[0][target_plugin_id]', 'commerce_promotion_order_total_price');
$this->waitForAjaxToFinish();

$edit['conditions[0][target_plugin_configuration][commerce_promotion_order_total_price][amount][number]'] = '50.00';

// Set an end date.
$this->getSession()->getPage()->checkField('end_date[0][has_value]');
$edit['end_date[0][container][value][date]'] = date("Y") + 1 . '-01-01';
Expand Down Expand Up @@ -143,13 +145,28 @@ public function testEditPromotion() {
'amount' => '0.10',
],
],
'conditions' => [
[
'target_plugin_id' => 'commerce_promotion_order_total_price',
'target_plugin_configuration' => [
'amount' => [
'number' => '9.10',
'currency_code' => 'USD',
],
],
],
],
]);

/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
$offer_field = $promotion->get('offer')->first();
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']);

$this->drupalGet($promotion->toUrl('edit-form'));
$this->assertSession()->pageTextContains('Restricted');
$this->assertSession()->checkboxChecked('Limit by total price');
$this->assertSession()->fieldValueEquals('conditions[form][order][commerce_promotion_order_total_price][configuration][form][amount][number]', '9.10');

$new_promotion_name = $this->randomMachineName(8);
$edit = [
'name[0][value]' => $new_promotion_name,
Expand Down
18 changes: 16 additions & 2 deletions src/ConditionManager.php
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\commerce;

use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
Expand All @@ -16,7 +15,7 @@
* @see \Drupal\commerce\Annotation\CommerceCondition
* @see plugin_api
*/
class ConditionManager extends DefaultPluginManager implements CategorizingPluginManagerInterface {
class ConditionManager extends DefaultPluginManager implements ConditionManagerInterface {

use CategorizingPluginManagerTrait;

Expand Down Expand Up @@ -66,4 +65,19 @@ public function processDefinition(&$definition, $plugin_id) {
}
}

/**
* {@inheritdoc}
*/
public function getDefinitionsByEntityTypes(array $entity_types) {
$definitions = $this->getDefinitions();
if (!empty($entity_types)) {
// Remove conditions not matching the specified entity types.
$definitions = array_filter($definitions, function ($definition) use ($entity_types) {
return in_array($definition['entity_type'], $entity_types);
});
}

return $definitions;
}

}
23 changes: 23 additions & 0 deletions src/ConditionManagerInterface.php
@@ -0,0 +1,23 @@
<?php

namespace Drupal\commerce;

use Drupal\Component\Plugin\CategorizingPluginManagerInterface;

/**
* Defines the interface for commerce_condition plugin managers.
*/
interface ConditionManagerInterface extends CategorizingPluginManagerInterface {

/**
* Gets the plugin definitions for the given entity types.
*
* @param array $entity_types
* The entity type IDs.
*
* @return array
* The plugin definitions.
*/
public function getDefinitionsByEntityTypes(array $entity_types);

}

0 comments on commit adf3dd2

Please sign in to comment.