Json collection?

Is it possible to make a collection that outputs json like this one? Want to make a calendar events editing a bit more easier.

      "start_date":"2019-12-02 00:00:00",
      "end_date":"2019-12-04 00:00:00",
      "text":"dblclick me!",
      "start_date":"2019-12-09 00:00:00",
      "end_date":"2019-12-11 00:00:00",
      "text":"and me!",
      "start_date":"2019-12-16 00:00:00",
      "end_date":"2019-12-18 00:00:00",
      "text":"and me too!",
      "start_date":"2019-12-02 08:00:00",
      "end_date":"2019-12-02 14:10:00",
      "text":"Type 2 event",
   "collections": {

Hello? Does anyone even care?

Hello, we are still a small community and we contribute and help in our spare time - and it was Christmas.

Your question:

It is possible, but you have to do multiple steps.

Create a collection with your fields.

If you don’t need numeric ids, you can use the default _id field with mongodb id style.

For start_date and end_date, you can use the date field. If you need the part 00:00:00 in your output, you can copy and paste the date field component file into path/to/cockpit/config/tags/field-date.tag and modifiy it to produce your exact date format. This way, the core component will be overwritten.

Use a select field for type.

You can create your own field components, if you want to. I needed a custom date range field in the past and published it for inspiration: https://github.com/raffaelj/cockpit-scripts/blob/master/custom-fields/field-date-range.tag

Custom rest api output

You can create your own api endpoint, that produces the format, you want. Create a file /cockpit/config/api/myCustomEndpoint.php and return your format.


$data = cockpit('collections')->find('calendar');

$collections = ['sections' => ['...' => '...']];

return compact('data', 'collections');