A notice stopping the "Remove" buttons to works when they is two of them inside the same entity

Notice: Undefined offset: -2 in /xxxxxxxxxxxx/web/modules/contrib/entity_browser/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php on line 817

This notice causes ajax to fail, resulting in the field being broken unless the visitor reloads the page.

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

steveoriol created an issue. See original summary.

steveoriol’s picture

StatusFileSize
new1.16 KB

Status: Needs review » Needs work

The last submitted patch, 2: avoid_notice_mulit_remove-3125117-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

niral098’s picture

Thanks @steveoriol. The above #2 patch works. But the patch has indentation errors. Adding a patch after solving those indentation errors.

niral098’s picture

StatusFileSize
new1.68 KB

Fixed coding standards.

pnagornyak’s picture

StatusFileSize
new1.04 KB
new1.63 KB

Previous patches breaks the code. It is showed by failed test. Uploading patch that should not break logic.

pnagornyak’s picture

Status: Needs work » Needs review
steveoriol’s picture

Ok, the patch #6 works for me ;-)

daggerhart’s picture

Title: Notice that stop the "Remove" buttons to works » Remove button in entity browser field widget doesn't work
Version: 8.x-2.4 » 8.x-2.5
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Related issues: +#2811747: Can't remove attached image

#6 works. RTBC

It's the same bug as reported here in the 1.x version #2811747: Can't remove attached image.

berdir’s picture

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

I have committed #3048530: EntityReferenceBrowserWidget::formElementEntities() incorrectly matches on wrong trigger element when not visible, I believe this is related if not a duplicate. Please retry with 8.x-2.x-dev and either close this or update the patch.

ahmed eldesoky’s picture

Version: 8.x-2.5 » 8.x-2.7
StatusFileSize
new1.13 KB

patch #6 fails to apply on version 2.7
This is a 2.7 compatible version of patch #6

cbfannin’s picture

#11 worked for me on v2.9

Added other related issues.

I would also add this is very strange. Before patching, our remove button worked fine on Pantheon Test environment. It only did not work on local, multidev, and dev environments. But after the patch, it now works on local, multidev, and dev.

cbfannin’s picture

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

In following up with the environments that the remove functionality works on vs those it does not work on, I realized it was because Devel is disabled on Test and Live but enabled on Dev and Local. As soon as I disabled Devel on my local and tried to remove the file from the field, the remove functionality worked.

This is without the patch being applied.

cbfannin’s picture

I did a clean install on simplytest.me with 8.x-2.9 and no longer seeing this issue.

My test steps were:
- Installed 8.x-2.9 (unpatched)
- Created a new Entity Browser adding the upload widget.
- Added an entity reference field to the page node type.
- Set the entity reference field's form display to Entity Browser.
- Created a new node.
- Selected an image with the new field.
- Saved the node.
- Edited the node.
- Clicked the "Removed" button.
Result: The image was removed with no reported errors.

Again this was a clean install with no patches.
If anyone wants to provide their test steps, I am more than happy to test again.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

Setting to needs work for steps to confirm what exactly causes this, also closed #3341527: Undefined array key on file remove as a duplicate.

Also should be a merge request now. Not against merging it as it shouldn't cause issues, but having steps to reproduce and preferably tests would make it easier to verify.

Berdir credited thatguy.

berdir’s picture

eduardo morales alberti’s picture

@berdir as said in the comment #3247212-6: Ajax error on delete item

We're seeing the same issue. It doesn't actually cause a problem unless PHP has the display_errors option switched on.

Here the problem is caused by having both an Entity Browser field and a core Media Library field on the same form. When trying to remove an item from the media widget, the Entity Browser code is getting confused because the button for core Media Library is also called remove_button

Attaching patch which checks the field name on the triggering element matches the expected one for this widget. This allows both the media library and entity browser to function correctly again here.

We are unsure how to cover it with tests, but the patch #3125117-11: Remove button in entity browser field widget doesn't work works for us.

eduardo morales alberti’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new192.67 KB

We test it on a simpletestme https://master-cg4lcdtc1ja3gfni2izgajxkko4dkwxp.tugboatqa.com/

