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.
Comments
Comment #2
apmsooner commentedHi, please try the patch in this issue as i think it might already be resolved: https://www.drupal.org/project/custom_field/issues/3558244
Comment #3
harrrrrrr commentedComment #4
harrrrrrr commentedThe 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).
Comment #5
apmsooner commentedOkay, thanks. It's an easy fix so I will merge this in today when I get a chance to test myself.
Comment #6
harrrrrrr commentedThere 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.
Comment #8
apmsooner commentedI 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.
Comment #10
apmsooner commented