Problem/Motivation

When using the custom field linkit form widget on PHP 8.1 you get an error when saving the entity with the custom field. The problem is the $value['options']['attributes'] array is not set when trying to merge the $value['attributes'] in.
The error:

TypeError: Unsupported operand types: null + array in Drupal\custom_field_linkit\Plugin\CustomField\FieldWidget\LinkitWidget->massageFormValue() (line 202 of modules/contrib/custom_field/modules/custom_field_linkit/src/Plugin/CustomField/FieldWidget/LinkitWidget.php).
Drupal\custom_field\Plugin\Field\FieldWidget\CustomWidgetBase->massageFormValues(Array, Array, Object) (Line: 513)
Drupal\Core\Field\WidgetBase->extractFormValues(Object, Array, Object) (Line: 256)
Drupal\Core\Entity\Entity\EntityFormDisplay->extractFormValues(Object, Array, Object) (Line: 334)
Drupal\Core\Entity\ContentEntityForm->copyFormValuesToEntity(Object, Array, Object) (Line: 301)
Drupal\Core\Entity\EntityForm->buildEntity(Array, Object) (Line: 155)
Drupal\Core\Entity\ContentEntityForm->buildEntity(Array, Object) (Line: 186)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 82)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 275)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'node_product_edit_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('node_product_edit_form', Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('node_product_edit_form', Array, Object) (Line: 321)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext():121}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::onController():96}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 263)
Drupal\shield\ShieldMiddleware->bypass(Object, 1, 1) (Line: 171)
Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 715)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Set the $value['options']['attributes'] to an empty array when it is not set.

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

harrrrrrr created an issue. See original summary.

apmsooner’s picture

Status: Active » Postponed (maintainer needs more info)

Hi, please try the patch in this issue as i think it might already be resolved: https://www.drupal.org/project/custom_field/issues/3558244

harrrrrrr’s picture

StatusFileSize
new726 bytes
harrrrrrr’s picture

Status: Postponed (maintainer needs more info) » Needs review

The patch in the other issue doesn't fix this issue. I'm also not using layout builder in this case, it's a custom field on a node with the linkit form widget (without any attributes enabled).

apmsooner’s picture

Okay, thanks. It's an easy fix so I will merge this in today when I get a chance to test myself.

harrrrrrr’s picture

StatusFileSize
new1.19 KB

There was another issue with a multiple custom field with a linkit field widget. When adding a 3rd item, the uri parameter becomes an array. The updated patch fixes this but I don't know why the uri changes from a string to an array when adding 3 items to a field.

update: if I use the patch from https://www.drupal.org/project/custom_field/issues/3558244 then I only need my previous patch from #3.

apmsooner’s picture

Version: 4.0.1 » 4.0.x-dev
Status: Needs review » Fixed

I modified the patch slightly as the linkit attributes should have been merged in outside the other attributes as they are only needed for a linkit formatter that we don't even use but either way, the issue is fixed and merged into dev. Thanks for the report and followups.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

apmsooner’s picture

Status: Fixed » Closed (fixed)