Hi all,
I’m currently working on a site with vaguely this structure:
content/people/1-john-doe/person.txt
content/people/2-jane-doe/person.txt
content/events/1-disco/event.txt
content/events/2-lecture/event.txt
I’m wondering if there’s a simple way using hooks to update any person.txt
while editing any event.txt
in the panel. Think of it like this: on the page for 2-lecture I list that 1-john-doe was at the event. I do this with a page
panel field that updates adds page: people/john-doe
to 2-lecture/event.txt
, so that it looks like this:
Title: Lecture
----
In-attendance:
–
person: people/john-doe
which works great. But when I do that, I also want to edit 1-john-doe/person.txt
to do basically the inverse and list that they took part in this event. The end goal is for person.txt to look like this:
Title: John Doe
----
Attended-events:
—
event: events/lecture
and then any time a new event is created and a person is listed as having attended it would update their .txt file over time to eventually be something like:
Title: John Doe
----
Attended-events:
—
event: events/lecture
—
event: events/party
—
event: events/skygazing
the only way I can think to do this right now is to have a hook on panel.page.update
like so:
kirby()->hook('panel.page.update', function($page, $oldPage = null) {
//Check if there are any attendees listed
if($page->attendees()->isNotEmpty()):
foreach($page->attendees()->toStructure() as $attendee):
// Since we have to write to the correct URL and sometimes they have indexes:
$personUrl = $attendee->person()->value();
$personIndex = site()->find('people')->children()->indexOf($personUrl)+1;
$personFolder = $personIndex . '-' . explode('/' $personUrl)[1];
// /content/people/john-doe becomes /content/people/1-john-doe
$personPath = kirby()->roots()->content() . '/people/' . $personFolder;
//create the string to append to the end of person.txt:
$thisEvent = "-\r\n event: " . $page->uri();
//append it
f::write( $personPath . '/person.txt', $thisEvent, true);
endforeach;
endif;
Which, basically, finds the correct folder through a slightly messy process of adding the indexes back into the URI, then appends:
-
event: events/*EVENT-PAGE-BEING-EDITED-IN-PANEL*
to the bottom of it.
This does work, however it seems precarious seeing as how in order for this to work the “Attended-events” section will have to remain at the bottom of each person.txt file permanently.
Which gets me to my question:
This idea i’ve concocted—manually adding content to a field at the end of a file—works, but seems incredibly harebrained and I feel confident that there is a faster/safer/any other way to approach something like this that might already be built in. Is there?