Using a block to relate a collection item

I have written a custom block to allow users to select an item from a collection. This works perfectly but it stores the content of the linked item in the block.

Is there a way to create a link (as in a relationship like an id) to a collection item so that if the collection item’s content is updated, the content of the block will also update?

Collection

return function($site) {
    return $site->find('testimonials');
};

Block blueprint

name: Testimonial
icon: bolt
fields:
    testimonial:
        label: Testimonial
        type: select
        options: query
        query:
            fetch: kirby.collection("testimonials").testimonials.toStructure
            text: "{{ structureItem.author }}"
            value: "{{ structureItem.text }}::{{ structureItem.author }}"

Block snippet

<?php
$testimonial =  $block->testimonial();
$split = explode('::', $testimonial);
$text = $split[0];
$author = $split[1];

?><blockquote>
    <p><?= $text ?></p>
    <cite class="font-black uppercase text-sm"><?= $author ?></cite>
</blockquote>

I hope this makes sense.

Thanks

The value here defines what you actually store about your item in the field. It would make more sense to store an ID, e.g. one added automatically using the AutoID plugin.

1 Like

Yes, exactly… That looks like the sort of thing I need. I’ll try it out.

Thank you

Using AutoID worked perfectly. Thank you.