Skip to content

EspoCRM – Open Source CRM Application

Metadata scope accountLink

Yuriic4a213cc1cd5
diff --git a/application/Espo/Tools/Object/MetadataProvider.php b/application/Espo/Tools/Object/MetadataProvider.php
new file mode 100644
index 0000000000..7daeb9db25
--- /dev/null
+++ b/application/Espo/Tools/Object/MetadataProvider.php
@@ -0,0 +1,77 @@
+<?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\Object;
+
+use Espo\Core\Utils\Metadata;
+use Espo\Modules\Crm\Entities\Account;
+use Espo\ORM\Defs;
+use Espo\ORM\Type\RelationType;
+
+/**
+ * @since 10.0.0
+ */
+class MetadataProvider
+{
+ private const string LINK_ACCOUNT = 'account';
+
+ public function __construct(
+ private Metadata $metadata,
+ private Defs $defs,
+ ) {}
+
+ public function getAccountLink(string $entityType): ?string
+ {
+ $link = $this->metadata->get("scopes.$entityType.accountLink");
+
+ if ($link) {
+ return $link;
+ }
+
+ $link = self::LINK_ACCOUNT;
+
+ $relationDefs = $this->defs
+ ->getEntity($entityType)
+ ->tryGetRelation($link);
+
+ if (!$relationDefs) {
+ return null;
+ }
+
+ if (!in_array($relationDefs->getType(), [RelationType::BELONGS_TO, RelationType::HAS_ONE])) {
+ return null;
+ }
+
+ if ($relationDefs->tryGetForeignEntityType() !== Account::ENTITY_TYPE) {
+ return null;
+ }
+
+ return $link;
+ }
+}
diff --git a/application/Espo/Tools/Stream/Service.php b/application/Espo/Tools/Stream/Service.php
index 4d3f32de07..633c4c6bfb 100644
--- a/application/Espo/Tools/Stream/Service.php
+++ b/application/Espo/Tools/Stream/Service.php
@@ -75,6 +75,7 @@ use Espo\Core\Select\SelectBuilderFactory;
use Espo\Core\Select\SearchParams;
use Espo\Core\Utils\Acl\UserAclManagerProvider;
+use Espo\Tools\Object\MetadataProvider as ObjectMetadataProvider;
use stdClass;
class Service
@@ -108,6 +109,7 @@ class Service
private RecordServiceContainer $recordServiceContainer,
private SystemUser $systemUser,
private AssignmentHelper $assignmentHelper,
+ private ObjectMetadataProvider $objectMetadataProvider,
) {}
private function getStatusField(string $entityType): ?string
@@ -1131,19 +1133,15 @@ class Service
private function setSuperParent(Entity $entity, Note $note, bool $processTeamsUsers): void
{
- $accountId = $entity->get('accountId');
+ $link = $this->objectMetadataProvider->getAccountLink($entity->getEntityType());
- if (!$accountId) {
+ if (!$link) {
return;
}
- $entityDefs = $this->entityManager
- ->getDefs()
- ->getEntity($entity->getEntityType());
-
- $foreignEntityType = $entityDefs->tryGetRelation('account')?->tryGetForeignEntityType();
+ $accountId = $entity->get($link . 'Id');
- if ($foreignEntityType !== Account::ENTITY_TYPE) {
+ if (!$accountId) {
return;
}
diff --git a/schema/metadata/scopes.json b/schema/metadata/scopes.json
index 2f82a599dc..28a6552aaa 100644
--- a/schema/metadata/scopes.json
+++ b/schema/metadata/scopes.json
@@ -331,6 +331,10 @@
"type": "boolean",
"description": "Force to be available in Last Viewed."
},
+ "accountLink": {
+ "type": "string",
+ "description": "A link determining the Account the record belongs to. As of v10.0."
+ },
"entityManager": {
"description": "Entity manager items availability.",
"type": "object",