in a current project I use a structure field on a ‘lecturers’ page to manage a list of lecturers, e.g. the fields are the properties of my ‘lecturer model’ (generally spoken, not in Kirby page model terms). I fetch the entries from this field several times on other pages, because lecturers are linked to seminars and they are listed on the frontpage as well.
What I am missing now is a place to put lecturerer model related stuff, i.e. a function that returns the full name by combining first and last name. I’d also like to generate a slug from first name and last name dynamically. Is there any solution to having such model related logic in one place, without refactoring the whole setup to subpages instead?
Do you want to be able to access each lecturer via a unique url where you’ll display that lecturer’s information? If I understand correctly the only way of doing this without changing to subpage is to create a custom route.
You can add such functions in a file in the plugins folder so that they are available anywhere.
Thanks for your answer, Thiousi. In this case there are no lecturer detail pages. Otherwise I would have implemented it as subpages from the beginning.
Thanks texnixe, I thought about that already, but didn’t find information on how to do that for a single function, but now I did
This avoids some repetation, but I still wonder if there is a way to implement some kind of lecturer model where I keep all the logic in one place.
A plugin is actually probably the best place to put logic that will be used across different pages and templates.
The thing I still don’t get is about the slug? What do you want to do with the slug?
As regards the plugins folder, you can put all functions belonging to the lecturer model into a single file, or even create your own lecturer class if necessary.
Hey texnixe, took me some time to answer, because I mixed several thoughts in my opening post that needed to be sorted
I miss the possibility to identify a data set / entry of a structure field.
I have the lecturers page with a structured field, where I store details (description, avatar, maybe Email) of all my lecturers and I have several seminar pages that all have one or more lectureres attached to them (also a structure field). Now I want to iterate over the lectureres of the seminar and fetch the details from the lecuterers page. That’s the point where I thought about a slug, because I also wanna set anchors on the lecturers page (i.e. lectureres/#firstname-lastname).
I just realized I might give custom collection filters a try.
To get a specific entry from a structure field, you can do something like this:
// fetch all lecturers
$lecturers = page('lecturers')->lecturers()->toStructure();
// fetch lecturers on seminar page
$seminarLecturers = $page->seminar_lecturers()->toStructure();
foreach($seminarLecturers as $seminar_lecturer):
// get the details from the lecturers structure field
$lecturerDetails = $lecturers->findBy('lecturer_name', $lecturer->lastname());
As regards the slugs for the anchors, you can simply build them from the firstname/lastname fields, maybe using a helper function, can’t you?