Browse code

Add new content element for display text

Benjamin Roth authored on11/07/2025 16:05:11
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,38 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+namespace vonRotenberg\CoretoolsBundle\Controller\ContentElement;
6
+
7
+use Contao\ContentModel;
8
+use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
9
+use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
10
+use Contao\CoreBundle\Twig\FragmentTemplate;
11
+use Contao\StringUtil;
12
+use Contao\System;
13
+use Symfony\Component\HttpFoundation\Request;
14
+use Symfony\Component\HttpFoundation\Response;
15
+
16
+#[AsContentElement(category: 'texts')]
17
+class DisplayTextController extends AbstractContentElementController
18
+{
19
+    protected function getResponse(FragmentTemplate $template, ContentModel $model, Request $request): Response
20
+    {
21
+        if (System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest($request))
22
+        {
23
+            $GLOBALS['TL_CSS'][] = 'bundles/vonrotenbergcoretools/css/display-text.min.css|static';
24
+        }
25
+
26
+        $strText = $model->vr_dt_plainText ?: '';
27
+
28
+        if (!empty($strText) && !preg_match("/^h[1-3]$/",$model->vr_dt_size))
29
+        {
30
+            $strText = '<div' . (!empty($model->vr_dt_size) ? ' class="text-' . strtolower(StringUtil::prepareSlug($model->vr_dt_size)) . '"' : '') . '>' . $strText . '</div>';
31
+        } else {
32
+            $strText = '<' . strtolower($model->vr_dt_size) . '>' . $strText . '</' . strtolower($model->vr_dt_size) . '>';
33
+        }
34
+
35
+        $template->set('text', $strText);
36
+        return $template->getResponse();
37
+    }
38
+}