forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom data on the Relationship Type form IF enabled via exte…
…nsion. This is an effort at a way to genericise core forms that basically exist to do crud on an entity. We have a number of fairly straight forward forms of this type in core and, in order to allow extensions to use custom fields on a range of entities we should support editing them on these core crud forms. To add custom data support to an entity we need to a) add the custom data to the form using CRM_Custom_Form_CustomData::addToForm($this); b) ensure that the entity is saved using an api call not a BAO call c) add the custom data block to the tpl file - ie {include file="CRM/common/customDataBlock.tpl"} (the above is possible due to previous work to add support & simplify) In this PR the adding of the customData is done in the EntityFormTrait, the api is previously converted and the custom data is included by using a generic tpl to support metadata applied to the form. By using metadata on the form we can also give extension writers a lot more control over what is on the form as they can add to, alter, or remove the metadata in a php hook. This is the crux of this issue civicrm#12078 & it likewise is looking to use a generic field template to add fields based on metadata. A key difference between the 2 prs is that one uses divs & the other a table & that might preclude close sharing of the approach. Note this PR DOES have the impact of adding translate links to 2 localisable relationship type fields that did not currently have them - I think this is a good thing? Example of how to enable custom fields for RelationshipType in an extension. ``` civicrm_api3('OptionValue', 'create', [ 'option_group_id' => 'cg_extend_objects', 'name' => 'civicrm_relationship_type', 'label' => ts('Relationship Type'), 'value' => 'RelationshipType', ]); ```
- Loading branch information
1 parent
bcf8b46
commit d84ae5f
Showing
6 changed files
with
319 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 4.7 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2018 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License and the CiviCRM Licensing Exception along | | ||
| with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2018 | ||
*/ | ||
|
||
trait CRM_Core_Form_EntityFormTrait { | ||
/** | ||
* Get entity fields for the entity to be added to the form. | ||
* | ||
* @var array | ||
*/ | ||
public function getEntityFields() { | ||
return $this->entityFields; | ||
} | ||
|
||
/** | ||
* Explicitly declare the form context. | ||
*/ | ||
public function getDefaultContext() { | ||
return 'create'; | ||
} | ||
|
||
/** | ||
* Get entity fields for the entity to be added to the form. | ||
* | ||
* @var array | ||
*/ | ||
public function getDeleteMessage() { | ||
return $this->deleteMessage; | ||
} | ||
|
||
/** | ||
* Get the entity id being edited. | ||
* | ||
* @return int|null | ||
*/ | ||
public function getEntityId() { | ||
return $this->_id; | ||
} | ||
/** | ||
* If the custom data is in the submitted data (eg. added via ajax loaded form) add to form. | ||
*/ | ||
public function addCustomDataToForm() { | ||
$customisableEntities = CRM_Core_SelectValues::customGroupExtends(); | ||
if (isset($customisableEntities[$this->getDefaultEntity()])) { | ||
CRM_Custom_Form_CustomData::addToForm($this); | ||
} | ||
} | ||
|
||
/** | ||
* Build the form object. | ||
*/ | ||
public function buildQuickEntityForm() { | ||
if ($this->_action & CRM_Core_Action::DELETE) { | ||
$this->buildDeleteForm(); | ||
return; | ||
} | ||
$this->applyFilter('__ALL__', 'trim'); | ||
$this->addEntityFieldsToTemplate(); | ||
$this->assign('entityFields', $this->entityFields); | ||
$this->assign('entityID', $this->getEntityId()); | ||
$this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity()))); | ||
$this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity()))); | ||
$this->addCustomDataToForm(); | ||
$this->addFormButtons(); | ||
} | ||
|
||
/** | ||
* Build the form for any deletion. | ||
*/ | ||
protected function buildDeleteForm() { | ||
$this->assign('deleteMessage', $this->getDeleteMessage()); | ||
$this->addFormButtons(); | ||
} | ||
|
||
/** | ||
* Add relevant buttons to the form. | ||
*/ | ||
protected function addFormButtons() { | ||
if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { | ||
$this->addButtons(array( | ||
array( | ||
'type' => 'cancel', | ||
'name' => ts('Done'), | ||
'isDefault' => TRUE, | ||
), | ||
) | ||
); | ||
} | ||
else { | ||
$this->addButtons(array( | ||
array( | ||
'type' => 'next', | ||
'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), | ||
'isDefault' => TRUE, | ||
), | ||
array( | ||
'type' => 'cancel', | ||
'name' => ts('Cancel'), | ||
), | ||
) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Set translated fields. | ||
* | ||
* This function is called from the class constructor, allowing us to set | ||
* fields on the class that can't be set as properties due to need for | ||
* translation or other non-input specific handling. | ||
*/ | ||
protected function setTranslatedFields() { | ||
$this->setEntityFields(); | ||
$this->setDeleteMessage(); | ||
$metadata = civicrm_api3($this->getDefaultEntity(), 'getfields', ['action' => 'create']); | ||
$this->metadata = $metadata['values']; | ||
foreach ($this->metadata as $fieldName => $spec) { | ||
if (isset($this->entityFields[$fieldName])) { | ||
if ($spec['localizable']) { | ||
$this->entityFields[$fieldName]['is_add_translate_dialog'] = TRUE; | ||
} | ||
if (empty($spec['html'])) { | ||
$this->entityFields[$fieldName]['not-auto-addable'] = TRUE; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add defined entity field to template. | ||
*/ | ||
protected function addEntityFieldsToTemplate() { | ||
foreach ($this->getEntityFields() as $fieldSpec) { | ||
if (empty($fieldSpec['not-auto-addable'])) { | ||
$this->addField($fieldSpec['name']); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 5 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2018 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License and the CiviCRM Licensing Exception along | | ||
| with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*} | ||
{* this template is used for adding/editing entities *} | ||
<div class="crm-block crm-form-block crm-{$entityInClassFormat}-form-block"> | ||
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div> | ||
{if $action eq 8} | ||
<div class="messages status no-popup"> | ||
<div class="icon inform-icon"></div> | ||
{$deleteMessage|escape} | ||
</div> | ||
{else} | ||
<table class="form-layout-compressed"> | ||
{foreach from=$entityFields item=fieldSpec} | ||
{assign var=fieldName value=$fieldSpec.name} | ||
<tr class="crm-{$entityInClassFormat}-form-block-{$fieldName}"> | ||
{include file="CRM/Core/Form/Field.tpl"} | ||
</tr> | ||
{/foreach} | ||
</table> | ||
{include file="CRM/common/customDataBlock.tpl"} | ||
{/if} | ||
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div> | ||
</div> |
Oops, something went wrong.