Function extraction (Move towards generic custom data support for all forms)
authorMatthew Wire <devel@mrwire.co.uk>
Sun, 6 May 2018 17:09:40 +0000 (18:09 +0100)
committereileen <emcnaughton@wikimedia.org>
Tue, 8 May 2018 08:10:08 +0000 (20:10 +1200)
CRM/Custom/Form/CustomData.php
CRM/Event/Form/Participant.php
CRM/Member/Form.php
CRM/Member/Form/Membership.php

index 91cd7409318bbbb2be2f2ac5710f9f6fa60f9756..0473b33572a612cd38c45adf97ba2646199fb3c4 100644 (file)
@@ -29,8 +29,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2018
- * $Id$
- *
  */
 
 /**
  */
 class CRM_Custom_Form_CustomData {
 
+  /**
+   * Generic wrapper to add custom data to a form via a single line in preProcess.
+   *
+   * $this->getDefaultEntity() must be defined for the form class for this to work.
+   *
+   * If the postProcess form cannot use the api & instead uses a BAO function it will need.
+   *   $params['custom'] = CRM_Core_BAO_CustomField::postProcess($submitted, $this->_id, $this->getDefaultEntity());
+   *
+   * @param CRM_Core_Form $form
+   * @param null|string $subType values stored in civicrm_custom_group.extends_entity_column_value
+   *   e.g Student for contact type
+   * @param null|string $subName value in civicrm_custom_group.extends_entity_column_id
+   * @param null|int $groupCount number of entities that could have custom data
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public static function addToForm(&$form, $subType = NULL, $subName = NULL, $groupCount = 1) {
+    $entityName = $form->getDefaultEntity();
+
+    // when custom data is included in this page
+    if (!empty($_POST['hidden_custom'])) {
+      self::preProcess($form, $subName, $subType, $groupCount, $entityName, $form->_id);
+      self::buildQuickForm($form);
+      self::setDefaultValues($form);
+    }
+    // need to assign custom data type and subtype to the template
+    $form->assign('customDataType', $entityName);
+    $form->assign('customDataSubType', $subType);
+    $form->assign('entityID', $form->_id);
+  }
+
   /**
    * @param CRM_Core_Form $form
    * @param null|string $subName
@@ -46,6 +75,8 @@ class CRM_Custom_Form_CustomData {
    * @param string $type
    * @param null|int $entityID
    * @param null $onlySubType
+   *
+   * @throws \CRM_Core_Exception
    */
   public static function preProcess(
     &$form, $subName = NULL, $subType = NULL,
index 0de0114de4f0c6273efae289c576a3222ae6cb9e..6f8d6613062d858db7f93a6e7b7eae6ae9695622 100644 (file)
@@ -71,7 +71,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
   public $_priceSet;
 
   /**
-   * The id of the participation that we are proceessing.
+   * The id of the participation that we are processing.
    *
    * @var int
    */
@@ -372,6 +372,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
 
     // when custom data is included in this page
     if (!empty($_POST['hidden_custom'])) {
+      $eventId = (int) CRM_Utils_Array::value('event_id', $_POST);
       // Custom data of type participant role
       // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string,
       //       not sure if that ever really happens
@@ -384,13 +385,13 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       }
 
       //custom data of type participant event
-      CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $_POST['event_id'], 1, 'Participant', $this->_id);
+      CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $eventId, 1, 'Participant', $this->_id);
       CRM_Custom_Form_CustomData::buildQuickForm($this);
       CRM_Custom_Form_CustomData::setDefaultValues($this);
 
       // custom data of type participant event type
       $eventTypeId = NULL;
-      if ($eventId = CRM_Utils_Array::value('event_id', $_POST)) {
+      if ($eventId) {
         $eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'event_type_id', 'id');
       }
       CRM_Custom_Form_CustomData::preProcess($this, $this->_eventTypeCustomDataTypeID, $eventTypeId,
@@ -1209,10 +1210,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       );
       $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
       $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
-      $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
-        $this->_id,
-        'Participant'
-      );
+
+      $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, $this->getDefaultEntity());
     }
 
     //do cleanup line  items if participant edit the Event Fee.
index 3fad351fffceddedced8ec86301db982b027ae31..39997389e419729cf332c4ddc83b1881570d3539 100644 (file)
@@ -84,6 +84,13 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
    */
   public $_priceSet;
 
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'Membership';
+  }
+
   /**
    * Values submitted to the form, processed along the way.
    *
index 6c98758924bb2253f955ecf33afacaf10bc67ac3..5a7ad1428ebf66e566557e1219d2a552c656963e 100644 (file)
@@ -243,12 +243,9 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
         $this->_memType = $params['membership_type_id'][1];
       }
     }
-    // when custom data is included in this page
-    if (!empty($_POST['hidden_custom'])) {
-      CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_memType, 1, 'Membership', $this->_id);
-      CRM_Custom_Form_CustomData::buildQuickForm($this);
-      CRM_Custom_Form_CustomData::setDefaultValues($this);
-    }
+
+    // Add custom data to form
+    CRM_Custom_Form_CustomData::addToForm($this, $this->_memType);
 
     // CRM-4395, get the online pending contribution id.
     $this->_onlinePendingContributionId = NULL;
@@ -455,11 +452,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       $this->assign('hasPriceSets', $buildPriceSet);
     }
 
-    //need to assign custom data type and subtype to the template
-    $this->assign('customDataType', 'Membership');
-    $this->assign('customDataSubType', $this->_memType);
-    $this->assign('entityID', $this->_id);
-
     if ($this->_action & CRM_Core_Action::DELETE) {
       $this->addButtons(array(
         array(