Skip to content

Commit

Permalink
need to eager load data
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 25, 2023
1 parent 1d2a07d commit 6010aac
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/fields/Keystone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 20 additions & 5 deletions src/models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/templates/components/asset.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<img {{ attributes}} src="{{props.asset.one().getUrl()|default }}">
1 change: 1 addition & 0 deletions src/templates/components/text.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% export propTypes = {
text: field('plaintext').multiline(true)
} %}
{% export summary = props.text|default('')|slice(0,25) %}
{% export icon %}<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><path d="M206,56V88a6,6,0,0,1-12,0V62H134V194h26a6,6,0,0,1,0,12H96a6,6,0,0,1,0-12h26V62H62V88a6,6,0,0,1-12,0V56a6,6,0,0,1,6-6H200A6,6,0,0,1,206,56Z"></path></svg>{% endexport %}
<div {{ attributes }} {% if craft.app.getRequest().isPreview() %}contenteditable data-id='{{ component.getQueryCondition()|json_encode }}'{% endif %}>
{{ props.text }}
Expand Down
1 change: 1 addition & 0 deletions src/templates/field.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<a class="k-text-link" href="{{ cpUrl('keystone/components/edit', params) }}" data-open-keystone-component-editor='{{ params|json_encode }}'>
<div style="background: white; display: inline-block; position: relative; padding: 4px;">{{ child.getType().getIcon({class: 'k-w-4 k-inline'}) }}</div>
{{ child.getType().getName() }}
<span class="k-text-gray-400">{{ child.getExports().summary|default }}</span>
</a>
{{ hiddenInput('nodes[]', child.id ~ '@' ~ child.dateUpdated) }}
</div>
Expand Down

0 comments on commit 6010aac

Please sign in to comment.