getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.27k stars 167 forks source link

Custom dropdown defined as closure is being accessed as array #3970

Closed sebastiangreger closed 2 years ago

sebastiangreger commented 2 years ago

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:

Stacktrace:
#9 Error in /home/username/Projects/websites/.kirby/3.6-latest/src/Panel/Panel.php:454
#8 Kirby\Panel\Panel:routesForDropdowns in /home/username/Projects/websites/.kirby/3.6-latest/src/Panel/Panel.php:368
#7 Kirby\Panel\Panel:routes in /home/username/Projects/websites/.kirby/3.6-latest/src/Panel/Panel.php:300
#6 Kirby\Panel\Panel:router in /home/username/Projects/websites/.kirby/3.6-latest/config/routes.php:101
#5 Kirby\Http\Route:{closure} in [internal]:0
#4 Closure:call in /home/username/Projects/websites/.kirby/3.6-latest/src/Http/Router.php:107
#3 Kirby\Http\Router:call in /home/username/Projects/websites/.kirby/3.6-latest/src/Cms/App.php:341
#2 Kirby\Cms\App:call in /home/username/Projects/websites/.kirby/3.6-latest/src/Cms/App.php:1077
#1 Kirby\Cms\App:render in /home/username/Projects/websites/sgnet/index.php:11
#0 require in /home/username/.config/composer/vendor/cpriego/valet-linux/server.php:232

To Reproduce
Steps to reproduce the behavior:

  1. Create a custom area and define a dropdown as presented in the example from the docs.
  2. See error

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 key pattern but $dropdown is actually a Closure, not an array?

sebastiangreger commented 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) {
          ...
distantnative commented 2 years ago

@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:

That seems to be the same for the two code snippets you posted. Where is the discrepancy?

sebastiangreger commented 2 years ago

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.

sebastiangreger commented 2 years ago

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

bastianallgeier commented 2 years ago