Skip to content

EspoCRM – Open Source CRM Application

Refactor lead capture form service

Yurii1a3d2f1e0a84
diff --git a/application/Espo/EntryPoints/LeadCaptureForm.php b/application/Espo/EntryPoints/LeadCaptureForm.php
index 301f39fe88..13db872498 100644
--- a/application/Espo/EntryPoints/LeadCaptureForm.php
+++ b/application/Espo/EntryPoints/LeadCaptureForm.php
@@ -63,7 +63,7 @@ class LeadCaptureForm implements EntryPoint
throw new BadRequest("No ID.");
}
- [$leadCapture, $data, $captchaScript] = $this->service->getData($id);
+ [$leadCapture, $data, $captchaScript, $direction] = $this->service->getData($id);
$params = new ActionRenderer\Params(
controller: 'controllers/lead-capture-form',
@@ -75,7 +75,7 @@ class LeadCaptureForm implements EntryPoint
->withFrameAncestors($leadCapture->getFormFrameAncestors())
->withPageTitle($leadCapture->getFormTitle())
->withTheme($leadCapture->getFormTheme())
- ->withDirection($this->service->getDirection($leadCapture));
+ ->withDirection($direction);
if ($captchaScript) {
$params = $params->withScripts([new Script(source: $captchaScript)]);
diff --git a/application/Espo/Tools/LeadCapture/FormService.php b/application/Espo/Tools/LeadCapture/FormService.php
index a9678145bf..9fe1176cf0 100644
--- a/application/Espo/Tools/LeadCapture/FormService.php
+++ b/application/Espo/Tools/LeadCapture/FormService.php
@@ -51,7 +51,6 @@ use RuntimeException;
class FormService
{
private const CACHE_KEY_PREFIX = 'leadCaptureForm';
- private const RTL_LANGUAGE_CODE_LIST = ['ar', 'fa', 'he', 'ur'];
public function __construct(
private EntityManager $entityManager,
@@ -66,10 +65,11 @@ class FormService
private ThemeManager $themeManager,
private Config\SystemConfig $systemConfig,
private ThemeMetadataProvider $themeMetadataProvider,
+ private ThemeDirectionDetector $themeDirectionDetector,
) {}
/**
- * @return array{LeadCapture, array<string, mixed>, ?string}
+ * @return array{LeadCapture, array<string, mixed>, ?string, Direction}
* @throws NotFound
*/
public function getData(string $id): array
@@ -82,15 +82,9 @@ class FormService
$data['captchaKey'] = $captchaKey;
- return [$leadCapture, $data, $captchaScript];
- }
-
- public function getDirection(LeadCapture $leadCapture): Direction
- {
- $language = $leadCapture->getFormLanguage() ?? $this->config->get('language') ?? 'en_US';
- $languageCode = strtolower(substr($language, 0, 2));
+ $direction = $this->themeDirectionDetector->detect($leadCapture);
- return in_array($languageCode, self::RTL_LANGUAGE_CODE_LIST, true) ? Direction::Rtl : Direction::Ltr;
+ return [$leadCapture, $data, $captchaScript, $direction];
}
/**
diff --git a/application/Espo/Tools/LeadCapture/ThemeDirectionDetector.php b/application/Espo/Tools/LeadCapture/ThemeDirectionDetector.php
new file mode 100644
index 0000000000..54dc605cd3
--- /dev/null
+++ b/application/Espo/Tools/LeadCapture/ThemeDirectionDetector.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\LeadCapture;
+
+use Espo\Core\Binding\Attributes\Qualify;
+use Espo\Core\Utils\Language;
+use Espo\Core\Utils\Theme\Direction;
+use Espo\Entities\LeadCapture;
+
+class ThemeDirectionDetector
+{
+ private const array RTL_LANGUAGE_CODE_LIST = [
+ 'ar',
+ 'fa',
+ 'he',
+ 'ur',
+ ];
+
+ public function __construct(
+ #[Qualify(Language::QUALIFIER_DEFAULT)]
+ private Language $language,
+ ) {}
+
+ public function detect(LeadCapture $leadCapture): Direction
+ {
+ $language = $leadCapture->getFormLanguage() ?? $this->language->getLanguage();
+
+ $languageCode = strtolower(substr($language, 0, 2));
+
+ return in_array($languageCode, self::RTL_LANGUAGE_CODE_LIST, true) ?
+ Direction::Rtl :
+ Direction::Ltr;
+ }
+}
diff --git a/tests/unit/Espo/Tools/LeadCapture/FormServiceTest.php b/tests/unit/Espo/Tools/LeadCapture/FormServiceTest.php
deleted file mode 100644
index 8a67aee7ed..0000000000
--- a/tests/unit/Espo/Tools/LeadCapture/FormServiceTest.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-
-namespace tests\unit\Espo\Tools\LeadCapture;
-
-use Espo\Core\Utils\Theme\Direction;
-use Espo\Entities\LeadCapture;
-use Espo\Tools\LeadCapture\FormService;
-use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\TestCase;
-use ReflectionClass;
-
-class FormServiceTest extends TestCase
-{
- /**
- * @return iterable<string, array{string, Direction}>
- */
- public static function directionProvider(): iterable
- {
- yield 'Arabic' => ['ar_AR', Direction::Rtl];
- yield 'Persian' => ['fa_IR', Direction::Rtl];
- yield 'Hebrew' => ['he_IL', Direction::Rtl];
- yield 'Urdu' => ['ur_IN', Direction::Rtl];
- yield 'English' => ['en_US', Direction::Ltr];
- yield 'Turkish' => ['tr_TR', Direction::Ltr];
- }
-
- #[DataProvider('directionProvider')]
- public function testDirectionFromFormLanguage(string $language, Direction $expected): void
- {
- $leadCapture = $this->createMock(LeadCapture::class);
- $leadCapture
- ->method('getFormLanguage')
- ->willReturn($language);
-
- $reflection = new ReflectionClass(FormService::class);
- /** @var FormService $service */
- $service = $reflection->newInstanceWithoutConstructor();
-
- $this->assertSame($expected, $service->getDirection($leadCapture));
- }
-}
diff --git a/tests/unit/Espo/Tools/LeadCapture/ThemeDirectionDetectorTest.php b/tests/unit/Espo/Tools/LeadCapture/ThemeDirectionDetectorTest.php
new file mode 100644
index 0000000000..789416efd9
--- /dev/null
+++ b/tests/unit/Espo/Tools/LeadCapture/ThemeDirectionDetectorTest.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace tests\unit\Espo\Tools\LeadCapture;
+
+use Espo\Core\Utils\Language;
+use Espo\Core\Utils\Theme\Direction;
+use Espo\Entities\LeadCapture;
+use Espo\Tools\LeadCapture\ThemeDirectionDetector;
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\TestCase;
+
+class ThemeDirectionDetectorTest extends TestCase
+{
+ /**
+ * @return iterable<string, array{string, Direction}>
+ */
+ public static function directionProvider(): iterable
+ {
+ yield ['ar_AR', Direction::Rtl];
+ yield ['fa_IR', Direction::Rtl];
+ yield ['he_IL', Direction::Rtl];
+ yield ['ur_IN', Direction::Rtl];
+ yield ['en_US', Direction::Ltr];
+ yield ['tr_TR', Direction::Ltr];
+ }
+
+ #[DataProvider('directionProvider')]
+ public function testDirectionFromFormLanguage(string $language, Direction $expected): void
+ {
+ $leadCapture = $this->createMock(LeadCapture::class);
+ $leadCapture
+ ->method('getFormLanguage')
+ ->willReturn($language);
+
+ $language = $this->createMock(Language::class);
+ $language
+ ->method('getLanguage')
+ ->willReturn('en_US');
+
+ $detector = new ThemeDirectionDetector(
+ language: $language,
+ );
+
+ $this->assertEquals($expected, $detector->detect($leadCapture));
+ }
+}