From 6010aacf3432f48649cc07c5dc4181e281cfff35 Mon Sep 17 00:00:00 2001 From: markhuot Date: Wed, 25 Oct 2023 03:17:56 -0400 Subject: [PATCH] need to eager load data --- src/fields/Keystone.php | 17 ++++++++++++----- src/models/Component.php | 25 ++++++++++++++++++++----- src/templates/components/asset.twig | 1 + src/templates/components/text.twig | 1 + src/templates/field.twig | 1 + 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/fields/Keystone.php b/src/fields/Keystone.php index 1fd88e5..06a2033 100644 --- a/src/fields/Keystone.php +++ b/src/fields/Keystone.php @@ -24,12 +24,19 @@ public static function hasContentColumn(): bool */ protected function getFragment(ElementInterface $element) { + $children = Component::find() + // @todo fix this? + // ->with('data') + ->where([ + 'elementId' => $element->id, + 'fieldId' => $this->id, + ]) + ->orderBy('sortOrder') + ->all(); + $component = new Component; - $component->type = 'keystone/fragment'; - $component->setSlotted(Component::find()->where([ - 'elementId' => $element->id, - 'fieldId' => $this->id, - ])->orderBy('sortOrder')->all()); + $component->data->type = 'keystone/fragment'; + $component->setSlotted($children); return $component; } diff --git a/src/models/Component.php b/src/models/Component.php index 0cc6c24..9b558c1 100644 --- a/src/models/Component.php +++ b/src/models/Component.php @@ -69,11 +69,6 @@ public function __get($name) return $value; } - public function setType($type) - { - $this->data->setAttribute('type', $type); - } - public function getType(): ComponentType { return (new GetComponentType)->byType($this->data->type); @@ -133,6 +128,26 @@ public function render(): string ]); } + public function getExports(): array + { + $exports = new class { + public array $exports = []; + + public function add($key, $value) { + $this->exports[$key] = $value; + } + }; + + $this->getType()->render([ + 'component' => $this, + 'props' => $this->data, + 'attributes' => new AttributeBag($this->data['_attributes']), + 'exports' => $exports, + ]); + + return $exports->exports; + } + public function __toString(): string { $html = $this->render(); diff --git a/src/templates/components/asset.twig b/src/templates/components/asset.twig index 89d5d6c..55b7506 100644 --- a/src/templates/components/asset.twig +++ b/src/templates/components/asset.twig @@ -2,4 +2,5 @@ {% export propTypes = { asset: field('asset').defaultUploadLocationSource('volume:' ~ craft.app.volumes.getAllVolumes[0].uid|default) } %} +{% export summary = props.asset.one().title|default %} diff --git a/src/templates/components/text.twig b/src/templates/components/text.twig index dd93196..2fd86e2 100644 --- a/src/templates/components/text.twig +++ b/src/templates/components/text.twig @@ -1,6 +1,7 @@ {% export propTypes = { text: field('plaintext').multiline(true) } %} +{% export summary = props.text|default('')|slice(0,25) %} {% export icon %}{% endexport %}
{{ props.text }} diff --git a/src/templates/field.twig b/src/templates/field.twig index b75952b..1fa7d5d 100644 --- a/src/templates/field.twig +++ b/src/templates/field.twig @@ -31,6 +31,7 @@
{{ child.getType().getIcon({class: 'k-w-4 k-inline'}) }}
{{ child.getType().getName() }} + {{ child.getExports().summary|default }}
{{ hiddenInput('nodes[]', child.id ~ '@' ~ child.dateUpdated) }}