Closed sebastiangreger closed 2 years ago
Jep, the following works just fine:
...
'dropdowns' => [
'bookmarks/(:any)' => [
'action' => function (string $id) {
return [
...
…the problem is indeed the Closure from the example code.
On the other hand, the Closure notion works just fine when overriding a core dropdown:
'areas' => [
'site' => function ($kirby) {
return [
'dropdowns' => [
'page' => function (string $id) {
...
@sebastiangreger I need your explanation a bit more:
If I look at the docs (https://getkirby.com/docs/reference/plugins/extensions/panel-dropdowns#custom-dropdowns), I see the following in all examples:
dropdowns
is an arrayThat seems to be the same for the two code snippets you posted. Where is the discrepancy?
The described issue occurred when using the notion from the examples to define a new dropdown.
each dropdown array entry is a closure
That seems to be the part that triggered the error when using the code from the docs' examples. When defining a custom dropdown, I had to define the dropdown array entries as arrays (as in my first snippet) in order to get it to work. When using closures for the dropdown array, it gets stuck in line 454 (as it, if I read that line correctly, expects an array not a closure?).
The second snippet refers to a test I did, using the "each dropdown array entry as closure" notion to override (rather than defining a new one) an existing dropdown. In that context, the closure notion – as presented in the examples from the docs – works just fine.
Hope that helps; please let me know if you cannot replicate the error and I'll try to come up with a better way to demonstrate it.
The custom dropdown example from the latest cookbook article https://getkirby.com/docs/cookbook/panel/advanced-panel-area#options-dropdown is essentially the long form of my first snippet above: it uses an array not a closure to define the dropdown. That's the way I got it to work as well.
Update: I tested this with the downloaded "Products" panel area demo from the cookbook article on a fresh starterkit: if I change the code in index.php
and dropdowns/product.php
to follow the closure syntax from the docs article, I can reproduce the exact error described above. My modified version of the demo plugin here: products_modified_to_reproduce_error.zip
✅
Describe the bug
Following the sample code from https://getkirby.com/docs/reference/plugins/extensions/panel-dropdowns#custom-dropdowns returns an
Error thrown with message "Cannot use object of type Closure as array"
error:To Reproduce
Steps to reproduce the behavior:
Expected behavior
Errm… the sample code should not fail with an error? ;) Either the example or the code seems to have a bug.
Kirby Version
3.6.0 on Valet Linux; happens both on PHP 7.4 and 8.0
Additional context
The culprit seems to be in line 454 where
$dropdown['pattern']
tries to access array keypattern
but$dropdown
is actually a Closure, not an array?