Skip to content

Commit

Permalink
Issue #2868637 by vasike: AddToCartFormatter not working in Views
Browse files Browse the repository at this point in the history
  • Loading branch information
vasike authored and bojanz committed Apr 21, 2017
1 parent c15564f commit b9ef8d0
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dependencies:
module:
- commerce_product
id: test_multiple_cart_forms
label: 'Test Multiple Cart Forms'
label: 'Test Multiple Cart Forms (Rendered products)'
module: views
description: ''
tag: ''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
langcode: en
status: true
dependencies:
module:
- commerce_product
id: test_multiple_cart_forms_fields
label: 'Test Multiple Cart Forms (Fields)'
module: views
description: ''
tag: ''
base_table: commerce_product_field_data
base_field: product_id
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: none
options: { }
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: mini
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ‹‹
next: ››
style:
type: default
row:
type: fields
fields:
variations:
id: variations
table: commerce_product__variations
field: variations
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: target_id
type: commerce_add_to_cart
settings:
combine: false
group_column: target_id
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
plugin_id: field
filters: { }
sorts: { }
title: 'Test Multiple Cart Forms'
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-multiple-cart-forms-fields
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
tags: { }
16 changes: 15 additions & 1 deletion modules/cart/tests/src/Functional/MultipleCartFormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected function setUp() {
* Tests that a page with multiple add to cart forms works properly.
*/
public function testMultipleCartsOnPage() {
// View of rendered products, each containing an add to cart form.
$this->drupalGet('/test-multiple-cart-forms');

/** @var \Behat\Mink\Element\NodeElement[] $forms */
$forms = $this->getSession()->getPage()->findAll('css', '.commerce-order-item-add-to-cart-form');
$this->assertEquals(5, count($forms));
Expand All @@ -65,6 +65,20 @@ public function testMultipleCartsOnPage() {
$this->cart = Order::load($this->cart->id());
$order_items = $this->cart->getItems();
$this->assertEquals(new Price('6', 'USD'), $order_items[0]->getTotalPrice());

// View of fields, one of which is the variations field
// rendered via the "commerce_add_to_cart" formatter.
$this->drupalGet('/test-multiple-cart-forms-fields');
/** @var \Behat\Mink\Element\NodeElement[] $forms */
$forms = $this->getSession()->getPage()->findAll('css', '.commerce-order-item-add-to-cart-form');
$this->assertEquals(5, count($forms));
$this->submitForm([], 'Add to cart', $forms[3]->getAttribute('id'));

\Drupal::entityTypeManager()->getStorage('commerce_order')->resetCache();
$this->cart = Order::load($this->cart->id());
$order_items = $this->cart->getItems();
$this->assertEquals(2, count($order_items));
$this->assertEquals(new Price('9', 'USD'), $order_items[1]->getTotalPrice());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ public function settingsSummary() {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
return [
'#lazy_builder' => ['commerce_product.lazy_builders:addToCartForm', [
$items->getEntity()->id(),
$this->viewMode,
$this->getSetting('combine'),
],
$elements = [];
$elements[0]['add_to_cart_form'] = [
'#lazy_builder' => [
'commerce_product.lazy_builders:addToCartForm', [
$items->getEntity()->id(),
$this->viewMode,
$this->getSetting('combine'),
],
],
'#create_placeholder' => TRUE,
];
return $elements;
}

/**
Expand Down

0 comments on commit b9ef8d0

Please sign in to comment.