TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 195 of /var/www/html/web/core/modules/media_library/src/Form/FileUploadForm.php)

Issue fork drupal-3246558

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

michiellucas created an issue. See original summary.

longwave’s picture

Status: Active » Needs review

How did you trigger this error?

michiellucas’s picture

using the add media button -> popup (ajax) not working

larowlan’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Can you provide a stack trace or steps to reproduce, starting from installing core

Thanks

Also, some specs regarding your environment etc

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pfrenssen’s picture

Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

I had this error also but it was due to a configuration error on my part. I have a media type that has an additional "Cover image" field. This field had a misconfiguration in the form display mode: it was set to display the preview image using the image style "thumbnail" which I suspect is the default, but this image style does not exist in my project.

When I added a new media entity using the media library "Add media" dialog and uploaded a cover image, the AJAX call would fail inside template_preprocess_image_style() in this part of the code:

$style = ImageStyle::load($variables['style_name']); // Returns NULL, style doesn't exist.
// ...
$style->transformDimensions($dimensions, $variables['uri']); // Error: Call to a member function transformDimensions() on null

This would cause the AJAX request to return a 500 error, so the file widget was not replaced and the file ID was not set in the form. When then the media library file upload form was submitted using either the "Save and select" or "Save and insert" buttons, then the error mentioned in the issue summary was thrown since $values['fids'] would not be populated.

In my case the solution was to configure the form display of my media correctly. This made the error go away.

Still, it could be good to fix this, since the code path obviously has a deficiency:

    // Here if 'upload' is empty we initialize $values to an empty array.
    $values = $form_state->getValue('upload', []);
    // On the very next line we assume that $values['fids'] exists and is countable, but this is clearly not always true.
    if (count($values['fids']) > $element['#cardinality'] && $element['#cardinality'] !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      // ...
    }

So I am marking this as RTBC. Having this fixed is already valuable since it will not lead developers on the wrong path to solving the actual problem they are having (like mine I described above).

Since this is exceedingly minor and will not occur during normal use I think this is fine without having a dedicated test. If it is really needed to test this we can perhaps write a simple unit test that passes in a mocked form state with an empty 'upload' value, rather than simulating a failed AJAX request.

catch’s picture

it was set to display the preview image using the image style "thumbnail" which I suspect is the default, but this image style does not exist in my project.

This seems like it might be a core bug in itself - do we need to remove the hardcoding of 'thumbnail' or add some kind of fallback logic for when it's not there?

pfrenssen’s picture

Created followup to address the hardcoding of the thumbnail image style: #3263700: ImageWidget defaults to the "thumbnail" preview image style but this might not exist. It looks like we can best default to an empty string if the image style doesn't exist. This will set the preview image style to "No preview".

catch’s picture

Status: Reviewed & tested by the community » Postponed

I think we should do #3263700: ImageWidget defaults to the "thumbnail" preview image style but this might not exist first - this might end up repairing old configuration that would fix the bug in #6.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

norman.lol’s picture

Run into the same on D 9.4.9. Thumbnail exists. The patch fixes it for me. Full backtrace found in dblog:

TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 211 of /var/www/html/web/core/modules/media_library/src/Form/FileUploadForm.php)
#0 /var/www/html/web/core/modules/media_library/src/Form/FileUploadForm.php(211): count(NULL)
#1 [internal function]: Drupal\media_library\Form\FileUploadForm->validateUploadElement(Array, Object(Drupal\Core\Form\FormState), Array)
#2 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1007): call_user_func_array(Array, Array)
#3 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1070): Drupal\Core\Form\FormBuilder->doBuildForm('media_library_a...', Array, Object(Drupal\Core\Form\FormState))
#4 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1070): Drupal\Core\Form\FormBuilder->doBuildForm('media_library_a...', Array, Object(Drupal\Core\Form\FormState))
#5 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(574): Drupal\Core\Form\FormBuilder->doBuildForm('media_library_a...', Array, Object(Drupal\Core\Form\FormState))
#6 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(320): Drupal\Core\Form\FormBuilder->processForm('media_library_a...', Array, Object(Drupal\Core\Form\FormState))
#7 /var/www/html/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(313): Drupal\Core\Form\FormBuilder->buildForm('Drupal\\dropzone...', Object(Drupal\Core\Form\FormState))
#8 /var/www/html/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(160): Drupal\media_library\MediaLibraryUiBuilder->buildMediaTypeAddForm(Object(Drupal\media_library\MediaLibraryState))
#9 /var/www/html/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(126): Drupal\media_library\MediaLibraryUiBuilder->buildLibraryContent(Object(Drupal\media_library\MediaLibraryState))
#10 /var/www/html/web/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php(824): Drupal\media_library\MediaLibraryUiBuilder->buildUi(Object(Drupal\media_library\MediaLibraryState))
#11 [internal function]: Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::openMediaLibrary(Array, Object(Drupal\Core\Form\FormState), Object(Symfony\Component\HttpFoundation\Request))
#12 /var/www/html/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array(Array, Array)
#13 /var/www/html/web/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(109): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse(Object(Symfony\Component\HttpFoundation\Request), Array, Object(Drupal\Core\Form\FormState), Array)
#14 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#15 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#16 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(229): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...')
#17 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(92): Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object(Drupal\Core\Form\FormAjaxException), Object(Symfony\Component\HttpFoundation\Request), 1)
#18 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#27 {main}
.
anybody’s picture

Issue tags: +PHP 8.1

Same issue here now with PHP 8.1

anybody’s picture

Status: Postponed » Active
StatusFileSize
new57.32 KB

This message already pops up for me when opening the modal with the file upload widget on top and the media selection table below. At that point no file has been uploaded.

So it looks to me, as if the patch was needed or the code has to be adjusted?

It might be related to the other issue in some cases, but doesn't have to, I guess.

See screenshot below:

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jorgik made their first commit to this issue’s fork.

bingol@ciandt.com’s picture

StatusFileSize
new1.1 KB
bingol@ciandt.com’s picture

StatusFileSize
new2.12 KB
bingol@ciandt.com’s picture

StatusFileSize
new2.21 KB
bingol@ciandt.com’s picture

StatusFileSize
new2.32 KB

mialdi98 changed the visibility of the branch 3246558-php8-error to hidden.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.