-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCategoryPageSubscriber.php
executable file
·101 lines (85 loc) · 4.66 KB
/
CategoryPageSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Omikron\FactFinder\Shopware6\Subscriber;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Omikron\FactFinder\Shopware6\Config\ExtensionConfig;
use Omikron\FactFinder\Shopware6\OmikronFactFinder;
use Omikron\FactFinder\Shopware6\Utilites\Ssr\Field\CategoryPath;
use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function Omikron\FactFinder\Shopware6\Internal\Utils\safeGetByName;
class CategoryPageSubscriber implements EventSubscriberInterface
{
private AbstractCategoryRoute $cmsPageRoute;
private Communication $config;
private ExtensionConfig $extensionConfig;
private string $fieldName;
private array $addParams;
public function __construct(
AbstractCategoryRoute $cmsPageRoute,
Communication $config,
ExtensionConfig $extensionConfig,
string $categoryPathFieldName,
array $categoryPageAddParams = [],
) {
$this->cmsPageRoute = $cmsPageRoute;
$this->config = $config;
$this->extensionConfig = $extensionConfig;
$this->fieldName = $categoryPathFieldName;
$this->addParams = $categoryPageAddParams;
}
public static function getSubscribedEvents()
{
return [NavigationPageLoadedEvent::class => 'onPageLoaded'];
}
public function onPageLoaded(NavigationPageLoadedEvent $event): void
{
$navigationId = $event->getRequest()->get('navigationId', $event->getSalesChannelContext()->getSalesChannel()->getNavigationCategoryId());
$category = $this->cmsPageRoute->load($navigationId, $event->getRequest(), $event->getSalesChannelContext())->getCategory();
$route = $event->getRequest()->get('_route');
$disableImmediate = safeGetByName($category->getCustomFields(), OmikronFactFinder::DISABLE_SEARCH_IMMEDIATE_CUSTOM_FIELD_NAME);
$isHome = $route === 'frontend.home.page';
$isCategory = !$isHome && !$disableImmediate;
$searchImmediate = $this->config->isSsrActive() === false && $isCategory;
$baseAddParams = array_filter(explode(',', (string) safeGetByName($event->getPage()->getExtension('factfinder')->get('communication'), 'add-params')));
/**
* $this->addParams + $baseAddParams will not override entries from $baseAddParams as $this->addParams is associative array and $baseAddParams is not
* $this->addParams is associative array because this is how parameters collection is passed as an argument constructor in Symfony.
*/
$mergedAddParams = array_reduce($this->addParams + $baseAddParams, function (array $acc, string $expr): array {
list($key, $value) = explode('=', $expr);
return $acc + [$key => $value];
}, []);
$categoryPath = (new CategoryPath($this->fieldName))->getValue($category);
$communication = [
'add-params' => implode(',', array_map(fn (string $key, string $value): string => sprintf('%s=%s', $key, $value), array_keys($mergedAddParams), array_values($mergedAddParams))),
] + ($isCategory ? ['category-page' => $categoryPath] : []);
if ($route === 'frontend.navigation.page') {
$event->getRequest()->attributes->set('categoryPath', $categoryPath);
}
$factfinderConfig = $event->getPage()->getExtension('factfinder')->getVars();
$communicationConfig = [];
if (isset($factfinderConfig['communication'])) {
$communicationConfig = array_merge($factfinderConfig['communication'], $communication);
}
$event->getPage()->getExtension('factfinder')->assign(
[
'communication' => $communication,
'trackingSettings' => $this->extensionConfig->getTrackingSettings(),
'redirectMapping' => (string) $this->extensionConfig->getRedirectMapping(),
'searchImmediate' => $searchImmediate ? 'true' : 'false',
'categoryPathFieldName' => "{$this->fieldName}ROOT",
'communicationAttributes' => !empty($communicationConfig) ? $this->getCommunicationAttributes($communicationConfig) : $communication,
]
);
}
private function getCommunicationAttributes(array $communicationConfig): array
{
return array_map(
fn (string $key, string $value) => sprintf('%s="%s" ', $key, htmlspecialchars($value)),
array_keys($communicationConfig),
array_values($communicationConfig)
);
}
}