EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Foreign type use hook to set view
Yuriib5a2c360cc60
diff --git a/application/Espo/Resources/metadata/fields/foreign.json b/application/Espo/Resources/metadata/fields/foreign.jsonindex 9f965f2c05..fe765ee0c9 100644--- a/application/Espo/Resources/metadata/fields/foreign.json+++ b/application/Espo/Resources/metadata/fields/foreign.json@@ -16,11 +16,6 @@"name": "relateOnImport","type": "bool","tooltip": true- },- {- "name": "view",- "type": "varchar",- "hidden": true}],"fieldTypeList": [@@ -69,5 +64,6 @@"notCreatable": false,"fieldDefs": {"readOnly": true- }+ },+ "hookClassName": "Espo\\Tools\\FieldManager\\Hooks\\ForeignType"}diff --git a/application/Espo/Tools/FieldManager/Hooks/ForeignType.php b/application/Espo/Tools/FieldManager/Hooks/ForeignType.phpnew file mode 100644index 0000000000..96d4132954--- /dev/null+++ b/application/Espo/Tools/FieldManager/Hooks/ForeignType.php@@ -0,0 +1,91 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program 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+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Tools\FieldManager\Hooks;++use Espo\Core\Exceptions\Error;+use Espo\Core\Utils\Metadata;++/**+ * @noinspection PhpUnused+ */+class ForeignType+{+ public function __construct(+ private Metadata $metadata,+ ) {}++ /**+ * @param array<string, mixed> $defs+ * @param array<string, mixed> $options+ * @throws Error+ */+ public function afterSave(string $scope, string $name, array $defs, array $options): void+ {+ if (!($options['isNew'] ?? null)) {+ return;+ }++ $link = $defs['link'] ?? null;+ $field = $defs['field'] ?? null;++ if (!$link) {+ throw new Error("No 'link'.");+ }++ if (!$field) {+ throw new Error("No 'field'.");+ }++ $foreignEntityType = $this->metadata->get("entityDefs.$scope.links.$link.entity");++ if (!$foreignEntityType) {+ throw new Error("Link '$link' does not have foreign entity type.");+ }++ $foreignFieldType = $this->metadata->get("entityDefs.$foreignEntityType.fields.$field.type");++ if (!$foreignFieldType) {+ throw new Error("Foreign field '$field' does not have type.");+ }++ $view = $this->metadata->get("fields.foreign.fieldTypeViewMap.$foreignFieldType") ??+ "views/fields/foreign";++ $this->metadata->set('entityDefs', $scope, [+ 'fields' => [+ $name => [+ 'view' => $view,+ ]+ ]+ ]);++ $this->metadata->save();+ }+}diff --git a/client/src/views/admin/field-manager/fields/foreign/field.js b/client/src/views/admin/field-manager/fields/foreign/field.jsindex 779b56a677..a9c0de463f 100644--- a/client/src/views/admin/field-manager/fields/foreign/field.js+++ b/client/src/views/admin/field-manager/fields/foreign/field.js@@ -36,12 +36,6 @@ export default class extends EnumFieldView {if (!this.model.isNew()) {this.wait(this.setReadOnly(true));}-- this.listenTo(this.model, 'change:field', () => {- this.manageField();- });-- this.viewValue = this.model.get('view');}setupOptions() {@@ -78,7 +72,7 @@ export default class extends EnumFieldView {this.params.options = Object.keys(Espo.Utils.clone(fields)).filter(item => {const type = fields[item].type;- if (!~this.typeList.indexOf(type)) {+ if (!this.typeList.inclides(type)) {return;}@@ -106,39 +100,4 @@ export default class extends EnumFieldView {this.params.options.unshift('');}-- manageField() {- if (!this.model.isNew()) {- return;- }-- const link = this.model.get('link');- const field = this.model.get('field');-- if (!link || !field) {- return;- }-- const scope = this.getMetadata().get(['entityDefs', this.options.scope, 'links', link, 'entity']);-- if (!scope) {- return;- }-- const type = this.getMetadata().get(['entityDefs', scope, 'fields', field, 'type']);-- this.viewValue = this.getMetadata().get(['fields', 'foreign', 'fieldTypeViewMap', type]);- }-- fetch() {- const data = super.fetch();-- if (this.model.isNew()) {- if (this.viewValue) {- data['view'] = this.viewValue;- }- }-- return data;- }}diff --git a/tests/integration/Espo/Tools/FieldManager/FieldTypes/ForeignTypeTest.php b/tests/integration/Espo/Tools/FieldManager/FieldTypes/ForeignTypeTest.phpnew file mode 100644index 0000000000..5da77963ff--- /dev/null+++ b/tests/integration/Espo/Tools/FieldManager/FieldTypes/ForeignTypeTest.php@@ -0,0 +1,72 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program 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+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace integration\Espo\Tools\FieldManager\FieldTypes;++use Espo\Core\Binding\BindingContainerBuilder;+use Espo\Core\ORM\Type\FieldType;+use Espo\Modules\Crm\Entities\Opportunity;+use Espo\Tools\FieldManager\FieldManager;+use tests\integration\Core\BaseTestCase;++class ForeignTypeTest extends BaseTestCase+{+ public function testView(): void+ {+ $fieldManager = $this->getInjectableFactory()->createWithBinding(+ FieldManager::class,+ BindingContainerBuilder::create()+ ->build()+ );++ $fieldManager->create(Opportunity::ENTITY_TYPE, 'accountType', [+ 'type' => FieldType::FOREIGN,+ 'link' => 'account',+ 'field' => 'type',+ ]);++ $metadata = $this->getMetadata();++ $this->assertEquals(+ 'views/fields/foreign-enum',+ $metadata->get("entityDefs.Opportunity.fields.cAccountType.view")+ );++ //++ $fieldManager->update(Opportunity::ENTITY_TYPE, 'cAccountType', [+ 'label' => 'Test',+ ]);++ $this->assertEquals(+ 'views/fields/foreign-enum',+ $metadata->get("entityDefs.Opportunity.fields.cAccountType.view")+ );+ }+}