diff --git a/src/flextype/Support/Parsers/Shortcodes/MediaShortcode.php b/src/flextype/Support/Parsers/Shortcodes/MediaShortcode.php new file mode 100644 index 00000000..0375212f --- /dev/null +++ b/src/flextype/Support/Parsers/Shortcodes/MediaShortcode.php @@ -0,0 +1,17 @@ +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')); + }); +} diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 94192852..c10e20e8 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -354,6 +354,8 @@ parsers: max_nesting_level: INF shortcode: shortcodes: + media: + enabled: true entries: enabled: true raw: diff --git a/tests/Support/Parsers/Shortcodes/MediaShortcodeTest.php b/tests/Support/Parsers/Shortcodes/MediaShortcodeTest.php new file mode 100644 index 00000000..6e4c1b79 --- /dev/null +++ b/tests/Support/Parsers/Shortcodes/MediaShortcodeTest.php @@ -0,0 +1,23 @@ +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"]')); +});