From 774e19c3a98b61543e1bbcc1c745cb2cacaa44ba Mon Sep 17 00:00:00 2001 From: "Javi H. Gil" Date: Wed, 15 Oct 2025 13:25:43 +0200 Subject: [PATCH] Add twig section render functions --- config/services/admin_services.yaml | 4 +- src/Twig/Extension/RenderExtension.php | 141 +++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 src/Twig/Extension/RenderExtension.php diff --git a/config/services/admin_services.yaml b/config/services/admin_services.yaml index f1ba5c6..7a7187d 100644 --- a/config/services/admin_services.yaml +++ b/config/services/admin_services.yaml @@ -9,6 +9,6 @@ services: Softspring\CmsSectionsPlugin\Admin\Menu\SectionMenuProvider: tags: [ 'sfs_cms.admin_menu.menu_provider' ] - Softspring\CmsSectionsPlugin\Twig\Extension\Admin\: - resource: '../../src/Twig/Extension/Admin/*' + Softspring\CmsSectionsPlugin\Twig\Extension\: + resource: '../../src/Twig/Extension/*' tags: [ 'twig.extension' ] diff --git a/src/Twig/Extension/RenderExtension.php b/src/Twig/Extension/RenderExtension.php new file mode 100644 index 0000000..5108382 --- /dev/null +++ b/src/Twig/Extension/RenderExtension.php @@ -0,0 +1,141 @@ + ['html']]), + new TwigFunction('sfs_cms_section_find_by_*', [$this, 'findBy'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section', [$this, 'renderEmbed'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_embed', [$this, 'renderEmbed'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_embed_by_*', [$this, 'renderEmbedBy'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_esi', [$this, 'renderEsi'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_esi_by_*', [$this, 'renderEsiBy'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_ajax', [$this, 'renderAjax'], ['is_safe' => ['html']]), + new TwigFunction('sfs_cms_section_ajax_by_*', [$this, 'renderAjaxBy'], ['is_safe' => ['html']]), + ]; + } + + /** + * @throws Exception + */ + public function find($criteria, array $orderBy = []): ?SectionInterface + { + if (is_string($criteria)) { + $criteria = ['id' => $criteria]; + } + + if (!is_array($criteria)) { + throw new Exception('Invalid criteria'); + } + + return $this->sectionManager->getRepository()->findOneBy($criteria, $orderBy); + } + + /** + * @throws Exception + */ + public function findBy($field, $value, array $criteria = [], array $orderBy = []): ?SectionInterface + { + $criteria[$field] = $value; + + return $this->find($criteria, $orderBy); + } + + /** + * @throws Exception + */ + public function renderEmbed(SectionInterface|string|null $sectionOrSectionId): string + { + if (!($section = $this->getSection($sectionOrSectionId))) { + return ""; + } + + return $this->twig->render('@module/section/render.html.twig', [ + 'section' => $section, + 'mode' => 'embed', + ]); + } + + /** + * @throws Exception + */ + public function renderEmbedBy($field, $value, array $criteria = [], array $orderBy = []): string + { + return $this->renderEmbed($this->findBy($field, $value, $criteria, $orderBy)); + } + + /** + * @throws Exception + */ + public function renderEsi(SectionInterface|string|null $sectionOrSectionId): string + { + if (!($section = $this->getSection($sectionOrSectionId))) { + return ""; + } + + return $this->twig->render('@module/section/render.html.twig', [ + 'section' => $section, + 'mode' => 'esi', + ]); + } + + /** + * @throws Exception + */ + public function renderEsiBy($field, $value, array $criteria = [], array $orderBy = []): string + { + return $this->renderEsi($this->findBy($field, $value, $criteria, $orderBy)); + } + + /** + * @throws Exception + */ + public function renderAjax(SectionInterface|string|null $sectionOrSectionId): string + { + if (!($section = $this->getSection($sectionOrSectionId))) { + return ""; + } + + return $this->twig->render('@module/section/render.html.twig', [ + 'section' => $section, + 'mode' => 'ajax', + ]); + } + + /** + * @throws Exception + */ + public function renderAjaxBy($field, $value, array $criteria = [], array $orderBy = []): string + { + return $this->renderAjax($this->findBy($field, $value, $criteria, $orderBy)); + } + + /** + * @throws Exception + */ + protected function getSection(SectionInterface|string|null $sectionOrSectionId): ?SectionInterface + { + if (is_string($sectionOrSectionId)) { + return $this->find($sectionOrSectionId); + } + + return $sectionOrSectionId; + } +}