Create table block from CSV possible?

Hi there,

before I dive deep into custom block development I’d like to know if it is possible to programmatically fill in the fields of a table block.

My use case is the following: I’d like to import a CSV file and populate the fields of a block in a blocks field (type table) using the contents of the CSV file.

There’s a helpful guide which describes how to read from a CSV file so I can get an array object.

Now the next step would be to create a block table from the table data object.

Any pointers?

This recipe should help: Add blocks to blocks field programmatically | Kirby CMS

Thanks!

But before I could test this I ran into another problem.

I’d like to use a panel dropdown to trigger the convert action. My current code looks like this:

<?php

Kirby::plugin('example/dropdown', [
  'areas' => [
    'site' => function ($kirby) {
      return [
        'dropdowns' => [
          'page.file' => function (string $id, string $filename) {

            $file = Find::file($id, $filename);
            $dropdown = $file->panel()->dropdown();
            $dropdown[] = '-';
            $dropdown[] = [
              'icon'   => 'share',
              'text'   => 'Create Block from CSV',
              'dialog' => 'convert'
            ];

            return $dropdown;
          }
        ],
        'dialogs' => [
          'convert' => [
            'load' => function (string $id) {
              return [
                'component' => 'k-text-dialog',
                'props' => [
                  'text' => 'Convert CSV to Block?'
                ]
              ];
            },
            'submit' => function (string $id) {

              // do the conversion process

              return true;
            }
          ]
        ]
      ];
    },
  ]
]);

I know, it’s very rudimentary. When I trigger the dropdown action I get the following error: Too few arguments to function Kirby\Http\Route::{closure}(), 0 passed and exactly 1 expected.

I assume the id is not passed into the dialog. How can I do that? In the submit callback, I need to access the $page object of the page the file is associated with.