I wondered if someone could help with getting the Relationship plugin working?
The module installed fine in Kirby v2.5.14. Using this in my blueprint:
subpagesbrand:
label: Sub Pages (Services) For This Section
type: relationship
options: query
query:
fetch: children
max: 3
help: Select the pages that discuss Brand Services
I can now see, select, sort and save the list of child pages in the Panel field without an issue. However, when I come to render anything in a template I get nothing.
For example, this (testing the foreach loop around the sub pages using code suggested in the module docs):
I’m not sure what you mean (which suggested I may have missed a step…)
The Relationship field in question is in a ‘Services’ page which will preview sub-pages for each ‘Service’. The Service pages themselves have a title and icon in their fields which I intend to display on the ‘Services’ page by way of overview.
The Services page is split into three sections, each of which will feature three of the sub-pages to preview. The Relationship field is being used to select which three sub-pages will be feature in each section.
Once there are pages selected in the Services page section panel (the relationship field) is there another step between this and being able to render, say, the title of each chosen page?
For example, I would have though that I could loop through to display these in something like this:
I found an answer that does allow this but it’s not ideal:
// Build Array of Sub Page Slugs
$subPages = explode(',', $page->subpagesbrand());
// Loop Over Array of Slugs
foreach ($subPages as $subPage) {
$thisPageTitle = $page->children()->findByURI($subPage)->title();
echo $thisPageTitle;
}
This assumes the name of the Relationship field is ‘subpagesbrand’ and that all the sub pages are child pages of the one we are searching from. Without the ‘->children()’ selector it doesn’t work and I’m not sure why.
The plugin allows you to use a controller where you can define the options you want to use, including what is stored in the content file. I founds this example in an old project:
// used by the relationship plugin
class PageSelector {
static function pagelist($field) {
$blog = page('blog')->children()->visible()->sortBy('date', 'desc');
$events= page('events')->children()->visible()->sortBy('start_date', 'desc')->filter(function($child) {
return $child->intendedTemplate() == 'event';
});
$options = (new Pages([$blog,$events]));
$result = array();
foreach ($options as $option) {
$result[$option->uri()] = $option->title();
}
return $result;
}
}
In blueprint:
related:
label: Related
type: relationship
controller: PageSelector::pagelist