Partial conversion of Payment form to use the EntityFormTrait & add title
authoreileen <emcnaughton@wikimedia.org>
Mon, 28 Oct 2019 01:57:27 +0000 (14:57 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 28 Oct 2019 01:57:27 +0000 (14:57 +1300)
This partially converts the form to the new EntityForm format

CRM/Admin/Form/PaymentProcessor.php
CRM/Financial/DAO/PaymentProcessor.php
templates/CRM/Admin/Form/PaymentProcessor.tpl
xml/schema/Financial/PaymentProcessor.xml

index d8b32939550b01676a60c7d8f56a564c36178f32..c997a46e1f033cb7aee3f0395f68edeb99aad0ce 100644 (file)
  * This class generates form components for Payment Processor.
  */
 class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
-  protected $_id = NULL;
-
-  protected $_testID = NULL;
-
-  protected $_fields = NULL;
+  use CRM_Core_Form_EntityFormTrait;
 
   protected $_paymentProcessorDAO;
 
@@ -49,6 +45,49 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
    */
   protected $_paymentProcessorType;
 
+  /**
+   * Fields for the entity to be assigned to the template.
+   *
+   * Fields may have keys
+   *  - name (required to show in tpl from the array)
+   *  - description (optional, will appear below the field)
+   *     Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide)
+   *  - not-auto-addable - this class will not attempt to add the field using addField.
+   *    (this will be automatically set if the field does not have html in it's metadata
+   *    or is not a core field on the form's entity).
+   *  - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
+   *  - template - use a field specific template to render this field
+   *  - required
+   *  - is_freeze (field should be frozen).
+   *
+   * @var array
+   */
+  protected $entityFields = [];
+
+  /**
+   * Set entity fields to be assigned to the form.
+   */
+  protected function setEntityFields() {
+    $this->entityFields = [
+      'payment_processor_type_id' => [
+        'name' => 'payment_processor_type_id',
+        'required' => TRUE,
+      ],
+      'name' => [
+        'name' => 'name',
+        'required' => TRUE,
+      ],
+      'title' => [
+        'name' => 'title',
+      ],
+      'description' => [
+        'name' => 'description',
+      ],
+    ];
+
+    $this->setEntityFieldsMetadata();
+  }
+
   /**
    * Get the name of the base entity being edited.
    *
@@ -58,6 +97,15 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
     return 'PaymentProcessor';
   }
 
+  /**
+   * Set the delete message.
+   *
+   * We do this from the constructor in order to do a translation.
+   */
+  public function setDeleteMessage() {
+    $this->deleteMessage = ts('Deleting this Payment Processor may result in some transaction pages being rendered inactive.') . ' ' . ts('Do you want to continue?');
+  }
+
   public function preProcess() {
     parent::preProcess();
 
@@ -171,18 +219,14 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
    * @param bool $check
    */
   public function buildQuickForm($check = FALSE) {
-    parent::buildQuickForm();
+    $this->buildQuickEntityForm();
 
-    if ($this->_action & CRM_Core_Action::DELETE) {
+    if ($this->isDeleteContext()) {
       return;
     }
 
     $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor');
 
-    $this->add('text', 'name', ts('Name'),
-      $attributes['name'], TRUE
-    );
-
     $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
       'CRM_Financial_DAO_PaymentProcessor',
       $this->_id,
@@ -190,10 +234,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
       CRM_Core_Config::domainID(),
     ]);
 
-    $this->add('text', 'description', ts('Description'),
-      $attributes['description']
-    );
-
+    // @todo - remove this & let the entityForm do it - need to make sure we are handling the js though.
     $this->add('select',
       'payment_processor_type_id',
       ts('Payment Processor Type'),
@@ -383,6 +424,9 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
 
   /**
    * Process the form submission.
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @throws \CRM_Core_Exception
    */
   public function postProcess() {
 
@@ -428,6 +472,8 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
    * @param array $values
    * @param int $domainID
    * @param bool $test
+   *
+   * @throws \CiviCRM_API3_Exception
    */
   public function updatePaymentProcessor(&$values, $domainID, $test) {
     if ($test) {
index 48676c59918f8c84a84f167226dabe569bc8bbb4..339daccc5e3dfb2d261121c2a5e95aa00e4bbf39 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Financial/PaymentProcessor.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:6d3b0b9b13fae223bc45c6c1e4ce7b94)
+ * (GenCodeChecksum:0bd325aff061b6eea4854ef63757772a)
  */
 
 /**
@@ -275,6 +275,9 @@ class CRM_Financial_DAO_PaymentProcessor extends CRM_Core_DAO {
           'entity' => 'PaymentProcessor',
           'bao' => 'CRM_Financial_BAO_PaymentProcessor',
           'localizable' => 0,
+          'html' => [
+            'type' => 'Text',
+          ],
         ],
         'payment_processor_type_id' => [
           'name' => 'payment_processor_type_id',
index 250500270d0e8e9e37aa9cccb56a54c80669278e..9430a872509fac56275935393fee05ff80c2bfff 100644 (file)
 
 {if $action eq 8}
   <div class="messages status no-popup">
-      <div class="icon inform-icon"></div>
-        {ts}WARNING: Deleting this Payment Processor may result in some transaction pages being rendered inactive.{/ts} {ts}Do you want to continue?{/ts}
+    <div class="icon inform-icon"></div>
+    {$deleteMessage|escape}
   </div>
 {else}
   <table class="form-layout-compressed">
-    <tr class="crm-paymentProcessor-form-block-payment_processor_type">
-        <td class="label">{$form.payment_processor_type_id.label}</td><td>{$form.payment_processor_type_id.html} {help id='proc-type'}</td>
-    </tr>
-    <tr class="crm-paymentProcessor-form-block-name">
-        <td class="label">{$form.name.label}</td><td>{$form.name.html}</td>
-    </tr>
-    <tr class="crm-paymentProcessor-form-block-description">
-        <td class="label">{$form.description.label}</td><td>{$form.description.html}</td>
-    </tr>
+    {* This works for the fields managed from the EntityFields trait - see RelationshipType.tpl for end goal in this tpl *}
+    {foreach from=$entityFields item=fieldSpec}
+      {assign var=fieldName value=$fieldSpec.name}
+      <tr class="crm-{$entityInClassFormat}-form-block-{$fieldName}">
+        {include file="CRM/Core/Form/Field.tpl"}
+      </tr>
+    {/foreach}
 
     <tr class="crm-paymentProcessor-form-block-financial_account">
       <td class="label">{$form.financial_account_id.label}</td>
index 76d1ef13bed11d243a7764d49cd5f32c118c2104..ce5e3250abe79117cb51e7cf6cf50b3a08b41605 100644 (file)
@@ -64,6 +64,9 @@
     <title>Processor Description</title>
     <type>varchar</type>
     <length>255</length>
+    <html>
+      <type>Text</type>
+    </html>
     <comment>Payment Processor Description.</comment>
     <add>1.8</add>
   </field>