src/Flexy/FrontBundle/EventSubscriber/HookSubscriber.php line 33
<?php
// src/EventSubscriber/HookSubscriber.php
namespace App\Flexy\FrontBundle\EventSubscriber;
use App\Entity\Settings;
use App\Flexy\FrontBundle\Entity\Menu;
use App\Flexy\FrontBundle\Entity\Page;
use App\Flexy\FrontBundle\Service\FlexyFrontProvider;
use App\Flexy\FrontBundle\Service\HookService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class HookSubscriber implements EventSubscriberInterface
{
private HookService $hookService;
private ManagerRegistry $doctrine;
public function __construct(HookService $hookService,ManagerRegistry $doctrine,private readonly FlexyFrontProvider $flexyFrontProvider)
{
$this->hookService = $hookService;
$this->doctrine = $doctrine;
}
public static function getSubscribedEvents()
{
return [
'kernel.request' => 'onKernelRequest', // Adjust this to suit your needs
];
}
public function onKernelRequest(RequestEvent $requestEvent)
{
if (!$requestEvent->isMainRequest()) {
return; // Ensure this only runs for the main request
}
// Example: Registering a hook to render a Twig template
// $this->hookService->renderTwig('header_left', '@Flexy/FrontBundle/Themes/Default/templates/web-themes/flexy-shop/partials/header/_left.html.twig', [], 10);
// Example: Registering a hook to render a Twig template
$settings = $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
if($settings->getAssetFolderName() != "Default" ){
return;
}
$this->hookService->register('home_before_main_content', function() {
return `
<!-- About Us -->
<div class="container my-5">
<h2 class="text-center mb-4">About Us</h2>
<div class="row">
<div class="col-md-6">
<img src="https://placehold.co/500x300" class="img-fluid" alt="About Us">
</div>
<div class="col-md-6">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur.</p>
</div>
</div>
</div>
`;
}, 0);
$this->hookService->register('footer', function() {
return ' <div class="container text-center p-5">
<p>© 2024 Flexy Platform. All rights reserved.</p>
</div>';
},0);
$currentWebsiteTheme = $settings->getWebsiteTheme()->getCode();
$this->hookService->register('header_left', function(){
$settings = $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
$title = $settings->getLogo() ? '<a href="/" class="logo"><img src="/uploads/default/'.$settings->getLogo().'" /></a>' : '<h2>Your logo</h2>';
return '<div class="pt-2">
'.$title.'
</div>';
}, 0);
$this->hookService->register('header_middle', function(){
$menu = $this->doctrine->getManager()->getRepository(Menu::class)->findOneBy(["isMainMenu"=>true]);
$menuHTML = $this->flexyFrontProvider->getRendredMenu($menu);
return $menuHTML;
}, 0);
$this->hookService->renderTwig('header_right', '@Flexy/FrontBundle/Themes/Default/templates/web-themes/'.$currentWebsiteTheme.'/partials/header/_right.html.twig', [], 0);
// Widgets Hooks Start
$widgets = $this->doctrine->getManager()->getRepository(Page::class)->findBy(["type"=>"widget"]);
foreach($widgets as $singleWidget){
if($singleWidget->getFrontHook()){
$this->hookService->renderRoute(
$singleWidget->getFrontHook(),
'widget_single_page',
["slug"=>$singleWidget->getSlug()],
(int)$singleWidget->getFrontHookPriority(),
$singleWidget->getSlug()
);
}
}
}
}