|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.u. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Twig; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Processor\EmbeddedImageProcessor; |
|
22
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
|
23
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; |
|
24
|
|
|
use Twig\Extension\AbstractExtension; |
|
25
|
|
|
use Twig\TwigFunction; |
|
26
|
|
|
|
|
27
|
|
|
final class ArticleBodyExtension extends AbstractExtension |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var MediaManagerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $mediaManager; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var FileExtensionCheckerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $fileExtensionChecker; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(MediaManagerInterface $mediaManager, FileExtensionCheckerInterface $fileExtensionChecker) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->mediaManager = $mediaManager; |
|
42
|
|
|
$this->fileExtensionChecker = $fileExtensionChecker; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return TwigFunction[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFunctions(): array |
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
new TwigFunction('setRendition', [$this, 'setRendition']), |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setRendition(Meta $articleMeta, string $renditionName): void |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$articleMeta->getValues() instanceof ArticleInterface) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var ArticleInterface $article */ |
|
62
|
|
|
$article = $articleMeta->getValues(); |
|
63
|
|
|
$embeddedImageProcessor = new EmbeddedImageProcessor($this->mediaManager, $this->fileExtensionChecker); |
|
64
|
|
|
$embeddedImageProcessor->setDefaultImageRendition($renditionName); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($article->getMedia() as $media) { |
|
67
|
|
|
$embeddedImageProcessor->process($article, $media); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|