commit 2018d773325610a77abbedac9722363432e4834f
Author: Bart Feenstra <bart@mynameisbart.com>
Date:   Wed Oct 9 15:37:42 2013 +0200

    Issue #2107979 by Xano: Port the payment edit form.

diff --git a/payment/lib/Drupal/payment/Entity/Payment.php b/payment/lib/Drupal/payment/Entity/Payment.php
index 7571327..a1f9073 100644
--- a/payment/lib/Drupal/payment/Entity/Payment.php
+++ b/payment/lib/Drupal/payment/Entity/Payment.php
@@ -25,7 +25,8 @@ use Drupal\payment\Plugin\payment\status\PaymentStatusInterface;
  *   controllers = {
  *     "access" = "Drupal\payment\Entity\PaymentAccessController",
  *     "form" = {
- *       "delete" = "Drupal\payment\Entity\PaymentDeleteFormController"
+ *       "delete" = "Drupal\payment\Entity\PaymentDeleteFormController",
+ *       "edit" = "Drupal\payment\Entity\PaymentEditFormController"
  *     },
  *     "list" = "Drupal\payment\Entity\PaymentListController",
  *     "render" = "Drupal\payment\Entity\PaymentRenderController",
@@ -40,7 +41,8 @@ use Drupal\payment\Plugin\payment\status\PaymentStatusInterface;
  *   id = "payment",
  *   label = @Translation("Payment"),
  *   links = {
- *     "canonical" = "/payment/{payment}"
+ *     "canonical" = "/payment/{payment}",
+ *     "edit-form" = "/payment/{payment}/edit"
  *   },
  *   module = "payment",
  *   route_base_path = "admin/config/services/payment/type/{bundle}"
diff --git a/payment/lib/Drupal/payment/Entity/PaymentEditFormController.php b/payment/lib/Drupal/payment/Entity/PaymentEditFormController.php
new file mode 100644
index 0000000..01baf32
--- /dev/null
+++ b/payment/lib/Drupal/payment/Entity/PaymentEditFormController.php
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\payment\Entity\PaymentEditFormController.
+ */
+
+namespace Drupal\payment\Entity;
+
+use Drupal\Core\Entity\EntityFormController;
+use Drupal\payment\Element\PaymentPaymentMethodInput;
+use Drupal\payment\Plugin\payment\status\Manager;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Provides the payment edit form.
+ */
+class PaymentEditFormController extends EntityFormController {
+
+  /**
+   * The payment status plugin manager.
+   *
+   * @var \Drupal\payment\Plugin\payment\status\Manager
+   */
+  protected $paymentStatusManager;
+
+  /**
+   * Constructor.
+   *
+   * @param \Drupal\payment\Plugin\payment\status\Manager The payment status
+   *   plugin manager.
+   */
+  function __construct(Manager $payment_status_manager) {
+    $this->paymentStatusManager = $payment_status_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static($container->get('plugin.manager.payment.status'));
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function form(array $form, array &$form_state) {
+    $payment = $this->getEntity();
+    $form['status_plugin_id'] = array(
+      '#default_value' => $payment->getStatus()->getPluginId(),
+      '#description' => t('Updating a payment status manually can disrupt automatic payment processing.'),
+      '#options' => $this->paymentStatusManager->options(),
+      '#title' => t('Status'),
+      '#type' => 'select',
+    );
+
+    return parent::form($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submit(array $form, array &$form_state) {
+    parent::submit($form, $form_state);
+    $payment = $this->getEntity();
+    $payment->save();
+    $uri = $payment->uri();
+    $form_state['redirect'] = $uri['path'];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildEntity(array $form, array &$form_state) {
+    $payment = $this->getEntity();
+    if ($form_state['values']['status_plugin_id'] != $payment->getStatus()->getPluginId()) {
+      $status = $this->paymentStatusManager
+        ->createInstance($form_state['values']['status_plugin_id']);
+      $payment->setStatus($status);
+    }
+
+    return $payment;
+  }
+}
diff --git a/payment/lib/Drupal/payment/Tests/PaymentUIWebTest.php b/payment/lib/Drupal/payment/Tests/PaymentUIWebTest.php
index f837d84..5fd669d 100644
--- a/payment/lib/Drupal/payment/Tests/PaymentUIWebTest.php
+++ b/payment/lib/Drupal/payment/Tests/PaymentUIWebTest.php
@@ -73,6 +73,22 @@ class PaymentUIWebTest extends WebTestBase {
       $this->assertText(t('Status'));
     }
 
+    // Update the payment.
+    $path = 'payment/' . $payment->id() . '/edit';
+    $this->drupalGet($path);
+    $this->assertResponse('403');
+    $this->drupalLogin($this->drupalCreateUser(array('payment.payment.update.any')));
+    $this->drupalGet($path);
+    if ($this->assertResponse('200')) {
+      $this->assertFieldByXPath('//select[@name="status_plugin_id"]');
+      $this->drupalPostForm(NULL, array(
+        'status_plugin_id' => 'payment_cancelled',
+      ), t('Save'));
+    }
+    $this->assertUrl('payment/' . $payment->id());
+    $payment = entity_load_unchanged('payment', $payment->id());
+    $this->assertEqual($payment->getStatus()->getPluginId(), 'payment_cancelled');
+
     // Delete a payment.
     $path = 'payment/' . $payment->id() . '/delete';
     $this->drupalGet($path);
diff --git a/payment/payment.module b/payment/payment.module
index 58299c6..758906b 100644
--- a/payment/payment.module
+++ b/payment/payment.module
@@ -32,16 +32,11 @@ function payment_menu() {
     'weight' => -10,
   );
   $items['payment/%entity_object/edit'] = array(
-    'load arguments' => array('payment'),
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('payment_form_standalone', 1),
-    'access callback' => 'payment_access',
-    'access arguments' => array('update', 1),
+    'route_name' => 'payment.payment.edit',
     'title' => 'Edit',
     'type' => MENU_LOCAL_TASK,
     'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
     'weight' => -9,
-    'file' => 'payment.ui.inc',
   );
 
   // Administration section.
diff --git a/payment/payment.routing.yml b/payment/payment.routing.yml
index 02a3371..2ea4a9c 100644
--- a/payment/payment.routing.yml
+++ b/payment/payment.routing.yml
@@ -21,6 +21,13 @@ payment.payment.view:
   requirements:
     _entity_access: 'payment.view'
 
+payment.payment.edit:
+  path: '/payment/{payment}/edit'
+  defaults:
+    _entity_form: 'payment.edit'
+  requirements:
+    _entity_access: 'payment.update'
+
 payment.payment.delete:
   path: '/payment/{payment}/delete'
   defaults:
