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:
35
src/flextype/core/Entries/Directives/FieldsDirective.php
Normal file
35
src/flextype/core/Entries/Directives/FieldsDirective.php
Normal 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);
|
||||
});
|
@@ -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:
|
||||
|
3
tests/fixtures/settings/settings.yaml
vendored
3
tests/fixtures/settings/settings.yaml
vendored
@@ -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:
|
||||
|
@@ -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']);
|
||||
});
|
Reference in New Issue
Block a user