I could use a little help… I´m trying to build a “contactform” block.
My problem is, that I get the error Block error: "Cannot use object of type Kirby\Cms\Block as array" in block type: "contactform". I think the error is thrown, because I try to access $data[‘somevalue’] in the template. I guess I have to pass $data to the block somehow.
As far as I know it is not possible to create controllers for blocks, right? Where to put the controller code?
@pixelijn: I´m not trying to access a field. I’m trying to access form-data passed from the controller.
Ah now I get it. The form example from the cookbook passes $data from the controller containing all the from-data. Inside of blocks $data referes to the current block instance.
So I changed $data to $fromdata and the error is gone.
But I’m still not sure where to put the controller code for my block as there are no block controllers, only page controllers, right?
Hi - did you get this working? If you could share the snippet of your contact form block that would be awesome, I am currently implementing the same thing
Ok, I am stuck at this and still lacking some understanding…
I have gote a template contenu.php that uses a snippet cours.php (as listed page’s children), in that child there are blocks event.php with a contact form that triger a modal window to fill a contact form, the content of the contact form is a snippet registration-form.php
I put the copied code from cookbook in the controllers folder as registration-form.php then I got a Block error: “Undefined variable $success” in “event”.
I try to copy the controller code into the block event.php, but none of the registration-form fields appear…
Can you help with this one? Thank you
A controller always belongs to a template, so it doesn’t make any sense to name that controller register-form.php, because that template does not exist. You would need a controller for contenu.php, because that is the only template involved here.
I’m afraid I couldn’t follow your instructions. Would it be possible to explain the adjustments that are necessary in comparison to the instructions at Email contact form | Kirby CMS? Unfortunately, I still get the error message „Cannot use object of type Kirby\Cms\Block as array" in block type: …“.
Just had the same problem. The issue is that the $data variable is already used by Kirby block logic, so you need to rename the variable that is passed from the controller—otherwise the code in the template is trying to access the wrong $data variable which causes said error.