Problem/Motivation
We get the following exception when opening layout builder with a contained color field:
TypeError: Drupal\color_field\ColorHex::__construct(): Argument #2 ($opacity) must be of type ?string, int given, called in /var/www/html/web/modules/contrib/color_field/src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php on line 312 in Drupal\color_field\ColorHex->__construct() (line 30 of modules/contrib/color_field/src/ColorHex.php).
Drupal\color_field\ColorHex->__construct() (Line: 312)
Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterCss->viewValue() (Line: 230)
Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterCss->viewElements() (Line: 89)
Drupal\Core\Field\FormatterBase->view() (Line: 265)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple() (Line: 266)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple() (Line: 223)
Drupal\Core\Entity\Entity\EntityViewDisplay->build() (Line: 461)
Drupal\Core\Entity\EntityViewBuilder->viewField() (Line: 243)
Drupal\Core\Field\FieldItemList->view() (Line: 162)
Drupal\layout_builder\Plugin\Block\FieldBlock->build() (Line: 106)
Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender()
call_user_func() (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 90)
Drupal\layout_builder\SectionComponent->toRenderArray() (Line: 88)
Drupal\layout_builder\Section->toRenderArray() (Line: 242)
Drupal\layout_builder\Element\LayoutBuilder->buildAdministrativeSection() (Line: 126)
Drupal\layout_builder\Element\LayoutBuilder->layout() (Line: 100)
Drupal\layout_builder\Element\LayoutBuilder->preRender()
call_user_func_array() (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 788)
Drupal\Core\Render\Renderer->doCallback() (Line: 374)
Drupal\Core\Render\Renderer->doRender() (Line: 446)
Drupal\Core\Render\Renderer->doRender() (Line: 204)
Drupal\Core\Render\Renderer->render() (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 243)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
call_user_func() (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 50)
Drupal\ban\BanMiddleware->handle() (Line: 82)
Drupal\crowdsec\Middleware->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
Stack\StackedHttpKernel->handle() (Line: 713)
Drupal\Core\DrupalKernel->handle() (Line: 19)
Proposed resolution
All other ColorXYZ classes declare opacity as a float, only ColorHex uses string or NULL. This should be updated to float too.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | color_field-opacity_wrong_type-3344907-17.patch | 8.26 KB | rafmagsou |
| #16 | color_field-opacity_wrong_type-3344907-11.patch | 660 bytes | rafmagsou |
| #10 | color_field-opacity_wrong_type-3344907-9.patch | 1.34 KB | bronismateusz |
| #8 | color_field-opacity_wrong_type-3344907-8.patch | 655 bytes | bronismateusz |
| #6 | after-patch.png | 238.24 KB | clarkssquared |
Issue fork color_field-3344907
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
Comment #2
jurgenhaasAlso removed the return value in the constructor.
Comment #4
jurgenhaasUpdated the MR because the opacity can also be NULL and in PHP 8.1 that needs to be declared accordingly.
Comment #5
itaran commentedThanks for the proposed solution and PR, hopefully these will be accepted soon. Run into the same problem with php 8.1, attaching quick fix patch with casting $item->opacity to string value.
Comment #6
clarkssquared commentedHi jurgenhaas,
I confirmed the issue was resolved when I applied patch #5 to the "Color Field" against Version
3.0.x-dev. Please look at the screenshots attached.
For your review.
Thank you.
Comment #7
bronismateusz commentedI applied this patch, but when setting the field formatting to Color CSS declaration and trying to save the settings with layout_builder enabled, the error still occurs.
Comment #8
bronismateusz commentedI wrote working patch for this issue.
Comment #10
bronismateusz commentedI forgot modified also `ColorFieldFormatterCss.php` file. Now should be okay in layout_builder display and on page.
Comment #11
godotislateRTBC for the MR changes.
Comment #12
godotislateI was a little hasty in marking as RTBC and didn't test sufficiently. Seems like when loading the entity to be viewed, the opacity value returned from DB is a string, so there are errors with the formatters:
Comment #13
godotislateOK, so it looks like, depending on context, the opacity value passed to the ColorHex constructor can be NULL, float/int, or string.
One simple way would be to use a union type hint of
?string|float, but that is PHP 8+ only.I wonder if there's a core issue to cast values to float when retrieved from DB storage for fields of type float (or similarly for any other fields).
Anyway, for now:
Pushed a commit to the MR to cast opacity to the right value before passing into Constructor in the formatters and in the module file.
Comment #14
jurgenhaas@godotislate your latest commit to the MR looks great and should solve all possible issues.
Comment #15
simgui8 commentedMR looks good and fixes the issue for me.
Thanks
Comment #16
rafmagsou commentedPatch #9 updated to Drupal 10 context, apply the same guarantee to #1 parameter.
Comment #17
rafmagsou commentedGenerate a patch with the solution proposed on the merge request Compatible with D10 php 8.1
Comment #18
darksnowRerolled patch #17 is working for me.
Comment #19
mlzrpatch #17 is working for me too - thanks!
Comment #21
mandclu commentedThanks for everyone's work on this issue. I was able to reproduce the problem, and verify that the patch in #17 resolved it. Merged in.