Skip to content

Commit

Permalink
entry queries and assets all use templates now
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 29, 2023
1 parent abf3117 commit 923fbec
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/actions/GetComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function byType(string $type): ComponentType
$fqcn = $className;
}

if ($fqcn) {
if (! empty($fqcn)) {
return Craft::$container->get($fqcn, ['context' => $this->context]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/listeners/RegisterDefaultComponentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class RegisterDefaultComponentTypes
public function handle(RegisterComponentTypes $event): void
{
$event->registerTwigTemplate('keystone/asset', 'cp:keystone/components/asset.twig');
$event->registerTwigTemplate('keystone/elementquery', 'cp:keystone/components/elementquery.twig');
$event->registerTwigTemplate('keystone/entry', 'cp:keystone/components/entry.twig');
$event->registerTwigTemplate('keystone/entryquery', 'cp:keystone/components/entryquery.twig');
$event->registerTwigTemplate('keystone/fragment', 'cp:keystone/components/fragment.twig');
$event->registerTwigTemplate('keystone/heading', 'cp:keystone/components/heading.twig');
$event->registerTwigTemplate('keystone/icon', 'cp:keystone/components/icon.twig');
Expand Down
3 changes: 3 additions & 0 deletions src/templates/asset/embed.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if asset|default(false) %}
<img {{ attributes}} src="{{asset.getUrl(transform|default(null)) }}">
{% endif %}
5 changes: 4 additions & 1 deletion src/templates/components/asset.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
} %}
{% export summary = props.asset.one().title|default %}
{% export category = "Data" %}
{% set slot = component.getType.defineSlot().defaults([{type: 'keystone/template', data: {template: 'cp:keystone/asset/embed'}}]) %}
{% for asset in props.asset %}
<img {{ attributes}} src="{{asset.getUrl(props.transform.value|default(null))|default }}">
{% do component.mergeContext({asset: asset, transform: props.transform.value|default(null)}) %}
{{ slot }}
{% do component.mergeContext({asset: null, transform: null}) %}
{% endfor %}
2 changes: 1 addition & 1 deletion src/templates/components/entry.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
} %}
{% export summary = props.entry.one().title|default %}
{% export category = "Data" %}
{% set slot = component.getType().defineSlot() %}
{% set slot = component.getType().defineSlot().defaults([{type: 'keystone/template', data: {template: 'cp:keystone/entry/link'}}]) %}
{% for entry in props.entry %}
{% do component.mergeContext({entry: entry}) %}
{{ slot }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{% export category = "Data" %}
{% export name = "Element Query" %}
{% export name = "Entry Query" %}
{% export icon %}<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><path d="M34,64a6,6,0,0,1,6-6H216a6,6,0,0,1,0,12H40A6,6,0,0,1,34,64Zm6,70h72a6,6,0,0,0,0-12H40a6,6,0,0,0,0,12Zm88,52H40a6,6,0,0,0,0,12h88a6,6,0,0,0,0-12Zm108.24,10.24a6,6,0,0,1-8.48,0l-21.49-21.48a38.06,38.06,0,1,1,8.49-8.49l21.48,21.49A6,6,0,0,1,236.24,196.24ZM184,170a26,26,0,1,0-26-26A26,26,0,0,0,184,170Z"></path></svg>{% endexport %}
{% export propTypes = {
search: field('\\markhuot\\keystone\\fields\\Condition'),
limit: field('\\craft\\fields\\Number'),
} %}
{% set defaultSlot = component.getType().defineSlot() %}
{% set elements = props.search.limit(props.limit|default(100)).all()|default([]) %}
{% set elements = props.search.all()|default([]) %}
{% for element in elements %}
{{ defaultSlot.render({element: element}) }}
{{ defaultSlot.render({entry: element}) }}
{% endfor %}
1 change: 1 addition & 0 deletions src/templates/components/template.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% export summary = props.template %}
{% if props.template %}
{% set parts = props.template|split(':') %}
{% if parts|length == 2 %}
Expand Down
4 changes: 3 additions & 1 deletion src/templates/entry/link.twig
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<a href="{{ entry.uri }}">{{ entry.title }}</a>
{% if entry|default(false) %}
<a href="{{ entry.uri }}">{{ entry.title }}</a>
{% endif %}
11 changes: 6 additions & 5 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
$this->actingAsAdmin()
->get(UrlHelper::cpUrl('keystone/components/edit', $component->getQueryCondition()))
->assertOk();
})->with([
'keystone/asset', 'keystone/elementquery', 'keystone/fragment',
'keystone/icon', 'keystone/link', 'keystone/heading',
'keystone/section', 'keystone/text',
]);
})->with(collect(scandir(__DIR__.'/../src/templates/components'))
->filter(fn ($file) => ! str_starts_with($file, '.'))
->filter(fn ($file) => str_ends_with($file, '.twig'))
->map(fn ($file) => 'keystone/'.preg_replace('/\.twig$/', '', $file))
->all()
);

it('stores a component', function () {
$component = Component::factory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

it('renders element queries', function () {
$entry = Entry::factory()->title('foobarbaz')->create();
$component = Component::factory()->type('keystone/elementquery')->create();
$component = Component::factory()->type('keystone/entryquery')->create();
$test = Component::factory()->type('keystone/text')->path($component->id)->create();
$test->data->merge(['text' => '{element.title}'])->save();
$test->data->merge(['text' => '{entry.title}'])->save();
$response = $component->render();
expect($response)->toContain('foobarbaz');
});

0 comments on commit 923fbec

Please sign in to comment.