Skip to content

Commit

Permalink
Function extraction (Move towards generic custom data support for all…
Browse files Browse the repository at this point in the history
… forms)
  • Loading branch information
mattwire authored and eileenmcnaughton committed May 8, 2018
1 parent 85b68a1 commit eb69c38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
35 changes: 33 additions & 2 deletions CRM/Custom/Form/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,44 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
* $Id$
*
*/

/**
* this class builds custom data
*/
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
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 3 additions & 11 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// CRM-4395, get the online pending contribution id.
$this->_onlinePendingContributionId = NULL;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit eb69c38

Please sign in to comment.