From 9b897393573beedb4ddcd2faae7ca40bd334c064 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sat, 31 Mar 2018 11:47:44 +0100 Subject: [PATCH] Consolidate code to add custom fields to form --- CRM/Contribute/Form/AbstractEditPayment.php | 17 +---- CRM/Contribute/Form/Contribution.php | 10 +-- CRM/Custom/Form/CustomDataTrait.php | 77 +++++++++++++++++++++ CRM/Member/Form/Membership.php | 8 +-- 4 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 CRM/Custom/Form/CustomDataTrait.php diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 353bcad960e1..025dea779294 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -46,6 +46,7 @@ * */ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { + use CRM_Custom_Form_CustomDataTrait; public $_mode; public $_action; @@ -302,22 +303,6 @@ public function buildValuesAndAssignOnline_Note_Type($id, &$values) { $this->_contributionType = $values['financial_type_id']; } - /** - * @param string $type - * Eg 'Contribution'. - * @param string $subType - * @param int $entityId - */ - public function applyCustomData($type, $subType, $entityId) { - $this->set('type', $type); - $this->set('subType', $subType); - $this->set('entityId', $entityId); - - CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId); - CRM_Custom_Form_CustomData::buildQuickForm($this); - CRM_Custom_Form_CustomData::setDefaultValues($this); - } - /** * @param int $id * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find(); diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 07daaa7368df..b8756d8e7463 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -274,10 +274,7 @@ public function preProcess() { $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); } - // when custom data is included in this page - if (!empty($_POST['hidden_custom'])) { - $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); - } + $this->addCustomDataToFormIfSubmitted('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); $this->_lineItems = array(); if ($this->_id) { @@ -601,11 +598,6 @@ public function buildQuickForm() { $this->applyFilter('__ALL__', 'trim'); - //need to assign custom data type and subtype to the template - $this->assign('customDataType', 'Contribution'); - $this->assign('customDataSubType', $this->_contributionType); - $this->assign('entityID', $this->_id); - if ($this->_context == 'standalone') { $this->addEntityRef('contact_id', ts('Contact'), array( 'create' => TRUE, diff --git a/CRM/Custom/Form/CustomDataTrait.php b/CRM/Custom/Form/CustomDataTrait.php new file mode 100644 index 000000000000..404fd43cba35 --- /dev/null +++ b/CRM/Custom/Form/CustomDataTrait.php @@ -0,0 +1,77 @@ +set('type', $type); + $this->set('subType', $subType); + $this->set('entityId', $entityId); + + $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); + CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId); + CRM_Custom_Form_CustomData::buildQuickForm($this); + CRM_Custom_Form_CustomData::setDefaultValues($this); + + // Assign custom data type and subtype to the template so that custom data edit elements can be built via javascript/ajax. + $this->assign('customDataType', $type); + $this->assign('customDataSubType', $subType); + $this->assign('entityID', $entityId); + } + + /** + * If the custom data is in the submitted data (eg. added via ajax loaded form) add to form. + * + * @param string $type Eg 'Contact'. + * @param string $subType Eg 'Individual' + * @param int $entityID + * + * @throws \CRM_Core_Exception + */ + public function addCustomDataToFormIfSubmitted($entity, $subType, $entityID) { + // when custom data is included in this page + if (!empty($_POST['hidden_custom'])) { + $this->applyCustomData($entity, $subType, $entityID); + } + } + +} diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index fa30cf4e67e3..995beea9096c 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -243,12 +243,8 @@ public function preProcess() { $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); - } + + $this->addCustomDataToFormIfSubmitted('Membership', $this->_memType, $this->_id); // CRM-4395, get the online pending contribution id. $this->_onlinePendingContributionId = NULL;