Counting in panel

panel

#1

I use a structured field for listing reservations: name, email, ticket amount.
I would like to see in the panel the total amount of all the reservations combined. So how do I count all the ticket amounts in the panel? It only has to be visible in the panel. How should I approach this?


#2

You’d have to create a custom field to display that information.


#3

Ok, so then the counting logic is written in the custom field?

And the read the data from the structured field. Does that work the same way is it would on a template?


#4

Might make more sense to make a dashboard widget if its just a count up you want.


#5

If you base it on the info field, it can be. as basic as this:

<?php

class TestField extends InfoField
{



  public function result() {
    return null;
  }

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

  public function input() {
    return '<div class="text"> The Structure Field X contains ' . $this->page()->fieldname()->toStructure()->count() . ' items</div>';
  }
}

If you want to make it more versatile, instead of hardcoding the name of the structure field, you could add it as an attribute of the field.

<?php

class TestField extends InfoField
{

  public $fieldname;

  public function result() {
    return null;
  }

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

  public function input() {
    return '<div class="text"> The Structure Field '.$this->fieldname.' contains '.$this->page()->{$this->fieldname}()->toStructure()->count().' items</div>';
  }
}

In your blueprint:

fields:
  test:
    label: No of items
    type: test
    fieldname: contactoptions

A widget makes sense if you don’t want to display it in the page itself but on the dashboard.


#6

Ah yes, that makes a lot of sense.

After some fiddling, it works!
Thanks a lot!