From 3d92dba3358e4dd7f613401b4303db8e0b3bea5f Mon Sep 17 00:00:00 2001
From: Pieter-Jan Put <pieter-jan.put@intracto.com>
Date: Wed, 22 Jun 2016 16:21:59 +0200
Subject: [PATCH] Issue 2724059: allow quantity in shopping cart block to be
 plural

---
 .../cart/config/schema/commerce_cart.schema.yml    |  4 +-
 modules/cart/src/Plugin/Block/CartBlock.php        | 50 ++++++++++++++++++----
 2 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/modules/cart/config/schema/commerce_cart.schema.yml b/modules/cart/config/schema/commerce_cart.schema.yml
index a4baddb..0fc5733 100644
--- a/modules/cart/config/schema/commerce_cart.schema.yml
+++ b/modules/cart/config/schema/commerce_cart.schema.yml
@@ -17,8 +17,8 @@ block.settings.commerce_cart:
   label: 'Cart block'
   mapping:
     item_text:
-      type: string
-      label: 'Items text'
+      type: plural_label
+      label: 'item\x03items'
     dropdown:
       type: boolean
       label: 'Display cart contents in dropdown'
diff --git a/modules/cart/src/Plugin/Block/CartBlock.php b/modules/cart/src/Plugin/Block/CartBlock.php
index b227381..fb97dc0 100644
--- a/modules/cart/src/Plugin/Block/CartBlock.php
+++ b/modules/cart/src/Plugin/Block/CartBlock.php
@@ -7,6 +7,7 @@
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
 use Drupal\Core\Url;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -83,12 +84,42 @@ public function defaultConfiguration() {
    * {@inheritdoc}
    */
   public function blockForm($form, FormStateInterface $form_state) {
-    $form['commerce_cart_item_text'] = [
-      '#type' => 'textfield',
-      '#title' => $this->t('Items text'),
-      '#default_value' => $this->configuration['item_text'],
-      '#description' => $this->t('Shown after the number. Defaults to "items"'),
-    ];
+    $plural_array = explode(
+      PluralTranslatableMarkup::DELIMITER,
+      $this->configuration['item_text']
+    );
+    $plurals = $this->getNumberOfPlurals();
+    for ($i = 0; $i < $plurals; $i++) {
+      $form['commerce_cart_item_text'][$i] = array(
+        '#type' => 'textfield',
+        // @todo Should use better labels https://www.drupal.org/node/2499639
+        '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural(
+          $i,
+          'First plural form',
+          '@count. plural form'
+        )),
+        '#default_value' => isset($plural_array[$i]) ? $plural_array[$i] : '',
+        '#description' => $this->t(
+          'Text to use for this variant, shown after the number.'
+        ),
+        '#states' => array(
+          'visible' => array(
+            ':input[name="options[format_plural]"]' => array('checked' => TRUE),
+          ),
+        ),
+      );
+    }
+    if ($plurals == 2) {
+      // Simplify interface text for the most common case.
+      $form['commerce_cart_item_text'][0]['#description'] = $this->t(
+        'Text to use for the singular form, shown after the number.'
+      );
+      $form['commerce_cart_item_text'][1]['#title'] = $this->t('Plural form');
+      $form['commerce_cart_item_text'][1]['#description'] = $this->t(
+        'Text to use for the plural form, shown after the number.'
+      );
+    }
+
     $form['commerce_cart_dropdown'] = [
       '#type' => 'radios',
       '#title' => $this->t('Display cart contents in a dropdown'),
@@ -106,7 +137,10 @@ public function blockForm($form, FormStateInterface $form_state) {
    * {@inheritdoc}
    */
   public function blockSubmit($form, FormStateInterface $form_state) {
-    $this->configuration['item_text'] = $form_state->getValue('commerce_cart_item_text');
+    $this->configuration['item_text'] = implode(
+      PluralTranslatableMarkup::DELIMITER,
+      $form_state->getValue('commerce_cart_item_text')
+    );
     $this->configuration['dropdown'] = $form_state->getValue('commerce_cart_dropdown');
   }
 
@@ -153,7 +187,7 @@ public function build() {
         '#alt' => $this->t('Shopping cart'),
       ],
       '#count' => $count,
-      '#item_text' => $this->configuration['item_text'],
+      '#item_text' => PluralTranslatableMarkup::createFromTranslatedString($count, $this->configuration['item_text']),
       '#url' => Url::fromRoute('commerce_cart.page')->toString(),
       '#content' => $cart_views,
       '#links' => $links,
