1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(directives): add fields directive

This commit is contained in:
Awilum
2022-05-11 21:14:00 +03:00
parent 865897f80f
commit c41cf136eb
4 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
use Glowy\Arrays\Arrays as Collection;
emitter()->addListener('onEntriesFetchSingleDirectives', static function (): void {
if (! registry()->get('flextype.settings.entries.directives.fields.enabled')) {
return;
}
$field = entries()->registry()->get('methods.fetch.field');
$entry = entries()->registry()->get('methods.fetch.result');
if (is_string($field)) {
$field = preg_replace_callback('/@field\((.*?)\)/', function($matches) use ($entry) {
return $entry[$matches[1]] ?? '';
}, $field);
}
entries()->registry()->set('methods.fetch.field', $field);
});

View File

@@ -90,6 +90,9 @@ entries:
constants:
enabled: true
path: "/src/flextype/core/Entries/Directives/ConstantsDirective.php"
fields:
enabled: true
path: "/src/flextype/core/Entries/Directives/FieldsDirective.php"
macros:
debug: false
php:

View File

@@ -86,6 +86,9 @@ entries:
constants:
enabled: true
path: "/src/flextype/core/Entries/Directives/ConstantsDirective.php"
fields:
enabled: true
path: "/src/flextype/core/Entries/Directives/FieldsDirective.php"
macros:
debug: false
php:

View File

@@ -0,0 +1,16 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->create();
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('fields directive', function () {
entries()->create('field', ['foo' => '@field(id)']);
$this->assertEquals('field', entries()->fetch('field')['foo']);
});