Issue with multiple buttons on a View

Created on 17 April 2024, 8 months ago
Updated 31 May 2024, 7 months ago

Firstly, thanks for a great little module - very effective!

I've added a Like button to 2 custom twig templates for my nodes - one template is for the full node display and the other is for a display type called Grid which is used to show the results in Views.

The button on individual nodes works perfectly but I'm having issues with the ones in the View.

The View displays multiple nodes of the same type. All nodes show the Like button as expected and the form ID (like-form-node-xxx) is different on each Like button too. Any content I've previously liked on individual node pages stays flagged as Liked on the View. All good so far.

The issue comes when I start trying to Like multiple items in the View. I click the Like button on a first unliked item in the View. When I click it, the Like highlights as expected and it saves fine too and can be seen in the list at /admin/content/like. If I now click another Like button on a different node in the same View though, that button initially turns to Liked but then immediately refreshes again and goes back to showing as Unliked. If I now refresh the page, both that AND the previous Like I clicked in the view are showing as unlike and the first Liked item has also disappeared from the admin list too.

I'm wondering if there's some sort of clash with having the multiple different Like forms on one page perhaps? It's as if clicking the second button actually acts as if the first button has been clicked again, causing it to become unlike again.

The View itself is fairly standard with the Show settings set to Content and then the Grid display type selected. I've tried it with Ajax on and off and the same issue occurs in both scenarios.

Hope this all makes sense. Unfortunately, the site I'm working on isn't public yet otherwise I'd share the link to see/test for yourself.

🐛 Bug report
Status

Active

Version

2.0

Component

User interface

Created by

🇬🇧United Kingdom ttesteve

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ttesteve
  • First commit to issue fork.
  • 🇫🇷France zewebmaster Nantes

    Hi @ttesteve,

    I face a similar problem when trying to use this module on a comment thread.

    In my case, it turns out that in the Ajax callback LikeForm::ajaxSubmit, the entity embedded in the form validated (by clicking on the like button) always corresponds to my commented entity and not to the liked comment entity.

    # function LikeForm::ajaxSubmit
    
    /** @var \Drupal\Core\FieldItemInterface $item */
    $item = $form['#item'];
    $entity = $item->getEntity();
    

    I've tried to replicate the problem you describe by creating a very simple paginated view that displays teasers - as I understand your problem. And in the same way, the problem is quite similar and the entity loaded in the Ajax callback corresponds to the first item in the list of the current page and not to the liked entity => which causes the side effects you describe.

    I've tried to correct the problem in several ways:

    * By passing the entity into a field of type value => This doesn't change anything!

    * By using fields of type hidden to retrieve the metadata of the liked entity and load it into the callback => this works, although I'm not sure it's the most appropriate way of doing it.

    I therefore propose this correction. Don't hesitate to let me know if it fixes the bug you're experiencing.

  • 🇫🇷France zewebmaster Nantes

    zewebmaster changed the visibility of the branch 2.x to hidden.

  • 🇬🇧United Kingdom ttesteve

    Hi @zewebmaster,

    I've just tested your fix on my site and it works perfectly now with multiple like buttons on the same page all now working as expected!

    Many thanks for the solution!

  • 🇪🇸Spain pcambra Asturies

    Maybe you two could share the fix in a MR? thanks :)

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.1 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 7 months ago
    6 pass
Production build 0.71.5 2024