From 7b92eec6ce60d535046c9828ec2befea7a299ac8 Mon Sep 17 00:00:00 2001
From: Matt Glaman <nmd.matt@gmail.com>
Date: Thu, 21 Jul 2016 15:43:51 -0500
Subject: [PATCH] Issue #2770883: Provide a custom promotion entity form

---
 modules/promotion/commerce_promotion.info.yml      |  1 +
 modules/promotion/commerce_promotion.module        |  3 ++
 modules/promotion/src/Entity/Promotion.php         |  7 +--
 modules/promotion/src/Form/PromotionForm.php       | 56 ++++++++++++++++++++++
 .../templates/commerce-promotion-form.html.twig    | 19 ++++++++
 .../src/FunctionalJavascript/PromotionTest.php     | 10 ++--
 6 files changed, 90 insertions(+), 6 deletions(-)
 create mode 100644 modules/promotion/src/Form/PromotionForm.php
 create mode 100644 modules/promotion/templates/commerce-promotion-form.html.twig

diff --git a/modules/promotion/commerce_promotion.info.yml b/modules/promotion/commerce_promotion.info.yml
index ae127e1..71a8182 100644
--- a/modules/promotion/commerce_promotion.info.yml
+++ b/modules/promotion/commerce_promotion.info.yml
@@ -5,5 +5,6 @@ package: Commerce
 core: 8.x
 dependencies:
   - datetime
+  - inline_entity_form
   - commerce
   - commerce_order
diff --git a/modules/promotion/commerce_promotion.module b/modules/promotion/commerce_promotion.module
index ee21cd7..8675150 100644
--- a/modules/promotion/commerce_promotion.module
+++ b/modules/promotion/commerce_promotion.module
@@ -16,6 +16,9 @@ function commerce_promotion_theme() {
     'commerce_promotion' => [
       'render element' => 'elements',
     ],
+    'commerce_promotion_form' => [
+      'render element' => 'form',
+    ],
   ];
 }
 
