Skip to content

Commit 790e941

Browse files
authored
added support for entity with single domain friendly URL (#3809)
2 parents d7dab94 + 2bd5f33 commit 790e941

File tree

14 files changed

+202
-64
lines changed

14 files changed

+202
-64
lines changed

src/Component/Domain/Domain.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,31 @@ public function findFirstB2bDomain(): ?DomainConfig
236236
}
237237

238238
/**
239+
* @param int[] $limitDomainsByIds
239240
* @return int[]
240241
*/
241-
public function getAdminEnabledDomainIds(): array
242+
public function getAdminEnabledDomainIds(array $limitDomainsByIds = []): array
242243
{
243244
$selectedDomainIds = $this->administratorFacade->getCurrentlyLoggedAdministrator()->getDisplayOnlyDomainIds();
244245

245-
return count($selectedDomainIds) > 0 ? $selectedDomainIds : $this->getAllIds();
246+
$domainIds = count($selectedDomainIds) > 0 ? $selectedDomainIds : $this->getAllIds();
247+
248+
if (count($limitDomainsByIds) > 0) {
249+
return array_intersect($domainIds, $limitDomainsByIds);
250+
}
251+
252+
return $domainIds;
246253
}
247254

248255
/**
256+
* @param int[] $limitDomainsByIds
249257
* @return \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig[]
250258
*/
251-
public function getAdminEnabledDomains(): array
259+
public function getAdminEnabledDomains(array $limitDomainsByIds = []): array
252260
{
253261
$domains = [];
254262

255-
foreach ($this->getAdminEnabledDomainIds() as $selectedDomainId) {
263+
foreach ($this->getAdminEnabledDomainIds($limitDomainsByIds) as $selectedDomainId) {
256264
$domains[$selectedDomainId] = $this->getDomainConfigById($selectedDomainId);
257265
}
258266

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\Exception;
6+
7+
use Exception;
8+
9+
class FriendlyUrlIsNotMultidomainException extends Exception
10+
{
11+
/**
12+
* @param string $routeName
13+
*/
14+
public function __construct(string $routeName)
15+
{
16+
parent::__construct('Route "' . $routeName . '" does not support creating URL for multiple domains.');
17+
}
18+
}

src/Component/Router/FriendlyUrl/FriendlyUrlFactory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66

77
use Shopsys\FrameworkBundle\Component\Domain\Domain;
88
use Shopsys\FrameworkBundle\Component\EntityExtension\EntityNameResolver;
9+
use Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory;
10+
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\Exception\FriendlyUrlIsNotMultidomainException;
911
use Shopsys\FrameworkBundle\Component\String\TransformStringHelper;
12+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1013

1114
class FriendlyUrlFactory
1215
{
1316
/**
1417
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
1518
* @param \Shopsys\FrameworkBundle\Component\EntityExtension\EntityNameResolver $entityNameResolver
1619
* @param \Shopsys\FrameworkBundle\Component\String\TransformStringHelper $transformStringHelper
20+
* @param \Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory $domainRouterFactory
1721
*/
1822
public function __construct(
1923
protected readonly Domain $domain,
2024
protected readonly EntityNameResolver $entityNameResolver,
2125
protected readonly TransformStringHelper $transformStringHelper,
26+
protected readonly DomainRouterFactory $domainRouterFactory,
2227
) {
2328
}
2429

@@ -78,6 +83,10 @@ public function createForAllDomains(
7883
): array {
7984
$friendlyUrls = [];
8085

86+
if ($this->isRouteMultidomain($routeName) === false) {
87+
throw new FriendlyUrlIsNotMultidomainException($routeName);
88+
}
89+
8190
foreach ($this->domain->getAll() as $domainConfig) {
8291
if (array_key_exists($domainConfig->getLocale(), $namesByLocale)) {
8392
$friendlyUrl = $this->createIfValid(
@@ -95,4 +104,21 @@ public function createForAllDomains(
95104

96105
return $friendlyUrls;
97106
}
107+
108+
/**
109+
* @param string $routeName
110+
* @return bool
111+
*/
112+
protected function isRouteMultidomain(string $routeName): bool
113+
{
114+
$friendlyUrlRouter = $this->domainRouterFactory->getFriendlyUrlRouter($this->domain->getDomainConfigById(Domain::FIRST_DOMAIN_ID));
115+
$routeCollection = $friendlyUrlRouter->getRouteCollection();
116+
$route = $routeCollection->get($routeName);
117+
118+
if ($route === null) {
119+
throw new RouteNotFoundException('Route "' . $routeName . '" not found.');
120+
}
121+
122+
return $route->getOption('multidomain') ?? true;
123+
}
98124
}

src/Component/Router/FriendlyUrl/FriendlyUrlGeneratorFacade.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
/**
2929
* @param \Symfony\Component\Console\Output\OutputInterface $output
3030
*/
31-
public function generateUrlsForSupportedEntities(OutputInterface $output)
31+
public function generateUrlsForSupportedEntities(OutputInterface $output): void
3232
{
3333
foreach ($this->domain->getAll() as $domainConfig) {
3434
$output->writeln(' Start of generating friendly urls for domain ' . $domainConfig->getUrl());
@@ -48,12 +48,18 @@ public function generateUrlsForSupportedEntities(OutputInterface $output)
4848
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
4949
* @return int
5050
*/
51-
protected function generateUrlsByDomainConfig(OutputInterface $output, DomainConfig $domainConfig)
51+
protected function generateUrlsByDomainConfig(OutputInterface $output, DomainConfig $domainConfig): int
5252
{
5353
$totalCountOfCreatedUrls = 0;
5454
$friendlyUrlRouter = $this->domainRouterFactory->getFriendlyUrlRouter($domainConfig);
5555

5656
foreach ($friendlyUrlRouter->getRouteCollection() as $routeName => $route) {
57+
$isMultidomain = $route->getOption('multidomain') ?? true;
58+
59+
if ($isMultidomain === false) {
60+
continue;
61+
}
62+
5763
$countOfCreatedUrls = $this->generateUrlsByRoute($domainConfig, $routeName);
5864
$totalCountOfCreatedUrls += $countOfCreatedUrls;
5965

@@ -73,7 +79,7 @@ protected function generateUrlsByDomainConfig(OutputInterface $output, DomainCon
7379
* @param string $routeName
7480
* @return int
7581
*/
76-
protected function generateUrlsByRoute(DomainConfig $domainConfig, $routeName)
82+
protected function generateUrlsByRoute(DomainConfig $domainConfig, string $routeName): int
7783
{
7884
$countOfCreatedUrls = 0;
7985

src/Form/Admin/Article/ArticleFormType.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ public function __construct(
5151
*/
5252
public function buildForm(FormBuilderInterface $builder, array $options): void
5353
{
54+
/** @var \Shopsys\FrameworkBundle\Model\Article\Article|null $article */
55+
$article = $options['article'];
56+
5457
$seoMetaDescriptionAttributes = $this->getSeoMetaDescriptionAttributes($options);
5558

5659
$builderArticleData = $builder->create('articleData', GroupType::class, [
5760
'label' => t('Article data'),
5861
]);
5962

60-
if ($options['article'] === null) {
63+
if ($article === null) {
6164
$builderArticleData
6265
->add('domainId', DomainType::class, [
6366
'required' => true,
@@ -76,11 +79,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
7679
} else {
7780
$builderArticleData
7881
->add('id', DisplayOnlyType::class, [
79-
'data' => $options['article']->getId(),
82+
'data' => $article->getId(),
8083
'label' => t('ID'),
8184
])
8285
->add('domain', DisplayOnlyType::class, [
83-
'data' => $this->domain->getDomainConfigById($options['article']->getDomainId())->getName(),
86+
'data' => $this->domain->getDomainConfigById($article->getDomainId())->getName(),
8487
'label' => t('Domain'),
8588
]);
8689
}
@@ -176,12 +179,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
176179
'label' => t('Heading (H1)'),
177180
]);
178181

179-
if ($options['article'] !== null) {
182+
if ($article !== null) {
180183
$builderSeoData
181184
->add('urls', UrlListType::class, [
182185
'label' => t('URL addresses'),
183186
'route_name' => 'front_article_detail',
184-
'entity_id' => $options['article']->getId(),
187+
'entity_id' => $article->getId(),
188+
'limit_domains_by_ids' => [$article->getDomainId()],
185189
]);
186190
}
187191

@@ -190,7 +194,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
190194
->add($builderSeoData)
191195
->add('actionBar', ActionBarType::class, [
192196
'back_route' => 'admin_article_list',
193-
'entity' => $options['article'],
197+
'entity' => $article,
194198
]);
195199
}
196200

src/Form/Admin/Store/StoreFormType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private function createBasicInformationGroup(FormBuilderInterface $builder, ?Sto
9595
'route_name' => StoreFriendlyUrlProvider::ROUTE_NAME,
9696
'entity_id' => $store->getId(),
9797
'label' => t('URL settings'),
98+
'limit_domains_by_ids' => [$store->getDomainId()],
9899
]);
99100
}
100101

@@ -112,6 +113,7 @@ private function createBasicInformationGroup(FormBuilderInterface $builder, ?Sto
112113
->add('domainId', DomainType::class, [
113114
'required' => true,
114115
'label' => t('Display on'),
116+
'disabled' => $store !== null,
115117
])
116118
->add('externalId', TextType::class, [
117119
'required' => false,

src/Form/DomainType.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
*/
3030
public function buildView(FormView $view, FormInterface $form, array $options): void
3131
{
32-
$view->vars['domainConfigs'] = $this->getSortedDomainConfigsByAdminDomainTabs();
32+
$view->vars['domainConfigs'] = $this->getSortedDomainConfigsByAdminDomainTabs($options['limit_domains_by_ids']);
3333
$view->vars['displayUrl'] = $options['displayUrl'];
3434
}
3535

@@ -38,22 +38,26 @@ public function buildView(FormView $view, FormInterface $form, array $options):
3838
*/
3939
public function configureOptions(OptionsResolver $resolver): void
4040
{
41-
$resolver->setDefaults([
42-
'displayUrl' => false,
43-
]);
41+
$resolver
42+
->setDefaults([
43+
'displayUrl' => false,
44+
'limit_domains_by_ids' => [],
45+
])
46+
->setAllowedTypes('limit_domains_by_ids', 'array');
4447
}
4548

4649
/**
50+
* @param int[] $limitDomainsByIds
4751
* @return \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig[]
4852
*/
49-
private function getSortedDomainConfigsByAdminDomainTabs(): array
53+
private function getSortedDomainConfigsByAdminDomainTabs(array $limitDomainsByIds): array
5054
{
5155
$selectedDomainId = $this->adminDomainTabsFacade->getSelectedDomainId();
5256

5357
$list = [];
5458
$list[] = $this->adminDomainTabsFacade->getSelectedDomainConfig();
5559

56-
foreach ($this->domain->getAdminEnabledDomains() as $domainConfig) {
60+
foreach ($this->domain->getAdminEnabledDomains($limitDomainsByIds) as $domainConfig) {
5761
if ($domainConfig->getId() !== $selectedDomainId) {
5862
$list[] = $domainConfig;
5963
}

src/Form/FriendlyUrlType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Form\AbstractType;
99
use Symfony\Component\Form\Extension\Core\Type\TextType;
1010
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
1112
use Symfony\Component\Validator\Constraints;
1213

1314
class FriendlyUrlType extends AbstractType
@@ -23,7 +24,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2324
$builder->add(UrlListData::FIELD_DOMAIN, DomainType::class, [
2425
'displayUrl' => true,
2526
'required' => true,
27+
'limit_domains_by_ids' => $options['limit_domains_by_ids'],
2628
]);
29+
2730
$builder->add(UrlListData::FIELD_SLUG, TextType::class, [
2831
'required' => true,
2932
'constraints' => [
@@ -32,4 +35,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3235
],
3336
]);
3437
}
38+
39+
/**
40+
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
41+
*/
42+
public function configureOptions(OptionsResolver $resolver): void
43+
{
44+
$resolver
45+
->setDefaults([
46+
'limit_domains_by_ids' => [],
47+
])
48+
->setAllowedTypes('limit_domains_by_ids', 'array');
49+
}
3550
}

0 commit comments

Comments
 (0)