Page::$year is deprecated

Just updated Kirby and getting the error Page::$year is deprecated. This is the code in my controller.

    $events = $events->map(function($event) {
	$event->year = $event->timeline_date()->toDate('Y');
	return $event;
});

I get the issue, using a dynamic property which is likely leading to the issue. I suspect I need to be more specific now on what $event->year actually is but not sure how.

Any ideas?

You could define a public property in an $event page model.

Probably didn’t show enough context, here is all of the relevant code.

	// Collect all of the unique years to create the horizontol menu bar to make year selection easier.

$events = $page->grandchildren()
			   ->filterby('timeline_date', 'date >=', '1933-01-01')
			   ->filterby('timeline_date', 'date <=', '1945-12-31');

$events = $events->map(function($event) {
	$event->year = $event->timeline_date()->toDate('Y');
	return $event;
});

$years = $events->pluck('year', ',', true);


// return unqiue years and relevant events.

return compact('first_events', 'years');

I can only repeat what I wrote above. Create a page model for the $event page (the name of the model depends on the blueprint used for these grandchildren pages) and define a $year property.

Then it will work.

modern php does not allow creating properties on the fly anymore. like suggested you need a page model and for that class define the $year property before using your code above.

Ok, thanks will have a look at that. I guess I thought there was a simpler way to solve this in the controller than having to create a model just for this.

I am such a complete idiot sometimes. Why make it easy with a couple of lines of code when you can overcomplicate it unnecessarily? I knew I was making a total hash of it and searching way too many records just to get the years.

All I needed:

$events = $page->children()
		   ->filterby('title', 'date >=', '1933')
		   ->filterby('title', 'date <=', '1945');

$years = $events->pluck('title');

I have it set up as the following structure to make searching less heavy - Timeline > Year > Event

All I needed was the Year. Originally I was going into every single event to find the year. Pointless!

Other options:

  • Loop through the collection and fetch formatted dates into array.

  • Leave as is and disable PHP 8.2 deprecation warning.

  • Create a custom page method that returns the formatted date and use that with pluck

2 Likes