From 72254d523d0fc9570929e5076cca3e5fe44c1e34 Mon Sep 17 00:00:00 2001 From: Marko Cupic Date: Mon, 11 Dec 2023 20:52:41 +0100 Subject: [PATCH] Embed the album preview image with the link tag. --- .../templates/_new/component/_album.html.twig | 28 +++++++----- .../AbstractGalleryCreatorController.php | 2 - .../GalleryCreatorController.php | 2 - src/Model/GalleryCreatorAlbumsModel.php | 7 --- src/Util/MarkdownUtil.php | 43 +++++++++++-------- src/Util/PictureUtil.php | 23 +++++----- src/Util/SecurityUtil.php | 11 +++-- 7 files changed, 60 insertions(+), 56 deletions(-) diff --git a/contao/templates/_new/component/_album.html.twig b/contao/templates/_new/component/_album.html.twig index 90b5525..7049c75 100644 --- a/contao/templates/_new/component/_album.html.twig +++ b/contao/templates/_new/component/_album.html.twig @@ -4,30 +4,36 @@ {% trans_default_domain 'contao_default' %} {% block album_component %} + {% if album.href|default %} + {% + set album_href_attributes = attrs(album_href_attributes|default) + .set('href', album.href) + .set('title', album.meta.getTitle()) + .addClass('gc-album-list-link') + %} + {% endif %} {% block album_figure %} - {% if album.figure.build|default %}{% with {figure: album.figure.build} %}{{ block('figure_component') }}{% endwith %}{% endif %} + {% if album_href_attributes|default %} + + {% endif %} + {% if album.figure.build|default %}{% with {figure: album.figure.build} %}{{ block('figure_component') }}{% endwith %}{% endif %} + {% if album_href_attributes|default %} + + {% endif %} {% endblock %} {% block album_title %} - {% if album.href|default %} - {% - set album_href_attributes = attrs(album_href_attributes|default) - .set('href', album.href) - .set('title', album.meta.getTitle()) - .addClass('gc-album-list-link') - %} + {% if album_href_attributes|default %} {% endif %} - {% set album_title_tag = album_title_tag|default('h4') %} - <{{ album_title_tag }}{{ attrs(album_name_attributes|default).addClass('gc-album-list-name') }}>{% block album_name %}{{ album.name }}{% endblock %} - {% if album.href|default %} + {% if album_href_attributes|default %} {% endif %} {% endblock %} diff --git a/src/Controller/ContentElement/AbstractGalleryCreatorController.php b/src/Controller/ContentElement/AbstractGalleryCreatorController.php index ceeebdc..dd58be3 100644 --- a/src/Controller/ContentElement/AbstractGalleryCreatorController.php +++ b/src/Controller/ContentElement/AbstractGalleryCreatorController.php @@ -26,8 +26,6 @@ use Contao\CoreBundle\String\HtmlDecoder; use Contao\CoreBundle\Twig\FragmentTemplate; use Contao\Date; -use Contao\File; -use Contao\Files; use Contao\FilesModel; use Contao\PageModel; use Contao\StringUtil; diff --git a/src/Controller/ContentElement/GalleryCreatorController.php b/src/Controller/ContentElement/GalleryCreatorController.php index c98e372..7c84b54 100644 --- a/src/Controller/ContentElement/GalleryCreatorController.php +++ b/src/Controller/ContentElement/GalleryCreatorController.php @@ -240,8 +240,6 @@ function ($id) { // Add meta tags to the page header. $this->addMetaTagsToPage($this->pageModel, $this->activeAlbum); - - } // Trigger gcGenerateFrontendTemplateHook diff --git a/src/Model/GalleryCreatorAlbumsModel.php b/src/Model/GalleryCreatorAlbumsModel.php index 7ce217d..79eb9c1 100644 --- a/src/Model/GalleryCreatorAlbumsModel.php +++ b/src/Model/GalleryCreatorAlbumsModel.php @@ -29,13 +29,6 @@ class GalleryCreatorAlbumsModel extends Model */ protected static $strTable = 'tl_gallery_creator_albums'; - /** - * @param GalleryCreatorAlbumsModel $albumsModel - * - * @throws \Exception - * - * @return static|null - */ public static function getParentAlbum(self $albumsModel): self|null { return $albumsModel->getRelated('pid'); diff --git a/src/Util/MarkdownUtil.php b/src/Util/MarkdownUtil.php index 9aeca98..765e65b 100644 --- a/src/Util/MarkdownUtil.php +++ b/src/Util/MarkdownUtil.php @@ -16,9 +16,13 @@ use Contao\Config; use Contao\CoreBundle\Framework\ContaoFramework; +use Contao\CoreBundle\InsertTag\CommonMarkExtension; +use Contao\CoreBundle\InsertTag\InsertTagParser; use Contao\Input; +use League\CommonMark\ConverterInterface; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\Autolink\AutolinkExtension; +use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension; use League\CommonMark\Extension\Strikethrough\StrikethroughExtension; use League\CommonMark\Extension\Table\TableExtension; @@ -32,27 +36,40 @@ class MarkdownUtil public function __construct( private readonly ContaoFramework $framework, private readonly RequestStack $requestStack, + private readonly InsertTagParser $insertTagParser, ) { } - public function parse(string $strMarkdown): string + public function parse(string $markdown): string { $request = $this->requestStack->getCurrentRequest(); - /** @var Config $config */ $config = $this->framework->getAdapter(Config::class); - - /** @var Input $input */ $input = $this->framework->getAdapter(Input::class); - - $html = $this->createConverter($request)->convertToHtml($strMarkdown); + $html = $this->createConverter($request)->convert($markdown); return $input->stripTags($html, $config->get('allowedTags'), $config->get('allowedAttributes')); } - private function createConverter(Request $request): MarkdownConverter + /** + * Hint: This is protected on purpose, so you can override it for your app specific requirements. + * If you want to provide an extension with additional logic, consider providing your own special + * content element for that. + */ + protected function createConverter(Request $request): ConverterInterface { - $environment = Environment::createCommonMarkEnvironment(); + $environment = new Environment([ + 'external_link' => [ + 'internal_hosts' => $request->getHost(), + 'open_in_new_window' => true, + 'html_class' => 'external-link', + 'noopener' => 'external', + 'noreferrer' => 'external', + ], + ]); + + $environment->addExtension(new CommonMarkExtension($this->insertTagParser)); + $environment->addExtension(new CommonMarkCoreExtension()); // Support GitHub flavoured Markdown (using the individual extensions because we don't want the // DisallowedRawHtmlExtension which is included by default) @@ -64,16 +81,6 @@ private function createConverter(Request $request): MarkdownConverter // Automatically mark external links as such if we have a request $environment->addExtension(new ExternalLinkExtension()); - $environment->mergeConfig([ - 'external_link' => [ - 'internal_hosts' => $request->getHost(), - 'open_in_new_window' => true, - 'html_class' => 'external-link', - 'noopener' => 'external', - 'noreferrer' => 'external', - ], - ]); - return new MarkdownConverter($environment); } } diff --git a/src/Util/PictureUtil.php b/src/Util/PictureUtil.php index ab906ab..c6e6cc9 100644 --- a/src/Util/PictureUtil.php +++ b/src/Util/PictureUtil.php @@ -44,7 +44,6 @@ public function __construct( */ public function getPictureData(GalleryCreatorPicturesModel $pictureModel, ContentModel $contentElementModel): array|null { - $staticUrl = System::getContainer()->get('contao.assets.files_context')->getStaticUrl(); $request = $this->requestStack->getCurrentRequest(); @@ -95,10 +94,11 @@ public function getPictureData(GalleryCreatorPicturesModel $pictureModel, Conten ->createFigureBuilder() ->setSize($contentElementModel->gcSizeDetailView) ->setLightboxGroupIdentifier('lb'.$contentElementModel->id) - ->enableLightbox((bool)$contentElementModel->gcFullSize) + ->enableLightbox((bool) $contentElementModel->gcFullSize) ->setOverwriteMetadata(new Metadata($arrMeta)) ->fromUuid($filesModel->uuid) - ->setMetadata(new Metadata($arrMeta)); + ->setMetadata(new Metadata($arrMeta)) + ; if ($customHref) { $figure->setLinkHref($customHref); @@ -106,20 +106,20 @@ public function getPictureData(GalleryCreatorPicturesModel $pictureModel, Conten // Build the array return [ - 'row_owner' => $ownerModel ? $ownerModel->row() : [], - 'row_files' => $filesModel->row(), - 'row_picture' => $pictureModel->row(), - 'row_album' => $pictureModel->getRelated('pid')->row(), - 'local_media_src' => $localMediaSrc, + 'row_owner' => $ownerModel ? $ownerModel->row() : [], + 'row_files' => $filesModel->row(), + 'row_picture' => $pictureModel->row(), + 'row_album' => $pictureModel->getRelated('pid')->row(), + 'local_media_src' => $localMediaSrc, 'social_media_src' => $socialMediaSrc, - 'exif_data' => $this->galleryCreatorReadExifMetaData ? $this->getExif(new File($filesModel->path)) : [], - 'figure' => [ + 'exif_data' => $this->galleryCreatorReadExifMetaData ? $this->getExif(new File($filesModel->path)) : [], + 'figure' => [ 'build' => $figure->build(), 'uuid' => $filesModel->uuid, 'size' => $contentElementModel->gcSizeDetailView, 'enable_lightbox' => (bool) $contentElementModel->gcFullSize, 'meta_data' => new Metadata($arrMeta), - ], + ], ]; } @@ -149,5 +149,4 @@ public function pictureExists(GalleryCreatorPicturesModel $picturesModel): bool return true; } - } diff --git a/src/Util/SecurityUtil.php b/src/Util/SecurityUtil.php index b75eb9a..9a06a5b 100644 --- a/src/Util/SecurityUtil.php +++ b/src/Util/SecurityUtil.php @@ -14,6 +14,7 @@ namespace Markocupic\GalleryCreatorBundle\Util; +use Contao\CoreBundle\Framework\ContaoFramework; use Contao\CoreBundle\Routing\ScopeMatcher; use Contao\FrontendUser; use Contao\StringUtil; @@ -24,9 +25,10 @@ class SecurityUtil { public function __construct( - private readonly Security $security, - private readonly ScopeMatcher $scopeMatcher, + private readonly ContaoFramework $framework, private readonly RequestStack $requestStack, + private readonly ScopeMatcher $scopeMatcher, + private readonly Security $security, ) { } @@ -38,14 +40,15 @@ public function isAuthorized(GalleryCreatorAlbumsModel $albumsModel): bool { $user = $this->security->getUser(); $request = $this->requestStack->getCurrentRequest(); + $stringUtil = $this->framework->getAdapter(StringUtil::class); if (!$albumsModel->protected) { return true; } if ($request && $this->scopeMatcher->isFrontendRequest($request) && $user instanceof FrontendUser) { - $allowedGroups = StringUtil::deserialize($albumsModel->groups, true); - $userGroups = StringUtil::deserialize($user->groups, true); + $allowedGroups = $stringUtil->deserialize($albumsModel->groups, true); + $userGroups = $stringUtil->deserialize($user->groups, true); if (!empty(array_intersect($allowedGroups, $userGroups))) { return true;