Steps to reproduce:

  • We installed the drupal 10.3.9
  • Installed the entity browser and created a reference field to content with this widget
  • Installed media and media library
  • Add a media field with the widget media library to the content type
  • Created a content with a media and saved the content
  • Edit again the content and remove the media
  • Review the logs

It is tested on a clean environment so RTBC

Simpletestme error reproduction

Full message error:

Warning: Undefined array key -1 in Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget->formElementEntities() (line 779 of /var/lib/tugboat/stm/web/modules/contrib/entity_browser/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php)
#0 /var/lib/tugboat/stm/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real(2, 'Undefined array...', '/var/lib/tugboa...', 779)
#1 /var/lib/tugboat/stm/web/modules/contrib/entity_browser/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php(779): _drupal_error_handler(2, 'Undefined array...', '/var/lib/tugboa...', 779)
#2 /var/lib/tugboat/stm/web/modules/contrib/entity_browser/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php(338): Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget->formElementEntities(Object(Drupal\Core\Field\EntityReferenceFieldItemList), Array, Object(Drupal\Core\Form\FormState))
#3 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Field/WidgetBase.php(459): Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget->formElement(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#4 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Field/WidgetBase.php(101): Drupal\Core\Field\WidgetBase->formSingleElement(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#5 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(190): Drupal\Core\Field\WidgetBase->form(Object(Drupal\Core\Field\EntityReferenceFieldItemList), Array, Object(Drupal\Core\Form\FormState))
#6 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(121): Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object(Drupal\node\Entity\Node), Array, Object(Drupal\Core\Form\FormState))
#7 /var/lib/tugboat/stm/web/core/modules/node/src/NodeForm.php(134): Drupal\Core\Entity\ContentEntityForm->form(Array, Object(Drupal\Core\Form\FormState))
#8 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Entity/EntityForm.php(107): Drupal\node\NodeForm->form(Array, Object(Drupal\Core\Form\FormState))
#9 [internal function]: Drupal\Core\Entity\EntityForm->buildForm(Array, Object(Drupal\Core\Form\FormState))
#10 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(536): call_user_func_array(Array, Array)
#11 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(375): Drupal\Core\Form\FormBuilder->retrieveForm('node_page_edit_...', Object(Drupal\Core\Form\FormState))
#12 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(633): Drupal\Core\Form\FormBuilder->rebuildForm('node_page_edit_...', Object(Drupal\Core\Form\FormState), Array)
#13 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm('node_page_edit_...', Array, Object(Drupal\Core\Form\FormState))
#14 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\node\NodeForm), Object(Drupal\Core\Form\FormState))
#15 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#16 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#17 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#18 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#19 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#20 /var/lib/tugboat/stm/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#21 /var/lib/tugboat/stm/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#22 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/lib/tugboat/stm/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/lib/tugboat/stm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /var/lib/tugboat/stm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#33 /var/lib/tugboat/stm/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#34 {main}

@berdir do you need more info?

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

andyf’s picture

I was also seeing this error on a node edit form with both a media browser and entity browser. The patch from #11 fixed it. I've moved it to an MR, still could do with tests.

andyf’s picture

Like #14 I'm finding it only actually breaks when devel's enabled; otherwise it generates the same warning but things carry on functioning.

And in case it's not clear, it's the media library widget that stops working for me (ie you can't remove the image), not the entity browser.

For me, with devel enabled and Error handlers set to None the PHP warning get prepended to the JSON responses preventing them from being parsed, it seems to be fixed with https://gitlab.com/drupalspoons/devel/-/merge_requests/149.

ahmed eldesoky’s picture

ahmed eldesoky’s picture

  • berdir committed 62680a16 on 8.x-2.x authored by andyf
    Issue #3125117 by andyf, niral098, pnagornyak, steveoriol, berdir, ahmed...
berdir’s picture

Version: 8.x-2.9 » 8.x-2.x-dev
Status: Reviewed & tested by the community » Fixed

Merged.

  • berdir committed 411633f0 on 8.x-2.x
    Issue #3125117: by berdir: Remove invalid config dependency
    

Status: Fixed » Closed (fixed)

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