I have a tag field called textLabel
inside a structure field called tableTexts
.
I am trying to get the tag field’s suggested options using all values from this same tag field, from all pages with the same template named project
fields:
tableTexts:
type: structure
fields:
textLabel:
type: tags
max: 1
options:
type: query
# fetch ...
textContent:
type: textarea
My two best shots are the following, but I get errors (let me know if the error messages are helpful)
(A)
fetch: site.pages.index.template('project').tableTexts.toStructure
text: "{{ structureItem.textLabel }}"
value: "{{ structureItem.textLabel }}"
(B)
fetch: site.pages.index.template('project')
text: "{{ item.tableTexts.toStructure.pluck('textLabel', ',', true) }}"
value: "{{ item.tableTexts.toStructure.pluck('textLabel', ',', true) }}"
None of this will work. You would need a custom method that fetches all the values of all pages and fields into a single collection/array
Where should this method be implemented?
I’m guessing in a page model, but the tags of each page would still have to be merged into a single array.
Yes, page model or a custom page method. Basically, you would loop through the children and fetch all textLabel values into an array.
Page model
class ProjectPage extends Page {
public function getPageTableLabels () {
$pageLabels = [];
foreach ($this->tableTexts()->toStructure() as $row) {
$pageLabels = array_merge($row->textLabel()->split());
}
return $pageLabels;
}
}
In the blueprint
textLabel:
type: tags
max: 1
options:
type: query
fetch: site.pages.index.template('project')
text: "{{ item.getPageTableLabels.split }}"
value: "{{ item.getPageTableLabels.split }}"
I get no errors but no suggestions either
You have to use the model method in the query, not the text/value. BTW. its
type: query
query: ....
No longer fetch
, when you use the new syntax. But I thought you wanted to get the values of all siblings, not just the current page?
Yes, that’s what I’m trying to do.
With this setup (using page model) I don’t understand how to unite the arrays I get for each page into a single one.
(There’s probably something I don’t fully get about the query language)
public function getPageTableLabels () {
$pageLabels = [];
foreach ($this->siblings() as $p) {
foreach ($p->tableTexts()->toStructure() as $label)
$pageLabels[] = $label->textLabel();
}
return $pageLabels;
}
options:
type: query
query: page.getPageTableLabels
text: “{{ item }}”
value: “{{ item }}”
Thanks something similar worked for me.
For some reason I didn’t think I could get data from all pages directly in the page model — I was assuming methods in the page model would have to deal with this page’s data only.
–
Is there a more general place (apart from page models) where I can put a custom method to then use in blueprints (with Query Language)? ‘General’ meaning not relative to a specific template.
Yes, custom methods as I mentioned above, in this case a custom page method, or maybe a site method.