From c69bf399c9fc6d87ab6114194a0a0ec16bf33466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Sun, 25 Feb 2024 19:10:34 +0100 Subject: [PATCH] Fix #165 forward compatibility for fragment templates --- src/Element/CustomElement.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Element/CustomElement.php b/src/Element/CustomElement.php index a01c815..dec13a6 100644 --- a/src/Element/CustomElement.php +++ b/src/Element/CustomElement.php @@ -180,6 +180,8 @@ public function compile() $this->Template->getColumnClassName = function() use($self) { return call_user_func_array(array($self, 'getColumnClassName'), func_get_args()); }; + + $this->addFragmentControllerDefaults(); } /** @@ -283,4 +285,34 @@ public function getColumnClassName($index) return implode(' ', $classes); } + + private function addFragmentControllerDefaults() + { + $this->Template->template ??= $this->Template->getName(); + $this->Template->as_editor_view ??= System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create('')); + $this->Template->data ??= $this->objModel ? $this->objModel->row() : $this->arrData; + $this->Template->nested_fragments ??= []; + $this->Template->section ??= $this->strColumn; + $this->Template->properties ??= []; + $this->Template->element_html_id ??= $this->Template->cssID[0] ?? null; + $this->Template->element_css_classes ??= $this->Template->cssID[1] ?? ''; + + // Legacy templates access the text using `$this->headline`, twig templates use `headline.text` + $this->Template->headline = new class($this->Template->headline, $this->Template->hl) implements \Stringable + { + public ?string $text; + public ?string $tag_name; + + public function __construct(?string $text, ?string $tag_name) + { + $this->text = $text; + $this->tag_name = $tag_name; + } + + public function __toString(): string + { + return $this->text ?? ''; + } + }; + } }