Skip to content

Commit

Permalink
Merge pull request #10 from Xirdion/xirdion/feature-1
Browse files Browse the repository at this point in the history
Add copyright info to image template. Thanks @Xirdion
  • Loading branch information
Tastaturberuf authored Apr 8, 2022
2 parents 54d46d5 + 6cd8e50 commit 3621c72
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
68 changes: 68 additions & 0 deletions EventListener/ParseTemplateListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php // with ♥ and Contao

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2022 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\EventListener;


use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\FilesModel;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;


class ParseTemplateListener
{
private ScopeMatcher $scopeMatcher;
private ?Request $request;

public function __construct(ScopeMatcher $scopeMatcher, RequestStack $requestStack)
{
$this->scopeMatcher = $scopeMatcher;
$this->request = $requestStack->getCurrentRequest();
}

/**
* Add the copyright fields to the image template
*
* @param Template $template
*
* @return void
*/
public function onParseTemplate(Template $template): void
{
if (null === $this->request) {
return;
}

if (false === $this->scopeMatcher->isFrontendRequest($this->request)) {
return;
}

// Only the image template should be used
if (false === str_starts_with($template->getName(), 'image')) {
return;
}

// Load the image with the given uuid
$uuid = $template->__get('uuid');
$file = FilesModel::findByUuid($uuid);
if (null === $file) {
return;
}

// Add the file data to the template
$template->__set('ic_href', $file->__get('ic_href'));
$template->__set('ic_copyright', $file->__get('ic_copyright'));
}
}
7 changes: 7 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ services:
arguments:
$rootDir: '%kernel.project_dir%'
$validImageExtensions: '%contao.image.valid_extensions%'

Tastaturberuf\ContaoImageCopyrightBundle\EventListener\ParseTemplateListener:
arguments:
- '@contao.routing.scope_matcher'
- '@request_stack'
tags:
- { name: contao.hook, hook: parseTemplate, method: onParseTemplate, priority: 0 }

0 comments on commit 3621c72

Please sign in to comment.