A link between two pages and their children

Hello,

I’m on a new project and I have to make two big different categories of projects, and they are communicating. The “project one” got different project pages that are the reference of a “project two” page, and conversely a “project two” page got multiple “project one” pages in reference.
Then I don’t know what is the best way to make a clear panel for the “non-coder users” that will use it… I was thinking about a system like multiple writers on a blog, and change writers by “projet two” pages, but I don’t know.

Do you have an idea of the way I have to take ?

Have you had a look at the relationship plugin?

1 Like

I was just looking on it !
But the problem is that the users can’t touch the blueprint and add new options… perhaps am I wrong

Note that relationships in Kirby can be fragile if you are not careful. For example, when you reference page A in page B by storing its UID and the UID is later changed by the user, the reference will no longer point anywhere. It is therefore advisable to use the AutoID plugin on top of the relationship plugin.

Also, if a referenced page is deleted, the reference to that page will not be deleted.

1 Like

Options can be dynamic, it depends on your use case how to best solve that. For example, if you tell the plugin to fetch all children of a given page, of course the options will reflect this when more children are added etc. You can also define a field where a user adds new options and the options in. the page are then fetched from that field.

Ok I see like in the controller feature I can access to the userlist

I tried to make a simple plugin to check all my projects of “project one” page writing that :

class MyPlugin {
static function projectlist($field) {
  $kirby = kirby();
  $site = $kirby->site();
  $projectspage = $site->projects();
  $projects = $projectspage->children();

  $result = array();

  foreach ($projects as $project) {
    $result[$project->titlename()] = $project->title() . ' ' . $project->reftext() . '-' . $project->refnum();
  }

  return $result;
}

And it return me Illegal offset type in the Panel

This will not return. a page. Is that a field in site.txt? Or are. you looking for the. page called projects?

And a general recommendation: Never ever call an. object method (like children() in this example) without prior checking if you got a corresponding object, in this case a page. object.

I’m searching for the page called project

May I do something like that :

foreach (page('projects')->children()->visible() as $user) {
  $result[$user->titlename()] = $user->title() . ' ' . $user->reftext() . '-' . $user->refnum();
}

Or was it a goob beginning before ?

Well, I find it ! Thank you so much.
Now I was thinking about something… These relationship are just one way relation, I choose for an element the linked element to it, but when I choose them, they are not linked in the invert way by a “friendship system”… one way love

Of course, you can also link. back but the question is what exactly your use case is and if that is really. necessary.

You can use a hook to setup “2way relationships”. But, as texnixe suggested, I wouldn’t over engineer it and see if it’s really necessary first.

It is, the two pages will contain more than 100 projects children each, and for one project in a side, it will be linked with 2 or 3 in the other.

Now I got a problem with relationship (yeah I’m a php baby sorry)… I tried to show the link with some informations, then I wrote :

<?php foreach($page->link()->split() as $link): ?>
  <li><?= $link ?></li>
<?php endforeach ?> 

It’s ok, it show the link array number (0, 1, 5, 6 Etc.) but when I try to write things like <li><?= $link->title() ?></li> or <li><?= $link->year() ?></li> it doesn’t work.

Should be:

foreach($page->link()->pages(',') as $link) {
  echo $link->title();
}

The pages method turn a comma separated list of uris into a pages collection.

Nothing happen with this method, no echo and no Whoops debugger

What does your blueprint look like, what is stored in your content file? You probably get an empty collection if you store the UID instead of the URI. You can check such stuff if you

dump($page->link()->pages(','));

here is what I have in my blueprint

link:
    label: Link
    type: relationship
    controller: MyPlugin::link

And here my plugin

class MyPlugin {
  static function link($field) {
    $kirby = kirby();
    $site = $kirby->site();
    $projects = $site->page('projects');

    $result = array();

    foreach ($projects->children()->visible() as $user) {
      $result[] = $user->title() . ' • ' . $user->year() . ' • ' . $user->mounth();
    }
    return $result;
  }
}

Then in the panel, I can see $user->title() or $user->year() or $user->mounth()

Again, what does this store in your content file?