New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuggestFieldType with max_items = 1 fail saving with no value #1721

Closed
FrittenKeeZ opened this Issue Jan 15, 2018 · 9 comments

Comments

Projects
None yet
6 participants
@FrittenKeeZ

FrittenKeeZ commented Jan 15, 2018

Expected behaviour

Unsetting a value for a suggest mode with max_items: 1 will save the page as normal.

Actual behaviour

An exception is thrown (ajax) when trying to save the page.
ErrorException in SuggestFieldtype.php line 27: reset() expects parameter 1 to be array, null given
Also affects RelateFieldtype as it extends from SuggestFieldtype.

Steps to reproduce

  1. Add a field of type: suggest and mode: Pages
  2. Select a page and select Save and Continue
  3. Unset the selected page (backspace on focus).
  4. Try to save the page again.
  5. An error alert is shown.

Server Details

Statamic Version: 2.8.0

Fix

Change line 27, 28 in Statamic\Addons\Suggest\SuggestFieldtype to:

if (is_array($data) && $this->getFieldConfig('max_items') === 1) {
    return reset($data);

In Statamic\Addons\Relate\RelateFieldtype just remove the process method so it uses the one from SuggestFieldtype.

@FrittenKeeZ FrittenKeeZ changed the title from SuggestModes with max_items = 1 fail saving when unsetting value to SuggestFieldType with max_items = 1 fail saving with no value Jan 17, 2018

@FrittenKeeZ

This comment has been minimized.

FrittenKeeZ commented Mar 13, 2018

@jasonvarga any chance you can look at this soon? I have to revert these fixes for every new update =/

@gerbenrobijn

This comment has been minimized.

gerbenrobijn commented May 11, 2018

Same problem

@FrittenKeeZ

This comment has been minimized.

FrittenKeeZ commented May 22, 2018

@jackmcdade & @jasonvarga this one is really easy to fix, though tedious to maintain with every update =/

@jackmcdade jackmcdade self-assigned this Jun 4, 2018

@jackmcdade

This comment has been minimized.

Member

jackmcdade commented Jun 4, 2018

I tried to recreate this for an amount of time that would have looked like I lost my marbles, had someone been looking over my shoulder. I have concluded that this behavior was fixed in in 2.9.x as part of our fieldset processing logic. Huzzah!

Please reopen if you find otherwise!

@jackmcdade jackmcdade closed this Jun 4, 2018

@marsvogel

This comment has been minimized.

marsvogel commented Jul 12, 2018

I'm still experiencing this error. Here is what I did:

  1. Create a new statamic 2.9.8 installation: statamic new suggestion-error
  2. Go to site/settings/fieldsets/home.yaml and change it like so:
hide: true
fields:
  posts:
    type: integer
    display: Number of Posts
    width: 25
  content:
    type: markdown
  foo_bar:
    max_items: 1
    create: false
    type: suggest
    mode: Pages
    display: 'Foo Bar'
  1. Open the /cp/pages/edit to edit your home page.
  2. Choose whatever page you like in the new Foo Bar field
  3. Save your page
  4. Now remove the page from the Foo Bar field
  5. Save your Page
  6. You will get this error

screen shot 2018-07-12 at 10 40 21

[2018-07-12 08:40:21] production.ERROR: ErrorException: reset() expects parameter 1 to be array, null given in /Users/christopher/Desktop/suggestion-error/statamic/bundles/Suggest/SuggestFieldtype.php:29
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'reset() expects...', '/Users/christop...', 29, Array)
#1 /Users/christopher/Desktop/suggestion-error/statamic/bundles/Suggest/SuggestFieldtype.php(29): reset(NULL)
#2 /Users/christopher/Desktop/suggestion-error/statamic/core/Data/Processor.php(49): Statamic\Addons\Suggest\SuggestFieldtype->process(NULL)
#3 [internal function]: Statamic\Data\Processor->Statamic\Data\{closure}(NULL, 'foo_bar')
#4 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Support/Collection.php(469): array_map(Object(Closure), Array, Array)
#5 /Users/christopher/Desktop/suggestion-error/statamic/core/Data/Processor.php(51): Illuminate\Support\Collection->map(Object(Closure))
#6 /Users/christopher/Desktop/suggestion-error/statamic/core/CP/Publish/ProcessesFields.php(21): Statamic\Data\Processor->process(Array, true)
#7 /Users/christopher/Desktop/suggestion-error/statamic/core/CP/Publish/Publisher.php(101): Statamic\CP\Publish\Publisher->processFields(Object(Statamic\CP\Fieldset), Array, true)
#8 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Controllers/PublishController.php(105): Statamic\CP\Publish\Publisher->publish()
#9 [internal function]: Statamic\Http\Controllers\PublishController->save(Object(Illuminate\Http\Request))
#10 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(256): call_user_func_array(Array, Array)
#11 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(164): Illuminate\Routing\Controller->callAction('save', Array)
#12 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(112): Illuminate\Routing\ControllerDispatcher->call(Object(Statamic\Http\Controllers\PublishPageController), Object(Illuminate\Routing\Route), 'save')
#13 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#14 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#15 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#16 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#17 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(114): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#18 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(68): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(Statamic\Http\Controllers\PublishPageController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'save')
#19 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'Statamic\\Http\\C...', 'save')
#20 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
#21 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Router.php(708): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#22 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#23 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#24 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/CP/Authenticate.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 [internal function]: Statamic\Http\Middleware\CP\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#27 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/Outpost.php(48): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 [internal function]: Statamic\Http\Middleware\Outpost->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#30 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/CP/Localize.php(25): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 [internal function]: Statamic\Http\Middleware\CP\Localize->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#33 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/CP/DefaultLocale.php(28): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 [internal function]: Statamic\Http\Middleware\CP\DefaultLocale->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#36 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/CpEnabled.php(23): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 [internal function]: Statamic\Http\Middleware\CpEnabled->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#39 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#41 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Router.php(710): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#42 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Router.php(674): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#43 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#44 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#45 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#46 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#47 /Users/christopher/Desktop/suggestion-error/statamic/core/Http/Middleware/PersistStache.php(22): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 [internal function]: Statamic\Http\Middleware\PersistStache->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#50 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#52 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#53 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#54 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#55 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#56 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#58 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#59 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#60 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#61 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#62 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#63 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#64 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#65 /Users/christopher/Desktop/suggestion-error/statamic/core/StaticCaching/Middleware/Retrieve.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#66 [internal function]: Statamic\StaticCaching\Middleware\Retrieve->handle(Object(Illuminate\Http\Request), Object(Closure))
#67 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#68 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#69 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#70 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#71 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#72 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#73 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#74 /Users/christopher/Desktop/suggestion-error/statamic/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#75 /Users/christopher/Desktop/suggestion-error/index.php(155): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#76 /Users/christopher/.composer/vendor/laravel/valet/server.php(147): require('/Users/christop...')
#77 {main}  
@FrittenKeeZ

This comment has been minimized.

FrittenKeeZ commented Jul 18, 2018

@jackmcdade I can confirm this bug still exist in 2.10.0, but I can't reopen the issue.

@marsvogel

This comment has been minimized.

marsvogel commented Jul 18, 2018

@jackmcdade I can confirm too, that this bug exist in 2.10.0

@peda

This comment has been minimized.

peda commented Jul 27, 2018

This still exists on 2.10.2 - the issue is inside RelateFieldtype

    public function process($data)
    {
        $max_items = (int) $this->getFieldConfig('max_items');

        if ($max_items === 1 && is_array($data)) {
            return $data[0];
        }

        return $data;
    }

you are just checking for is_array($data) but if there is nothing selected you do store an empty array - therefore accessing the item at the 0 index will fail

@jackmcdade @jasonvarga

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Jul 31, 2018

Sorry about that everyone. Fixed for next release. To patch, do this:

  • Remove the process method from RelateFieldtype.php
  • Change process in SuggestFieldtype.php to:
    public function process($data)
    {
        $maxItems = (int) $this->getFieldConfig('max_items');

        if ($maxItems === 1 && is_array($data)) {
            $data = reset($data);
        }

        return $data;
    }

@jasonvarga jasonvarga closed this Aug 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment