Browse code

Implement BE wildcard template for content element

Benjamin Roth authored on02/08/2023 11:08:19
Showing1 changed files
... ...
@@ -12,6 +12,7 @@ declare(strict_types=1);
12 12
 
13 13
 namespace vonRotenberg\MemberfilesBundle\Controller\Frontend\ContentElement;
14 14
 
15
+use Contao\BackendTemplate;
15 16
 use Contao\Config;
16 17
 use Contao\ContentModel;
17 18
 use Contao\Controller;
... ...
@@ -29,6 +30,7 @@ use Contao\Template;
29 30
 use Doctrine\DBAL\Connection;
30 31
 use Symfony\Component\HttpFoundation\Request;
31 32
 use Symfony\Component\HttpFoundation\Response;
33
+use Symfony\Contracts\Translation\TranslatorInterface;
32 34
 use vonRotenberg\MemberfilesBundle\Model\SecureDownloadsModel;
33 35
 
34 36
 /**
... ...
@@ -49,6 +51,14 @@ class SecureDownloadsController extends AbstractContentElementController
49 51
         $this->User = FrontendUser::getInstance();
50 52
     }
51 53
 
54
+    public function __invoke(Request $request, ContentModel $model, string $section, array $classes = null): Response
55
+    {
56
+        if (System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
57
+            return $this->getBackendWildcard($model);
58
+        }
59
+        return parent::__invoke($request, $model, $section, $classes); // TODO: Change the autogenerated stub
60
+    }
61
+
52 62
 
53 63
     protected function getResponse(Template $template, ContentModel $model, Request $request): Response
54 64
     {
... ...
@@ -268,4 +278,31 @@ class SecureDownloadsController extends AbstractContentElementController
268 278
 
269 279
         return $template->getResponse();
270 280
     }
281
+
282
+    protected function getBackendWildcard(ContentModel $element): Response
283
+    {
284
+        $href = $this->container->get('router')->generate(
285
+            'contao_backend',
286
+            ['do' => 'themes', 'table' => 'tl_module', 'act' => 'edit', 'id' => $element->id]
287
+        );
288
+
289
+        $name = $this->container->get('translator')->trans('FMD.'.$this->getType().'.0', [], 'contao_modules');
290
+
291
+        $template = new BackendTemplate('be_wildcard');
292
+        $template->wildcard = '### '.strtoupper($name).' ###';
293
+        $template->id = $element->id;
294
+        $template->link = $element->name;
295
+        $template->href = $href;
296
+
297
+        return new Response($template->parse());
298
+    }
299
+
300
+    public static function getSubscribedServices(): array
301
+    {
302
+        $services = parent::getSubscribedServices();
303
+
304
+        $services['translator'] = TranslatorInterface::class;
305
+
306
+        return $services;
307
+    }
271 308
 }