Skip to content

Commit

Permalink
Embed the album preview image with the link tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
markocupic committed Dec 11, 2023
1 parent b7cb735 commit 72254d5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 56 deletions.
28 changes: 17 additions & 11 deletions contao/templates/_new/component/_album.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<div{{ attrs(album_inner_attributes|default).addClass('gc-album-list-inner') }}>
{% block album_figure %}
<div{{ attrs(album_figure_attributes|default).addClass('gc-album-list-figure') }}>
{% if album.figure.build|default %}{% with {figure: album.figure.build} %}{{ block('figure_component') }}{% endwith %}{% endif %}
{% if album_href_attributes|default %}
<a{{ album_href_attributes }}>
{% endif %}
{% if album.figure.build|default %}{% with {figure: album.figure.build} %}{{ block('figure_component') }}{% endwith %}{% endif %}
{% if album_href_attributes|default %}
</a>
{% endif %}
</div>
{% endblock %}

<div{{ attrs(album_facts_attributes|default).addClass('gc-album-list-facts') }}>
{% 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 %}
<a{{ album_href_attributes }}>
{% 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 %}</{{ album_title_tag }}>
{% if album.href|default %}
{% if album_href_attributes|default %}
</a>
{% endif %}
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/Controller/ContentElement/GalleryCreatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ function ($id) {

// Add meta tags to the page header.
$this->addMetaTagsToPage($this->pageModel, $this->activeAlbum);


}

// Trigger gcGenerateFrontendTemplateHook
Expand Down
7 changes: 0 additions & 7 deletions src/Model/GalleryCreatorAlbumsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
43 changes: 25 additions & 18 deletions src/Util/MarkdownUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);
}
}
23 changes: 11 additions & 12 deletions src/Util/PictureUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -95,31 +94,32 @@ 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);
}

// 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),
],
],
];
}

Expand Down Expand Up @@ -149,5 +149,4 @@ public function pictureExists(GalleryCreatorPicturesModel $picturesModel): bool

return true;
}

}
11 changes: 7 additions & 4 deletions src/Util/SecurityUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Markocupic\GalleryCreatorBundle\Util;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\FrontendUser;
use Contao\StringUtil;
Expand All @@ -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,
) {
}

Expand All @@ -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;
Expand Down

0 comments on commit 72254d5

Please sign in to comment.