Block development with bID always NULL

Hello guys,

I’m developing a package which also installs a block. Package and block installation works fine. I can add the block by drag and drop to any page.

But whenever I try to edit the block, the information for the edit form is an empty array. I found out that the block ID (bID) in the block controller is NULL at this point. So when I execute a SELECT query on my btTable for the certain block, I just get an empty array since the block ID is just NULL.

I checked the doumentation which explains how to build a block controller. It says that you can access the block ID with $this->bID. But somehow this wouldn’t work.

Can anybody explain to me what is happening here and why bID is null at this point?

Block Controller - edit() method:
edit_method

Greetings,
Patrick

bID doesn’t exist until after a block has been added. Then it changes each time a block is saved - that is how block versions work.

You should never need to look directly at your btTable. The core will load block data into your controller and to save you only need to make sure post data is good and pass to the parent.

Where bID is used to connect secondary tables, there is also a bunch of code to keep the connection updated when a block is saved, or to clear garbage when a block is deleted, or to track back through block versions.

Thank you for your reply.

I looked into the table, the post data is stored correctly. But how do I access the data then? Do I have to use the $this->set() function to access the data in my edit.php?

I already tried to use the table column names as variable names in my edit.php because I read that the core injects block data directly into the edit.php.
For example I have a column named “formName”. So I tried to output the form name with $formName. But even this doesn’t work…

edit.php:
edit

Its populated as controller class properties and also set for the view.

Suppose you have db.xml <field name="my_text" type="string" size="255"/>

In the controller class, you can access it as $this->my_text;

In the add/edit/view.php, its populated as $my_text.

in add/edit.php, echo $form->text('my_text', $my_text);

in view.php you can echo h($my_text); ( h() escapes html)

In the controller method validate($args) or save($args), it will be $args['my_text'];

You don’t need to explicitly update the table, the parent::save($args) will do that for you.

Building blocks is probably the best documented part of concrete CMS and there are many examples in the core blocks and in free marketplace blocks.

Thanks for the information. I tried to output my data the way you explained it but I am always getting displayed NULL value.

My problem might be somewhere else then.

Look at a simple core block, such as the html block (but ignore the javascript for the syntax editor)

In php8, I am not seeing this auto-population working in our custom block, I get “$my_text is undefined” errors. If I use $controller->my_text in the add/edit/view, it works.

Its auto populated with the field my_text from the block database.
If your controller messes with my_text before the add/edit/view, then you need to set() it.

In essence, the code behind the block controller takes my_text from the database and does a set() on the name and value. So if you mess with the value, the automated set() has already been done and you need to explicitly set() it because you have changed it.