Relationship Field Issue in Kirby V2

Hi there,

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):

<?php foreach ($page->subpagesbrand()->pages(', ') as $subPage): ?>
					
					<?php echo "Item "; ?>
				
				<?php endforeach; ?>

Renders nothing at all and yet this added just prior to the foreach:

<?php print_r($page->subpagesbrand()); ?>

Renders (as expected):

(
[page] => services
[key] => subpagesbrand
[value] => brand-positioning, test-brand-service

)

Showing the two sub pages selected in Panel are there.

My PHP version is 7.3.9.

Anyone have any ideas?

Thank you!

Try without the space after the comma

Hi, thank you. Sadly that wasn’t it. I’ve tried with and without but no luck sadly.

How is the content actually stored in the field?

Hi,

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:

<?php foreach ($page->subpagesbrand()->pages(',') as $subPage) {
					if($thisPage = $subPage()->toPage()) {
						echo "<h3>{$thisPage->title()}</h3>";
					}
				}
				?>

Am I missing something?

Thanks again for you help.

I would like to see how the stored value looks in the .txt field for that page…

To me it seems that the ids instead of just the slugs of the pages should be saved.

Oh I see! No Problem, it looks like this so yes, it looks like slugs rather than ids:

Subpagesbrand: brand-positioning, test-brand-service

Thanks again.

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.

Hopefully this will help someone.

It’s a long while that I have used the plugin, but isn’t it possible to store the id rather than just the slug via a plugin setting?

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

Ah, thank you.

I have a working solution at the moment but I’ll dig into this when I get a moment.