diff --git a/CRM/Custom/Form/CustomData.php b/CRM/Custom/Form/CustomData.php index 91cd7409318b..09cfeec4807f 100644 --- a/CRM/Custom/Form/CustomData.php +++ b/CRM/Custom/Form/CustomData.php @@ -38,6 +38,35 @@ */ 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. + * + * You also have to add something like this to the postProcess: + * $params['custom'] = CRM_Core_BAO_CustomField::postProcess($submitted, $this->_id, $this->getDefaultEntity()); + * + * @param CRM_Core_Form $form + * @param null|string $subName + * @param null|string $subType + * @param null|int $groupCount + * + * @throws \CRM_Core_Exception + */ + public static function addToForm(&$form, $subName = NULL, $subType = 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, diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 3fad351fffce..39997389e419 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -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. * diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 6c98758924bb..37fdd965bd82 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -243,12 +243,9 @@ 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); - } + + // Add custom data to form + CRM_Custom_Form_CustomData::addToForm($this, $this->_memType, 1); // CRM-4395, get the online pending contribution id. $this->_onlinePendingContributionId = NULL; @@ -455,11 +452,6 @@ public function buildQuickForm() { $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( diff --git a/api/v3/utils.php b/api/v3/utils.php index 9948ae9e6bdd..a0fb1c67ff69 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1117,7 +1117,7 @@ function _civicrm_api3_format_params_for_create(&$params, $entity) { } $values = array(); _civicrm_api3_custom_format_params($params, $values, $entity); - $params = array_merge($params, $values); + $params = CRM_Utils_Array::crmArrayMerge($params, $values); } /**