mirror of
https://github.com/flextype/flextype.git
synced 2025-08-07 05:36:54 +02:00
feat(shortcodes): add ability to get specific field with [entries]
shortcode
This commit is contained in:
@@ -30,50 +30,59 @@ parsers()->shortcodes()->addHandler('entries', static function (ShortcodeInterfa
|
||||
return '';
|
||||
}
|
||||
|
||||
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
|
||||
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
|
||||
$result = '';
|
||||
|
||||
if ($s->getParameter('fetch') != null && registry()->get('flextype.settings.parsers.shortcodes.shortcodes.entries.fetch.enabled') === true) {
|
||||
foreach($s->getParameters() as $key => $value) {
|
||||
|
||||
if ($key == 'fetch' && registry()->get('flextype.settings.parsers.shortcodes.shortcodes.entries.fetch.enabled') === true) {
|
||||
|
||||
// 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();
|
||||
// Set options
|
||||
if (isset($vars[1])) {
|
||||
parse_str($vars[1], $options);
|
||||
} else {
|
||||
$value = (string) $value;
|
||||
$options = [];
|
||||
}
|
||||
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);
|
||||
// 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);
|
||||
|
||||
// Return entry as a json string
|
||||
return $result->toJson();
|
||||
// Restore original entry data
|
||||
entries()->registry()->set('methods.fetch', $original);
|
||||
|
||||
// Convert entry as a json string
|
||||
$result = $result->toJson();
|
||||
}
|
||||
|
||||
// Get specific field value or return default value.
|
||||
if ($key == 'field' && $value !== null) {
|
||||
|
||||
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [''];
|
||||
|
||||
$result = collectionFromJson($result)->get($vars[0], $vars[1] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
return $result;
|
||||
});
|
@@ -11,7 +11,7 @@ afterEach(function () {
|
||||
});
|
||||
|
||||
test('[entries-fetch] shortcode', function () {
|
||||
$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', ['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']));
|
||||
@@ -20,4 +20,10 @@ test('[entries-fetch] shortcode', function () {
|
||||
$this->assertTrue(entries()->create('categories/dog', ['title' => 'Dog']));
|
||||
|
||||
expect(entries()->fetch('blog')->dot()->count())->toBe(44);
|
||||
|
||||
$this->assertTrue(entries()->create('blog-2', ['title' => 'Blog', 'category-cat' => "[entries fetch=\"categories/cat\" field=\"title,Foo\" /]"]));
|
||||
expect(entries()->fetch('blog-2')['category-cat'])->toBe('Cat');
|
||||
|
||||
$this->assertTrue(entries()->create('blog-3', ['title' => 'Blog', 'category-cat' => "[entries fetch=\"categories/cat\" field=\"title2,Foo\" /]"]));
|
||||
expect(entries()->fetch('blog-3')['category-cat'])->toBe('Foo');
|
||||
});
|
||||
|
Reference in New Issue
Block a user