area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.77k stars 576 forks source link

Fix single module browser #2474

Closed Tofandel closed 8 months ago

Tofandel commented 8 months ago

The documentation refers to a moduleName option for a single module browser, which doesn't seem to exist on the form builder, the code suggests that it should be possible using modules([Module::class]) and so does the doc

Browser::make()
    ->modules([Publications::class])
    ->name('publications')
    ->max(4);

And so the previous form field should be a browser for a single module

However that's not the case as can be seen from the dropdown

image

This is due to count($modules) === 1 && ! isset($modules[0]) which doesn't make much logical sense

With the PR this is what becomes: image

ifox commented 8 months ago

@Tofandel related discussion here: https://github.com/area17/twill/issues/2424#issuecomment-1931642021

Tofandel commented 8 months ago

I figured it could be true and I in fact worked around it using the key 1, but it's not clean and just a workaround and not what the doc says to use, so I fixed the condition so that now any array with a single string in it, no matter its key will work correctly

It handles [Post::class] or ['post' => Post::class]as a single module and [['name' => 'Post']] as multiple modules which is the normally expected behaviour

ifox commented 8 months ago

I'm totally with you on this, just wanted to relate the 2 discussions and give more context to avoid introducing any breaking changes.