mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 22:26:46 +02:00
feat(directives): add vars
directive
This commit is contained in:
36
src/flextype/core/Entries/Directives/VarsDirective.php
Normal file
36
src/flextype/core/Entries/Directives/VarsDirective.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?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('onEntriesFetchSingleField', static function (): void {
|
||||
|
||||
if (! registry()->get('flextype.settings.entries.directives.vars.enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$field = entries()->registry()->get('methods.fetch.field');
|
||||
$result = entries()->registry()->get('methods.fetch.result');
|
||||
|
||||
if (is_string($field['value'])) {
|
||||
$field['value'] = preg_replace_callback('/@var\((.*?)\)/', function($matches) use ($result) {
|
||||
return collection($result['vars'])->get($matches[1]);
|
||||
}, $field['value']);
|
||||
}
|
||||
|
||||
entries()->registry()->set('methods.fetch.field.key', $field['key']);
|
||||
entries()->registry()->set('methods.fetch.field.value', $field['value']);
|
||||
});
|
@@ -77,6 +77,8 @@ entries:
|
||||
parsers:
|
||||
shortcodes:
|
||||
enabled: true
|
||||
vars:
|
||||
debug: false
|
||||
directives:
|
||||
markdown:
|
||||
enabled: true
|
||||
@@ -93,8 +95,14 @@ entries:
|
||||
fields:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/FieldsDirective.php"
|
||||
vars:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/VarsDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
vars:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/VarsMacros.php"
|
||||
php:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/PhpMacros.php"
|
||||
|
8
tests/fixtures/settings/settings.yaml
vendored
8
tests/fixtures/settings/settings.yaml
vendored
@@ -73,6 +73,8 @@ entries:
|
||||
parsers:
|
||||
shortcodes:
|
||||
enabled: true
|
||||
vars:
|
||||
debug: false
|
||||
directives:
|
||||
markdown:
|
||||
enabled: true
|
||||
@@ -89,8 +91,14 @@ entries:
|
||||
fields:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/FieldsDirective.php"
|
||||
vars:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Directives/VarsDirective.php"
|
||||
macros:
|
||||
debug: false
|
||||
vars:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/VarsMacros.php"
|
||||
php:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Entries/Macros/PhpMacros.php"
|
||||
|
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('vars directive', function () {
|
||||
entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var(foo)']);
|
||||
|
||||
$this->assertEquals('Foo', entries()->fetch('type-vars')['title']);
|
||||
});
|
Reference in New Issue
Block a user