diff --git a/modules/promotion/src/Entity/Promotion.php b/modules/promotion/src/Entity/Promotion.php
index ab030c3..2756dc9 100644
--- a/modules/promotion/src/Entity/Promotion.php
+++ b/modules/promotion/src/Entity/Promotion.php
@@ -26,8 +26,9 @@
  *     "list_builder" = "Drupal\commerce_promotion\PromotionListBuilder",
  *     "views_data" = "Drupal\views\EntityViewsData",
  *     "form" = {
- *       "add" = "Drupal\Core\Entity\ContentEntityForm",
- *       "edit" = "Drupal\Core\Entity\ContentEntityForm",
+ *       "default" = "Drupal\commerce_promotion\Form\PromotionForm",
+ *       "add" = "Drupal\commerce_promotion\Form\PromotionForm",
+ *       "edit" = "Drupal\commerce_promotion\Form\PromotionForm",
  *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
  *     },
  *     "route_provider" = {
@@ -269,7 +270,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
       ->setLabel(t('Coupons'))
       ->setDescription(t('Coupons which allow promotion to be redeemed.'))
       ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
-      ->setRequired(TRUE)
+      ->setRequired(FALSE)
       ->setSetting('target_type', 'commerce_promotion_coupon')
       ->setSetting('handler', 'default')
       ->setTranslatable(TRUE)
diff --git a/modules/promotion/src/Form/PromotionForm.php b/modules/promotion/src/Form/PromotionForm.php
new file mode 100644
index 0000000..3d80b6c
--- /dev/null
+++ b/modules/promotion/src/Form/PromotionForm.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Drupal\commerce_promotion\Form;
+
+use Drupal\Core\Entity\ContentEntityForm;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Link;
+
+/**
+ * Defines the promotion add/edit form.
+ */
+class PromotionForm extends ContentEntityForm {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildForm(array $form, FormStateInterface $form_state) {
+    // Skip building the form if there are no available stores.
+    $store_query = $this->entityManager->getStorage('commerce_store')->getQuery();
+    if ($store_query->count()->execute() == 0) {
+      $link = Link::createFromRoute('Add a new store.', 'entity.commerce_store.add_page');
+      $form['warning'] = [
+        '#markup' => t("Promotions can't be created until a store has been added. @link", ['@link' => $link->toString()]),
+      ];
+      return $form;
+    }
+
+    return parent::buildForm($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function form(array $form, FormStateInterface $form_state) {
+    /* @var \Drupal\commerce_promotion\Entity\Promotion $promotion */
+    $promotion = $this->entity;
+    $form = parent::form($form, $form_state);
+
+    $form['#tree'] = TRUE;
+    $form['#theme'] = ['commerce_promotion_form'];
+
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function save(array $form, FormStateInterface $form_state) {
+    /* @var \Drupal\commerce_promotion\Entity\Promotion $promotion */
+    $promotion = $this->getEntity();
+    $promotion->save();
+    drupal_set_message($this->t('The promotion %label has been successfully saved.', ['%label' => $promotion->label()]));
+    $form_state->setRedirect('entity.commerce_promotion.collection', ['commerce_promotion' => $promotion->id()]);
+  }
+
+}
diff --git a/modules/promotion/templates/commerce-promotion-form.html.twig b/modules/promotion/templates/commerce-promotion-form.html.twig
new file mode 100644
index 0000000..1a6c2ce
--- /dev/null
+++ b/modules/promotion/templates/commerce-promotion-form.html.twig
@@ -0,0 +1,19 @@
+{#
+/**
+ * @file
+ * Template for the promotion add/edit form.
+ *
+ * Available variables:
+ * - form: The form.
+ *
+ * @ingroup themeable
+ */
+#}
+<div class="layout-promotion-form clearfix">
+  <div class="layout-region layout-region-promotion-main">
+    {{ form|without('actions') }}
+  </div>
+  <div class="layout-region layout-region-promotion-footer">
+    {{ form.actions }}
+  </div>
+</div>
diff --git a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php
index 218704d..c3dc8f0 100644
--- a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php
+++ b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php
@@ -22,7 +22,7 @@ class PromotionTest extends CommerceBrowserTestBase {
    *
    * @var array
    */
-  public static $modules = ['block', 'commerce_promotion'];
+  public static $modules = ['block', 'datetime', 'commerce_promotion'];
 
   /**
    * {@inheritdoc}
@@ -37,6 +37,8 @@ protected function getAdministratorPermissions() {
    * Tests creating a promotion.
    */
   public function testCreatePromotion() {
+    $this->createStore(NULL, NULL, 'default', TRUE);
+
     $this->drupalGet('admin/commerce/promotions');
     $this->getSession()->getPage()->clickLink('Add a new promotion');
 
@@ -48,8 +50,8 @@ public function testCreatePromotion() {
       'name[0][value]' => $name,
     ];
     $this->submitForm($edit, t('Save'));
-    $this->assertSession()->pageTextContains("Saved the $name promotion.");
-    $promotion_count = $this->getSession()->getPage()->find('css', '.view-commerce-promotions tr td.views-field-name');
+    $this->assertSession()->pageTextContains("The promotion $name has been successfully saved.");
+    $promotion_count = $this->getSession()->getPage()->find('xpath', '//table/tbody/tr/td[text()="' . $name . '"]');
     $this->assertEquals(count($promotion_count), 1, 'promotions exists in the table.');
   }
 
@@ -57,6 +59,8 @@ public function testCreatePromotion() {
    * Tests editing a promotion.
    */
   public function testEditPromotion() {
+    $this->createStore(NULL, NULL, 'default', TRUE);
+
     $promotion = $this->createEntity('commerce_promotion', [
       'name' => $this->randomMachineName(8),
     ]);
