Skip to content

EspoCRM – Open Source CRM Application

populate default pipeline

Yurii4cb8df2d0c41
diff --git a/application/Espo/Core/Record/Defaults/DefaultPopulator.php b/application/Espo/Core/Record/Defaults/DefaultPopulator.php
index fa383c836c..1763c6b3ee 100644
--- a/application/Espo/Core/Record/Defaults/DefaultPopulator.php
+++ b/application/Espo/Core/Record/Defaults/DefaultPopulator.php
@@ -47,6 +47,10 @@ use Espo\Modules\Crm\Entities\Contact;
use Espo\ORM\Defs\Params\FieldParam;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
+use Espo\Tools\Pipeline\Data\PipelineData;
+use Espo\Tools\Pipeline\MetadataProvider as PipelineMetadata;
+use Espo\Tools\Pipeline\UserPipelineDataProvider;
+use LogicException;
use RuntimeException;
/**
@@ -61,6 +65,8 @@ class DefaultPopulator implements Populator
private EntityManager $entityManager,
private CurrencyConfigDataProvider $currencyConfig,
private Metadata $metadata,
+ private PipelineMetadata $pipelineMetadata,
+ private UserPipelineDataProvider $userPipelineDataProvider,
) {}
public function populate(Entity $entity): void
@@ -69,6 +75,7 @@ class DefaultPopulator implements Populator
$this->processDefaultTeam($entity);
$this->processCurrency($entity);
$this->processPortal($entity);
+ $this->processPipeline($entity);
}
/**
@@ -275,4 +282,74 @@ class DefaultPopulator implements Populator
$entity->setValueObject($link, $linkMultiple);
}
}
+
+ private function processPipeline(Entity $entity): void
+ {
+ $entityType = $entity->getEntityType();
+
+ if (!$this->pipelineMetadata->isEnabled($entityType)) {
+ return;
+ }
+
+ $pipelineIdAttr = Field::PIPELINE . 'Id';
+ $stageIdAttr = Field::PIPELINE_STAGE . 'Id';
+
+ if (
+ $entity->get($pipelineIdAttr) &&
+ $entity->get($stageIdAttr)
+ ) {
+ return;
+ }
+
+ if (!$entity->get($pipelineIdAttr)) {
+ $pipeline = $this->userPipelineDataProvider->getForEntityType($entityType)[0] ?? null;
+
+ if (!$pipeline) {
+ return;
+ }
+
+ $entity->setMultiple([
+ $pipelineIdAttr => $pipeline->id,
+ Field::PIPELINE . 'Name' => $pipeline->name,
+ ]);
+ }
+
+ $id = $entity->get($pipelineIdAttr);
+
+ if (!$id) {
+ throw new LogicException();
+ }
+
+ if ($entity->get($stageIdAttr)) {
+ return;
+ }
+
+ $pipeline = $this->getPipeline($entityType, $id);
+
+ if (!$pipeline) {
+ return;
+ }
+
+ $stage = $pipeline->stages[0] ?? null;
+
+ if (!$stage) {
+ return;
+ }
+
+ $entity->setMultiple([
+ $stageIdAttr => $stage->id,
+ Field::PIPELINE_STAGE . 'Name' => $stage->name,
+ ]);
+ }
+
+ private function getPipeline(string $entityType, mixed $id): ?PipelineData
+ {
+ foreach ($this->userPipelineDataProvider->getForEntityType($entityType) as $it) {
+ if ($id === $it->id) {
+ return $it;
+ }
+ }
+
+ return null;
+ }
}
diff --git a/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php b/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php
new file mode 100644
index 0000000000..25dbdd6d8d
--- /dev/null
+++ b/application/Espo/Tools/Pipeline/UserPipelineDataProvider.php
@@ -0,0 +1,61 @@
+<?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\Pipeline;
+
+use Espo\Tools\Pipeline\Data\PipelineData;
+
+/**
+ * @since 9.4.0
+ */
+class UserPipelineDataProvider
+{
+ public function __construct(
+ private PipelineDataProvider $pipelineDataProvider,
+ private PipelineDataUserFilter $userFilter,
+ ) {}
+
+ /**
+ * @return PipelineData[]
+ */
+ public function getForEntityType(string $entityType): array
+ {
+ return $this->get()[$entityType] ?? [];
+ }
+
+ /**
+ * @return array<string, PipelineData[]>
+ */
+ private function get(): array
+ {
+ $pipelines = $this->pipelineDataProvider->get();
+
+ return $this->userFilter->filter($pipelines);
+ }
+}
diff --git a/tests/integration/Espo/Tools/Pipeline/PipelineTest.php b/tests/integration/Espo/Tools/Pipeline/PipelineTest.php
index 51653d2c05..5fed1613c2 100644
--- a/tests/integration/Espo/Tools/Pipeline/PipelineTest.php
+++ b/tests/integration/Espo/Tools/Pipeline/PipelineTest.php
@@ -180,6 +180,17 @@ class PipelineTest extends BaseTestCase
//
+ $opp = $oppService->create((object) [
+ Field::NAME => 'Opp 3',
+ Opportunity::FIELD_CLOSED_DATE => Date::fromString('2026-01-01')->toString(),
+ Opportunity::FIELD_AMOUNT => 1000,
+ ], CreateParams::create());
+
+ $this->assertEquals($pipeline1->getId(), $opp->get(Field::PIPELINE . 'Id'));
+ $this->assertEquals($pipeline1->getStages()[0]->getId(), $opp->get(Field::PIPELINE_STAGE . 'Id'));
+
+ //
+
$pipelinesParam = $this->getInjectableFactory()->create(Pipelines::class);
$pipelines = $pipelinesParam->get();