CRM_Admin_Form - Support standalone forms
authorColeman Watts <coleman@civicrm.org>
Wed, 19 Oct 2022 12:12:55 +0000 (08:12 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 19 Oct 2022 12:12:55 +0000 (08:12 -0400)
This type of form was traditionally embedded in a page, with values like _id and _action
`set()` by the page controller.

This allows Admin forms to be placed at their own url without any refactoring.

CRM/Admin/Form.php
CRM/Admin/Form/ContactType.php
CRM/Admin/Form/LocationType.php
CRM/Admin/Form/PaymentProcessor.php
CRM/Admin/Form/RelationshipType.php
CRM/Tag/Form/Edit.php

index e7d06df6689a5bc917be586a9be3a3d550261cc9..70ab548a5100c044a30d06f69a64c5640495f65e 100644 (file)
@@ -49,14 +49,24 @@ class CRM_Admin_Form extends CRM_Core_Form {
   }
 
   /**
-   * Basic setup.
+   * Note: This type of form was traditionally embedded in a page, with values like _id and _action
+   * being `set()` by the page controller.
+   * Nowadays the preferred approach is to place these forms at their own url, so this function
+   * handles both scenarios. It will retrieve id either from a value stored by the page controller
+   * if embedded, or from the url if standalone.
    */
   public function preProcess() {
     Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
     Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js');
 
-    $this->_id = $this->get('id');
+    // Lookup id from URL or stored value in controller
+    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
+
     $this->_BAOName = $this->get('BAOName');
+    // If BAOName not explicitly set, look it up from the api entity name
+    if (!$this->_BAOName) {
+      $this->_BAOName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity()));
+    }
     $this->_values = [];
     if (isset($this->_id)) {
       $params = ['id' => $this->_id];
index 676a7e4e9758e87781be8fa78f049e9808d8e4d3..20bedf59c5ccdb7034aeb893922449a3bffb318e 100644 (file)
  */
 class CRM_Admin_Form_ContactType extends CRM_Admin_Form {
 
-  public function preProcess(): void {
-    CRM_Utils_Request::retrieve('action', 'String', $this);
-    CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
-    $this->set('BAOName', 'CRM_Contact_BAO_ContactType');
-    parent::preProcess();
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'ContactType';
   }
 
   /**
index dfadc78188615dc28fb3ad4911db43bf1c6cf312..bfbc0efa2202ee8a6cdb8d5660d2aba53c6e1569 100644 (file)
@@ -25,11 +25,11 @@ class CRM_Admin_Form_LocationType extends CRM_Admin_Form {
    */
   public $submitOnce = TRUE;
 
-  public function preProcess(): void {
-    CRM_Utils_Request::retrieve('action', 'String', $this);
-    CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
-    $this->set('BAOName', 'CRM_Core_BAO_LocationType');
-    parent::preProcess();
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'LocationType';
   }
 
   /**
index a63dc1016f53d733730a6a892724f4b6b3ae3a74..1700337313ee45e33c94c4f36af00070b33eaf5d 100644 (file)
@@ -106,9 +106,6 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
    * @throws \CRM_Core_Exception
    */
   public function preProcess() {
-    CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
-    $this->set('BAOName', 'CRM_Financial_BAO_PaymentProcessor');
-
     parent::preProcess();
 
     $this->setPaymentProcessorTypeID();
index 43914d6a72fbb2a0ffc1abb5e3a909d025582a9a..544807eb504ec304dcf2e3c63a84453455d809b4 100644 (file)
@@ -22,6 +22,8 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
 
   use CRM_Core_Form_EntityFormTrait;
 
+  protected $_BAOName = 'CRM_Contact_BAO_RelationshipType';
+
   /**
    * Fields for the entity to be assigned to the template.
    *
@@ -41,13 +43,6 @@ class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
    */
   protected $entityFields = [];
 
-  public function preProcess(): void {
-    CRM_Utils_Request::retrieve('action', 'String', $this);
-    CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
-    $this->set('BAOName', 'CRM_Contact_BAO_RelationshipType');
-    parent::preProcess();
-  }
-
   /**
    * Set entity fields to be assigned to the form.
    */
index 1800246cfc7cae07230b7c90cf837d34d103b3ba..71fe9e8b47952651d8fcdcf5c05cd7268ed6ed9a 100644 (file)
@@ -28,12 +28,6 @@ class CRM_Tag_Form_Edit extends CRM_Admin_Form {
     return 'Tag';
   }
 
-  public function preProcess() {
-    CRM_Utils_Request::retrieve('id', 'Integer', $this, FALSE);
-    $this->set('BAOName', 'CRM_Core_BAO_Tag');
-    parent::preProcess();
-  }
-
   /**
    * Build the form object.
    */