src/Controller/ContentElement/VideoHero.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ContentElement;
  3. use Contao\BackendTemplate;
  4. use Contao\ContentModel;
  5. use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
  6. use Contao\CoreBundle\Routing\ScopeMatcher;
  7. use Contao\CoreBundle\ServiceAnnotation\ContentElement;
  8. use Contao\File;
  9. use Contao\FilesModel;
  10. use Contao\Model;
  11. use Contao\StringUtil;
  12. use Contao\Template;
  13. use Hofff\Contao\Consent\Bridge\ConsentId\ConsentIdParser;
  14. use Hofff\Contao\Consent\Bridge\ConsentToolManager;
  15. use Hofff\Contao\Consent\Bridge\Render\RenderInformation;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. /**
  19.  * @ContentElement("video_hero", category="media")
  20.  */
  21. class VideoHero extends AbstractContentElementController
  22. {
  23.     private ScopeMatcher       $scopeMatcher;
  24.     private ConsentToolManager $consentToolManager;
  25.     private ConsentIdParser    $consentIdParser;
  26.     public function __construct(
  27.         ScopeMatcher $scopeMatcher,
  28.         ConsentToolManager $consentToolManager,
  29.         ConsentIdParser $consentIdParser
  30.     ) {
  31.         $this->scopeMatcher       $scopeMatcher;
  32.         $this->consentToolManager $consentToolManager;
  33.         $this->consentIdParser    $consentIdParser;
  34.     }
  35.     protected function getResponse(Template $templateContentModel $modelRequest $request): ?Response
  36.     {
  37.         if ($this->scopeMatcher->isBackendRequest($request)) {
  38.             $template = new BackendTemplate('be_wildcard');
  39.             $template->wildcard '### VIDEO HERO ###';
  40.             return new Response($template->parse());
  41.         }
  42.         // Add the video file to the template
  43.         $template->file $this->videoFile($model$request->getLocale());
  44.         // Add the youtube player to the template
  45.         $template->player $this->playerTemplate($template$model);
  46.         try {
  47.             return $template->getResponse();
  48.         } catch (\Exception $objError) {
  49.             return null;
  50.         }
  51.     }
  52.     private function videoFile(ContentModel $modelstring $locale): ?File
  53.     {
  54.         if (!$model->playerSRC) {
  55.             return null;
  56.         }
  57.         $source StringUtil::deserialize($model->playerSRC);
  58.         if (empty($source) || !\is_array($source)) {
  59.             return null;
  60.         }
  61.         $files FilesModel::findMultipleByUuidsAndExtensions($source, ['mp4''m4v''mov''wmv''webm']);
  62.         if (null === $files) {
  63.             return null;
  64.         }
  65.         $videoModel $files->current();
  66.         $videoFile = new File($videoModel->path);
  67.         $videoFile->title StringUtil::specialchars($this->getMetaTitle($videoModel$locale));
  68.         return $videoFile;
  69.     }
  70.     private function playerTemplate(Template $templateContentModel $model): string
  71.     {
  72.         $playerTemplate = clone $template;
  73.         try {
  74.             $player $playerTemplate->parse();
  75.         } catch (\Exception $objError) {
  76.             return '';
  77.         }
  78.         $consentTool $this->consentToolManager->activeConsentTool();
  79.         if (null === $consentTool || !$model->hofff_consent_bridge_tag) {
  80.             return $player;
  81.         }
  82.         $consentId $this->consentIdParser->parse($model->hofff_consent_bridge_tag);
  83.         if (!$consentTool->requiresConsent($consentId)) {
  84.             return $player;
  85.         }
  86.         $renderInformation RenderInformation::autoRenderWithPlaceholder('hofff_consent_placeholder_youtube');
  87.         $player $consentTool->renderContent(
  88.             $playerTemplate->parse(),
  89.             $consentId,
  90.             $model,
  91.             $renderInformation->placeholderTemplate()
  92.         );
  93.         return $player;
  94.     }
  95.     private function getMetaTitle(Model $videoModelstring $locale): string
  96.     {
  97.         $arrMeta StringUtil::deserialize($videoModel->meta);
  98.         if (\is_array($arrMeta) && isset($arrMeta[$locale])) {
  99.             $title $arrMeta[$locale]['title'];
  100.         } else {
  101.             $title $videoModel->name;
  102.         }
  103.         return $title;
  104.     }
  105. }