mirror of
https://github.com/flextype/flextype.git
synced 2025-08-06 21:26:48 +02:00
feat(shortcodes): implement new logic for entries
shortcode
This commit is contained in:
@@ -24,11 +24,56 @@ use function entries;
|
||||
use function parsers;
|
||||
use function registry;
|
||||
|
||||
// Shortcode: [entries-fetch id="STRING" field="STRING" default="STRING"]
|
||||
parsers()->shortcodes()->addHandler('entries-fetch', static function (ShortcodeInterface $s) {
|
||||
// Shortcode: [entries]
|
||||
parsers()->shortcodes()->addHandler('entries', static function (ShortcodeInterface $s) {
|
||||
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.entries.enabled')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return collection(entries()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
|
||||
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
|
||||
|
||||
if ($s->getParameter('fetch') != null) {
|
||||
|
||||
// Get vars
|
||||
foreach($s->getParameters() as $key => $value) {
|
||||
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [];
|
||||
}
|
||||
|
||||
// Set options
|
||||
if (isset($vars[1])) {
|
||||
parse_str($vars[1], $options);
|
||||
} else {
|
||||
$options = [];
|
||||
}
|
||||
|
||||
// Prepare options
|
||||
$options = collection($options)->dot()->map(function($value) {
|
||||
if(strings($value)->isInteger()) {
|
||||
$value = strings($value)->toInteger();
|
||||
} elseif(strings($value)->isFloat()) {
|
||||
$value = strings($value)->toFloat();
|
||||
} elseif(strings($value)->isBoolean()) {
|
||||
$value = strings($value)->toBoolean();
|
||||
} elseif(strings($value)->isNull()) {
|
||||
$value = strings($value)->toNull();
|
||||
} else {
|
||||
$value = (string) $value;
|
||||
}
|
||||
return $value;
|
||||
})->undot()->toArray();
|
||||
|
||||
// Backup current entry data
|
||||
$original = entries()->registry()['methods.fetch'];
|
||||
|
||||
// Fetch entry
|
||||
$result = entries()->fetch($vars[0], $options);
|
||||
|
||||
// Restore original entry data
|
||||
entries()->registry()->set('methods.fetch', $original);
|
||||
|
||||
// Return entry as a json string
|
||||
return $result->toJson();
|
||||
}
|
||||
|
||||
return '';
|
||||
});
|
@@ -11,11 +11,13 @@ afterEach(function () {
|
||||
});
|
||||
|
||||
test('[entries-fetch] shortcode', function () {
|
||||
$this->assertTrue(entries()->create('foo', ['title' => 'Foo']));
|
||||
$this->assertEquals('Foo', parsers()->shortcodes()->parse('[entries-fetch id="foo" field="title"]'));
|
||||
$this->assertEquals('Bar', parsers()->shortcodes()->parse('[entries-fetch id="foo" field="bar" default="Bar"]'));
|
||||
|
||||
registry()->set('flextype.settings.parsers.shortcodes.shortcodes.entries.enabled', false);
|
||||
$this->assertEquals('', parsers()->shortcodes()->parse('[entries-fetch id="foo" field="bar" default="Bar"]'));
|
||||
$this->assertTrue(entries()->create('blog', ['title' => 'Blog', 'categories' => "@type:array [entries fetch=\"blog|collection=true&filter[sort_by][key]=date&filter[sort_by][direction]=ASC\" /]"]));
|
||||
$this->assertTrue(entries()->create('blog/post-1', ['title' => 'Post 1']));
|
||||
$this->assertTrue(entries()->create('blog/post-2', ['title' => 'Post 2']));
|
||||
$this->assertTrue(entries()->create('blog/post-3', ['title' => 'Post 3']));
|
||||
$this->assertTrue(entries()->create('categories', ['title' => 'Categories']));
|
||||
$this->assertTrue(entries()->create('categories/cat', ['title' => 'Cat']));
|
||||
$this->assertTrue(entries()->create('categories/dog', ['title' => 'Dog']));
|
||||
|
||||
expect(entries()->fetch('blog')->dot()->count())->toBe(44);
|
||||
});
|
||||
|
Reference in New Issue
Block a user