mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(shortcodes): add new shortcode - media_files_fetch
example: [media_files_fetch id="foo.txt" field="title" default="Bar"]
This commit is contained in:
17
src/flextype/Support/Parsers/Shortcodes/MediaShortcode.php
Normal file
17
src/flextype/Support/Parsers/Shortcodes/MediaShortcode.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [media_files_fetch id="media-id" field="field-name" default="default-value"]
|
||||
if (flextype('registry')->get('flextype.settings.parsers.shortcode.shortcodes.media.enabled')) {
|
||||
flextype('parsers')->shortcode()->addHandler('media_files_fetch', static function (ShortcodeInterface $s) {
|
||||
return arrays(flextype('media')->files()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
|
||||
});
|
||||
}
|
@@ -354,6 +354,8 @@ parsers:
|
||||
max_nesting_level: INF
|
||||
shortcode:
|
||||
shortcodes:
|
||||
media:
|
||||
enabled: true
|
||||
entries:
|
||||
enabled: true
|
||||
raw:
|
||||
|
23
tests/Support/Parsers/Shortcodes/MediaShortcodeTest.php
Normal file
23
tests/Support/Parsers/Shortcodes/MediaShortcodeTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/media')->create(0755, true);
|
||||
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
|
||||
filesystem()->directory(PATH['project'] . '/media')->delete();
|
||||
});
|
||||
|
||||
test('test media_files_fetch shortcode', function () {
|
||||
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('serializers')->yaml()->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
filesystem()->file(PATH['project'] . '/media/bar.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/media/.meta/bar.txt.yaml')->put(flextype('serializers')->yaml()->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
|
||||
$this->assertEquals('Foo', flextype('parsers')->shortcode()->process('[media_files_fetch id="foo.txt" field="title"]'));
|
||||
$this->assertEquals('Bar', flextype('parsers')->shortcode()->process('[media_files_fetch id="foo.txt" field="foo" default="Bar"]'));
|
||||
});
|
Reference in New Issue
Block a user