Skip to content

Commit fd254d9

Browse files
committed
[FEATURE] Add EnclosureViewHelper
Used to obtain image information from a feed item. Usage in template: `<img src="{feed:item.enclosure(attribute:'url')}" />` Related: #23
1 parent a6ee157 commit fd254d9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace Fab\RssDisplay\ViewHelpers\Item;
3+
4+
/*
5+
* This file is part of the Fab/RssDisplay project under GPLv2 or later.
6+
*
7+
* For the full copyright and license information, please read the
8+
* LICENSE.md file that was distributed with this source code.
9+
*/
10+
11+
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
12+
13+
/**
14+
* A View Helper which returns the enclosure of a SimplePie item.
15+
*/
16+
class EnclosureViewHelper extends AbstractViewHelper
17+
{
18+
19+
public function initializeArguments()
20+
{
21+
$this->registerArgument('attribute', 'string', 'The attribute to be obtained', true, 'url');
22+
$this->registerArgument('key', 'int', 'The enclosure item key. Starts with 0.', false, 0);
23+
}
24+
25+
public function render() {
26+
$attribute = $this->arguments['attribute'];
27+
if (!in_array($attribute, ['url', 'length', 'type'], true)) {
28+
$message = sprintf('Attribute "%s" is no valid enclosure attribute.', $attribute);
29+
throw new \InvalidArgumentException($message, 1500032671);
30+
}
31+
/** @var SimplePie_Item $item */
32+
$item = $this->templateVariableContainer->get('item');
33+
/** @var SimplePie_Enclosure $enclosure */
34+
$enclosure = $item->get_enclosure($this->arguments['key']);
35+
if ($enclosure) {
36+
switch ($this->arguments['attribute']) {
37+
case 'url':
38+
return $enclosure->get_link();
39+
case 'length':
40+
return $enclosure->get_length();
41+
case 'type':
42+
return $enclosure->get_type();
43+
default:
44+
return '';
45+
}
46+
}
47+
return '';
48+
}
49+
}

0 commit comments

Comments
 (0)