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.

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

jurgenhaas created an issue. See original summary.

jurgenhaas’s picture

Status: Active » Needs review

Also removed the return value in the constructor.

jurgenhaas’s picture

Updated the MR because the opacity can also be NULL and in PHP 8.1 that needs to be declared accordingly.

itaran’s picture

StatusFileSize
new645 bytes

Thanks 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.

clarkssquared’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new666.24 KB
new238.24 KB

Hi 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.

bronismateusz’s picture

I 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.

bronismateusz’s picture

I wrote working patch for this issue.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: color_field-opacity_wrong_type-3344907-8.patch, failed testing. View results

bronismateusz’s picture

I forgot modified also `ColorFieldFormatterCss.php` file. Now should be okay in layout_builder display and on page.

godotislate’s picture

Status: Needs work » Reviewed & tested by the community

RTBC for the MR changes.

godotislate’s picture

Status: Reviewed & tested by the community » Needs work

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

The website encountered an unexpected error. Please try again later.
TypeError: Drupal\color_field\ColorHex::__construct(): Argument #2 ($opacity) must be of type ?float, string given, called in /var/www/html/web/modules/contrib/color_field/src/Plugin/Field/FieldFormatter/ColorFieldFormatterText.php on line 131 in Drupal\color_field\ColorHex->__construct() (line 30 of modules/contrib/color_field/src/ColorHex.php).
Drupal\color_field\ColorHex->__construct('#EC1818', '0.7') (Line: 131)
Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterText->viewValue(Object) (Line: 81)
godotislate’s picture

Status: Needs work » Needs review

OK, so it looks like, depending on context, the opacity value passed to the ColorHex constructor can be NULL, float/int, or string.

  • When loading opacity value from DB to view in formatter, value is string
  • When editing a paragraph with color field in Layout Paragraphs builder, the opacity will be a float
  • When editing default layout of content type with a color field, opacity can be a int

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.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

@godotislate your latest commit to the MR looks great and should solve all possible issues.

simgui8’s picture

MR looks good and fixes the issue for me.

Thanks

rafmagsou’s picture

Patch #9 updated to Drupal 10 context, apply the same guarantee to #1 parameter.

rafmagsou’s picture

Generate a patch with the solution proposed on the merge request Compatible with D10 php 8.1

darksnow’s picture

Rerolled patch #17 is working for me.

mlzr’s picture

patch #17 is working for me too - thanks!

  • mandclu committed 1c234e3e on 3.0.x
    Issue #3344907 by rafmagsou, jurgenhaas, bronisMateusz, godotislate,...
mandclu’s picture

Status: Reviewed & tested by the community » Fixed

Thanks 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.