Related Objects and Events

Hi,
I have a problem. There are objects in my system (20 pieces) on the object detail page I would like to show events on which the object was used. I have created a multiselectfield in the event itself where I can select the objects. Unfortunately, I can’t get to the events on the object page because they are saved in the event. Actually, I would have to write the events additionally into the object, is that possible?

My blueprint:
          related:
            label: Related articles
            type: pages
            query: site.find('objects').children

Possible, but unnecessary.

Assuming, the parent page of the event pages is events and the multiselect field contains the IDs of the object pages (otherwise, the code would have to be modified):

$objectRelatedEvents = new Pages();
$eventPage.          = page('events');
if ( $eventPage ) {
  $objectReleatedEvents = $eventPage->children()->listed()->filter(function($event) use ( $page ) {
    return in_array($page->id(), $event->multiselectField()->split(','));
  });
endif;

Ok, thank you very much. What would be the best solution to show the assigned events on the object page?
Or do I have a mistake in thinking?

That’s what my code is doing, fetching those events that have the current object assigned, according to how you said you stored them, of course, multiselectField() needs to be replaced with the name of the used field.

Thank you very much.

Unfortunately it doesn’t work with this blueprint.

otherobjects:
label: Objekte
        type: multiselect
        options: query
        query:
          fetch: site.find('objects').children
          value: "{{page.title}}"
          text: "{{page.title}}"



<?php
      $objectRelatedEvents = new Pages();
$eventPage          = page('events');
if ( $eventPage ) {
  $objectReleatedEvents = $eventPage->children()->listed()->filter(function ($event) use ($page) {
    return in_array($page->id(), $event->otherobjects()->split(','));
  });
}
      ?>

It’s because you stored the title instead of the id (which is probably not a good idea because likely to change), but you can replace id with title in the php code

Ok, nothing happend when i change to title in PHP-Code.

Is the id of the events page correct?

What do you get when you dump the array/title inside the closure?

<?php
$objectRelatedEvents = new Pages();
$eventPage          = page('events');
if ( $eventPage ) {
  $objectReleatedEvents = $eventPage->children()->listed()->filter(function ($event) use ($page) {
     dump($event->otherobjects()->split(','));
     dump($page->title());
    return in_array($page->title(), $event->otherobjects()->split(','));
  });
}

Yes, id ist correct.

I get the Title of the Object itself, 4 Times.

  <pre>Array
(
[0] => DWARF
)
</pre><pre>Kirby\Cms\Field Object
(
[title] => DWARF
)
</pre><pre>Array
(
)
</pre><pre>Kirby\Cms\Field Object
(
[title] => DWARF
)
</pre><pre>Array
(
)
</pre><pre>Kirby\Cms\Field Object
(
[title] => DWARF
)
</pre><pre>Array
(
[0] => DWARF
)
</pre><pre>Kirby\Cms\Field Object
(
[title] => DWARF
)
</pre>

Ahm sorry, should be

return in_array($page->title()->value(), $event->otherobjects()->split(','));

Same like above.

With this Code, i get three 111. I have asigned three Events to this Object. Looks good. But the Title, Image would be great.

  <?php

  $objectRelatedEvents = new Pages();
$eventPage          = page('events');
if ( $eventPage ) {
  $objectReleatedEvents = $eventPage->children()->listed()->filter(function ($event) use ($page) {
echo in_array($page->title(), $event->otherobjects()->split(','));
  });
}

Solution: Reverse query for related pages - #3 by jrueda

Don’t know where you got the echo from, this should have been return like in all examples above.

Now it works too, thank you :slight_smile: