Custom fields in Kirby 3

I’ve updated a website from Kirby 2 to 3. There is one custom field used in de panel. I’m trying to get it to work in Kirby 3, without any luck.
I’ve read the documentation of custom fields in Kirby 3, but it’s way to complex for my experience level.

Can anybody help me out?

The custom field counts the amount ($optellen) of rows in the structured field (reservering) of the page.

Can this be done another way then writing a custom field?

The custom field in Kirby 2:

<?php

class countreservationsField extends InfoField
{
  public function result() {
    return null;
  }

  public function element() {
    $element = parent::element();
    $element->addClass('field-with-icon');
    return $element;
  }

  public function input() {
  $optellen = 0;
  	foreach($this->page()->reservering()->toStructure() as $temp): 
  		$optellen = $optellen + $temp->amount()->int();
  	endforeach ;
  		
    return '<div class="text">' . $optellen . '</div>';
  }
}

I would create a page model and then use a query in a regular info field.

I’ll invent that the structure field is in a page of type “hotel” (like, it uses a hotel.yml blueprint) as an example. You’ll need to adapt it to your case, of course.

site/models/hotel.php

<?php

class HotelPage extends Page {
    public function reservationCount() {
        $optellen = 0;
        foreach($this->reservering()->toStructure() as $temp) {
  	        $optellen += $temp->amount()->toInt();
        }
        return $optellen;
    }
}

Then in your blueprint you can use this function like this:

site/blueprints/pages/hotel.yml

fields:
  infofield:
    type: info
    text: "Tellen van reserveringen: {{ page.reservationCount }}"
  reservering:
    type: structure
    # ....
1 Like

Thank you for helping me out. I will try this approach. :+1:

Thanks @rasteiner This works perfectly!