-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Xirdion/xirdion/feature-1
Add copyright info to image template. Thanks @Xirdion
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters