Skip to content

Commit

Permalink
Merge pull request #13578 from mattwire/entityform_view_towards
Browse files Browse the repository at this point in the history
Towards supporting EntityForm for 'View Action'
  • Loading branch information
eileenmcnaughton authored Feb 24, 2019
2 parents 2025d50 + 48c242d commit f0039df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
28 changes: 24 additions & 4 deletions CRM/Core/Form/EntityFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function buildQuickEntityForm() {
$this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity())));
$this->addCustomDataToForm();
$this->addFormButtons();

if ($this->isViewContext()) {
$this->freeze();
}
}

/**
Expand All @@ -128,7 +132,7 @@ protected function buildDeleteForm() {
* Add relevant buttons to the form.
*/
protected function addFormButtons() {
if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) {
if ($this->isViewContext() || $this->_action & CRM_Core_Action::PREVIEW) {
$this->addButtons(array(
array(
'type' => 'cancel',
Expand All @@ -142,7 +146,7 @@ protected function addFormButtons() {
$this->addButtons(array(
array(
'type' => 'next',
'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
'name' => $this->isDeleteContext() ? ts('Delete') : ts('Save'),
'isDefault' => TRUE,
),
array(
Expand All @@ -159,19 +163,26 @@ protected function addFormButtons() {
*/
protected function getEntityDefaults() {
$defaults = [];
if ($this->_action != CRM_Core_Action::DELETE &&
if (!$this->isDeleteContext() &&
$this->getEntityId()
) {
$params = ['id' => $this->getEntityId()];
$baoName = $this->_BAOName;
$baoName::retrieve($params, $defaults);
}
foreach ($this->entityFields as $fieldSpec) {
foreach ($this->entityFields as &$fieldSpec) {
$value = CRM_Utils_Request::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($fieldSpec['name']));
if ($value !== FALSE && $value !== NULL) {
$defaults[$fieldSpec['name']] = $value;
}
if (CRM_Utils_Array::value('formatter', $fieldSpec) == 'crmMoney') {
if (!empty($defaults['currency'])) {
$fieldSpec['formatterParam'] = $defaults['currency'];
}
}
}
// Assign again as we may have modified above
$this->assign('entityFields', $this->entityFields);
return $defaults;
}

Expand Down Expand Up @@ -244,6 +255,15 @@ protected function isDeleteContext() {
return ($this->_action & CRM_Core_Action::DELETE);
}

/**
* Is the form being used in the context of a view.
*
* @return bool
*/
protected function isViewContext() {
return ($this->_action & CRM_Core_Action::VIEW);
}

protected function setEntityFieldsMetadata() {
foreach ($this->entityFields as $field => &$props) {
if (!empty($props['not-auto-addable'])) {
Expand Down
17 changes: 12 additions & 5 deletions CRM/Custom/Form/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ public static function addToForm(&$form, $subType = NULL, $subName = NULL, $grou
$entitySubType = $form->getEntitySubTypeId($subType);
}

// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
self::buildQuickForm($form);
self::setDefaultValues($form);
if ($form->getAction() == CRM_Core_Action::VIEW) {
// Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template)
$groupTree = CRM_Core_BAO_CustomGroup::getTree($entityName, NULL, $entityID, 0, $entitySubType);
CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID);
}
else {
// Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template)
if (!empty($_POST['hidden_custom'])) {
self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID);
self::buildQuickForm($form);
self::setDefaultValues($form);
}
}
// need to assign custom data type and subtype to the template
$form->assign('customDataType', $entityName);
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Core/Form/Field.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{/capture}{help id=$help.id file=$help.file}{/if}
{if $action == 2 && $fieldSpec.is_add_translate_dialog}{include file='CRM/Core/I18n/Dialog.tpl' table=$entityTable field=$fieldName id=$entityID}{/if}
</td>
<td>{$fieldSpec.pre_html_text}{if $form.$fieldName.html}{if $fieldSpec.formatter === 'crmMoney'}{$form.$fieldName.html|crmMoney}{else}{$form.$fieldName.html}{/if}{else}{$fieldSpec.place_holder}{/if}{$fieldSpec.post_html_text}<br />
<td>{$fieldSpec.pre_html_text}{if $form.$fieldName.html}{if $fieldSpec.formatter === 'crmMoney'}{$form.$fieldName.html|crmMoney:$fieldSpec.formatterParam}{else}{$form.$fieldName.html}{/if}{else}{$fieldSpec.place_holder}{/if}{$fieldSpec.post_html_text}<br />
{if $fieldSpec.description}<span class="description">{$fieldSpec.description}</span>{/if}
{if $fieldSpec.documentation_link}{docURL page=$fieldSpec.documentation_link.page resource=$fieldSpec.documentation_link.resource}{/if}
</td>
Expand Down

0 comments on commit f0039df

Please sign in to comment.