Skip to content

Commit

Permalink
Merge pull request #29651 from eileenmcnaughton/grant
Browse files Browse the repository at this point in the history
[php8.2] Apply custom data handling improvements to grant form
  • Loading branch information
eileenmcnaughton authored Mar 7, 2024
2 parents c2eb52a + 7f00e6d commit 8ebeaf9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
44 changes: 28 additions & 16 deletions ext/civigrant/CRM/Grant/Form/Grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
*
*/
class CRM_Grant_Form_Grant extends CRM_Core_Form {
use CRM_Custom_Form_CustomDataTrait;

/**
* The id of the grant when ACTION is update or delete.
*
* @var int
* @var int|null
*/
protected $_id;
protected ?int $_id;

/**
* The id of the contact associated with this contribution.
Expand All @@ -45,9 +46,8 @@ public function getDefaultEntity() {
*/
public function preProcess() {

$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->_grantType = NULL;
if ($this->_id) {
if ($this->getGrantID()) {
$this->_grantType = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id');
$this->_contactID = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'contact_id');
}
Expand Down Expand Up @@ -80,16 +80,29 @@ public function preProcess() {
}
}

// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
$grantTypeId = empty($_POST['grant_type_id']) ? NULL : $_POST['grant_type_id'];
$this->set('type', 'Grant');
$this->set('subType', $grantTypeId);
$this->set('entityId', $this->_id);
CRM_Custom_Form_CustomData::preProcess($this, NULL, $grantTypeId, 1, 'Grant', $this->_id);
CRM_Custom_Form_CustomData::buildQuickForm($this);
CRM_Custom_Form_CustomData::setDefaultValues($this);
if ($this->isSubmitted()) {
// The custom data fields are added to the form by an ajax form.
// However, if they are not present in the element index they will
// not be available from `$this->getSubmittedValue()` in post process.
// We do not have to set defaults or otherwise render - just add to the element index.
$this->addCustomDataFieldsToForm('Grant', array_filter([
'id' => $this->getGrantID(),
'grant_type_id' => $this->getSubmittedValue('grant_type_id'),
]));
}
}

/**
* @api supported for external use.
*
* @return int|null
* @throws \CRM_Core_Exception
*/
public function getGrantID(): ?int {
if (!isset($this->_id)) {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
}
return $this->_id;
}

/**
Expand Down Expand Up @@ -156,8 +169,7 @@ public function buildQuickForm() {
$attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
$this->addSelect('grant_type_id', ['placeholder' => ts('- select type -'), 'onChange' => "CRM.buildCustomData( 'Grant', this.value );"], TRUE);

//need to assign custom data type and subtype to the template
$this->assign('customDataType', 'Grant');
//need to assign custom data subtype to the template
$this->assign('customDataSubType', $this->_grantType);
$this->assign('entityID', $this->_id);

Expand Down Expand Up @@ -243,7 +255,7 @@ public function postProcess() {
$params['contact_id'] = $this->_contactID;

// build custom data array
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($this->getSubmittedValues(),
$this->_id,
'Grant'
);
Expand Down
2 changes: 1 addition & 1 deletion ext/civigrant/templates/CRM/Grant/Form/Grant.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</tr>
</table>

{include file="CRM/common/customDataBlock.tpl"}
{include file="CRM/common/customDataBlock.tpl" groupID='' customDataType='Grant' cid=false}

<div class="crm-grant-form-block-attachment">
{include file="CRM/Form/attachment.tpl"}
Expand Down

0 comments on commit 8ebeaf9

Please sign in to comment.