#2896815: Replace the order summary view with a Twig template has landed and demonstrated two things:
1) Twig is more performant and easier to customize for these use cases
2) We can still support using a View
So, let's apply the same recipe to the cart block view.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz created an issue. See original summary.

Thomas Cys’s picture

Thomas Cys’s picture

Status: Active » Needs review
Thomas Cys’s picture

Assigned: Unassigned » Thomas Cys
Status: Needs review » Needs work
mglaman’s picture

Rebased patch against HEAD. Reviewing tests, which I forgot I was blocker on for this.

mglaman’s picture

Status: Needs work » Needs review
bojanz’s picture

Status: Needs review » Needs work
diff --git a/modules/cart/templates/commerce-cart-order-items-table.html.twig b/modules/cart/templates/commerce-cart-order-items-table.html.twig
new file mode 100644
index 00000000..4aa0be66
--- /dev/null
+++ b/modules/cart/templates/commerce-cart-order-items-table.html.twig
@@ -0,0 +1,11 @@
+<table>
+    <tbody>
+    {% for order_item in order_entity.getItems %}
+        <tr>
+            <td>{{ order_item.getQuantity|number_format }} x</td>
+            <td>{{ order_item.label }}</td>
+            <td>{{ order_item.getTotalPrice|commerce_price_format }}</td>
+        </tr>
+    {% endfor %}
+    </tbody>
+</table>
\ No newline at end of file

There are a few problems here:

1) Missing wrapper div that would allow CSS targetting (probably the same wrapper we use for the view?)
2) Unexpected template name. We generally name templates after the role they have, not the markup they contain. So this doesn't match what we did with the checkout order summary, for example. I'd expect the template to be called commerce-cart-block.html.twig
3) Not following coding standards (4 spaces instead of 2, lack of ending newline)
4) Lack of the usual docs block on top

waspper’s picture

Reviewed #5, and added some changes.

Not sure about advice No. 2 at #7, because template is provided for block content. Whole block will remain using the "commerce_cart_block" theme, because adding some content. Therefore, template was named to "commerce-cart-block-content-simple", to match better with its purpose. What do you think? :)

waspper’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: cart_block_simple-2906024-8.patch, failed testing. View results

Thomas Cys’s picture

Assigned: mglaman » Thomas Cys
Thomas Cys’s picture

Thomas Cys’s picture

Thomas Cys’s picture

Assigned: Thomas Cys » Unassigned
Status: Needs work » Needs review

Don't mind #12
Thanks for the patch waspper.
I just renamed the new template file from commerce-cart-block-content-simple to commerce-cart-block-content.

agoradesign’s picture

When touching the CartBlock class already, we should take the chance and clean up cache metadata.

I've just found some needless lines:

Within build(), a CacheableMetadata object is created and populated, but never attached anywhere.

Further I'm unsure whether the cache context added directly to the render array is redundant - we already have implemented getCacheContexts(). I never needed to define this twice in any block I've created so far.

@bojanz: if this should go into a separate issue, tell me, I can provide a patch (didn't do it because it would only break this one, if it gets sooner committed)

charlieweb82’s picture

Status: Needs review » Needs work

Testing the patch and the template reference commerce-cart-block-content.html.twig in the code don’t load any changes like add a class or delete elements from the template, should be nice to load different field separately, also not use table in a small space. view cart should use in the template div to display like table.

Thomas Cys’s picture

Status: Needs work » Needs review

Not 100% sure what you mean by

the code don’t load any changes like add a class or delete elements from the template, should be nice to load different field separately

Remember that this twig file only mimics the view (commerce_cart_block) that is provided by the commerce_cart module. The twig file in this patch should not vary from that.