Ajax not working when using non-default view mode.

Created on 24 July 2017, over 7 years ago
Updated 6 August 2024, 5 months ago

If parent entity is rendered using non-default (not "full") view mode, ajax comment form is never updated.

On ajax_comments_entity_display_build_alter cache is set like that:

Utility::setEntityRenderArray($build, $context['entity'], $context['display']->getMode());

where $context['display']->getMode() is a current view mode, which can be anything, if you render node, for example, like

$build = $view_builder->view($node, "my_custom_view_mode");

On getWrapperIdFromEntity we're getting cache like that:

$render_array = static::getEntityRenderArray($commented_entity, 'full');

And inside of getEntityRenderArray we have this for view modes:

$modes = [
  $view_mode,
  '_custom',
  'default',
];

Where $view_mode argument is always "full". That means that if you use non-full view mode for parent entity, cache key will be different on cache set and get, since cache key is based on view mode. Because of that hidden 'wrapper_html_id' form field gets different "#id" property from actual <section> id and comment block is never updated on ajax response.

Not sure about what approach should be used here, will try to provide some patch later.

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇮🇪Ireland ArchieV

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States devitate Orange County, CA

    💩, it needs a beta5 patch now.

  • 🇷🇴Romania adevms

    Attaching patch for beta5

  • 🇩🇪Germany Anybody Porta Westfalica

    Could we please have MR for this and perhaps someone could contact an active maintainer to have this fixed finally? Looks like a lot of people run into this trouble? :)

    Thanks!

  • 🇩🇪Germany Anybody Porta Westfalica

    As of #61

  • Issue was unassigned.
  • 🇳🇱Netherlands idebr

    Approach looks good, but this issue will need some test coverage before commit.

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    2 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    2 pass
  • @parisek opened merge request.
  • 🇨🇿Czech Republic parisek

    Created MR and hid patches.

  • Status changed to Needs review about 1 year ago
  • 🇸🇰Slovakia coaston

    Thank you parisek, your MR works as expected.

    Now It works for a different view mode for me.

  • 🇸🇰Slovakia coaston

    I am not sure if it is related. But Edit button now does not work. I am using modal window and using different view mode to display. Everything works fine after i applied that MR, however edit button does not work - whole screen become greyed out. Once I click ESC and open dialog window again + edit button - it works fine.
    But every time refresh of page needs to be done.

  • 🇸🇰Slovakia coaston

    #71 - it does not work also for the normal view, so it is not related to this issue. Hence I created a new one here.

    3394119 🐛 Edit button in Dialog window Active

  • 🇩🇪Germany Anybody Porta Westfalica

    @parisek, @coaston still no test coverage, sadly. It's needed to get this issue fixed finally.

  • 🇸🇰Slovakia coaston

    Anybody, do you think it is possible to disable ajax for operation actions like edit/delete (or simply add target "_blank" to this links ?)
    This would solve the issue for dialog.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 12 months ago
    2 pass
  • 🇺🇸United States pvalappil

    Updating #60 to convert display option to empty array if it is null

  • 🇫🇷France PhilY 🇪🇺🇫🇷 Paris, France

    Patch #75 works for me using Drupal 10.1.7 + CKEditor 5 on a full display mode.

  • 🇬🇧United Kingdom globexplorer

    The problem with the above patches is they assume every entity is fieldable, but if not it throws WSOD!

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    2 pass
  • 🇺🇸United States SocialNicheGuru

    @globexplorer, please remove changes to ajax_comments.info.yml and could you provide an interdiff?

  • Status changed to Needs work 10 months ago
  • Status changed to Needs review 9 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Could we perhaps switch to a MR which is easier to review and "interdiffed automatically"?

  • Status changed to Needs work 9 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Still this has no tests to ensure it doesn't break again in the future.

  • 🇺🇸United States erutan

    I still need to refresh the page when adding in a comment via a field in views. Drupal 10.3.1 and beta5 of this module. Tried with a bootstrap 5 subtheme and with Olivero.

  • #81 didn't work.
    By the way, Ajax comments module breaks other modules working like eca, message subscription etc. after Drupal 10.3
    When I uninstall ajax comments these modules work fine. I'm not sure what the main problem is.

  • 🇺🇸United States erutan

    @pearls there's some other needs review patches here that seem to help with containing the scope / not having conflicts.

    I'm currently running the following in my composer.json and haven't noticed any issues with related modules. That said it's of minimal use when trying to have comments in a view all it does is stop the redirect to the entity edit page on save (which is great).

    "drupal/ajax_comments": {
                    "fix ajax on non default views": "https://www.drupal.org/files/issues/2024-03-18/ajax_comments-ajax_non_default_view_mode-2896916-beta5-81.patch",
                    "remove deprecated library": "https://www.drupal.org/files/issues/2023-01-20/ajax_comments-jquery-form-3335188-2.patch",
                    "fix non-unique IDs": "https://www.drupal.org/files/issues/2021-04-09/3208008-2.patch"
                },
  • @erutan, I tried it with the above mentioned patches as per your recommendation.
    Unfortunately, it doesn't work unless I remove the message subscribe example module.I still need to refresh the page.
    Before Drupal 10.3 they worked together without any problems.

    There is an ECA issue with having ajax comments enabled for anonymous users when viewing a node.That's why I had to remove the ajax comment module again.
    https://www.drupal.org/project/eca/issues/3462785 🐛 TypeError: eca content entity view mode alter Active

  • 🇺🇸United States erutan

    I'm not using ECA for anything involving anonymous users, but good to know about that issue.

    I've disabled the module myself and am just going without comments on views until things clear up.

  • Status changed to Needs review 5 months ago
  • Status changed to Needs work 5 months ago
  • 🇳🇱Netherlands idebr

    The TempStore implementation of "view mode" is problematic: it is saved per entity type, but fields may be configured differently per bundle / field.

    The approach in Don't rebuild the entire comment field/thread when posting a comment Needs work is to add / update / delete only the changed parts of the comment thread using deterministic HTML ids similar to how the module worked in D7. This approach should make the module a lot less complicated and allow it to work for different view modes and in Views listings.

    Feel free to continue this issue, but it is unlikely to be committed with the current approach.

  • MR 11 patch does not apply.

    https://git.drupalcode.org/project/ajax_comments/-/merge_requests/11.patch

    Checking patch ajax_comments.module...
    error: while searching for:
    use Drupal\Core\Url;
    use Drupal\Core\Routing\RouteMatch;
    
    /**
     * Implements hook_comment_links_alter().
     *
    
    error: patch failed: ajax_comments.module:21
    error: ajax_comments.module: patch does not apply
    Checking patch src/Controller/AjaxCommentsController.php...
    Checking patch src/Form/AjaxCommentsForm.php...
    error: while searching for:
        parent::save($form, $form_state);
        /** @var \Drupal\comment\CommentInterface $comment */
        $comment = $form_state->getFormObject()->getEntity();
        $comment_formatter = $this->fieldSettingsHelper->getFieldFormatterFromComment($comment, 'full');
        if (empty($comment_formatter) || !$this->fieldSettingsHelper->isEnabled($comment_formatter)) {
          // If not using Ajax Comments, do not change the redirect.
          return;
    
    error: patch failed: src/Form/AjaxCommentsForm.php:411
    error: src/Form/AjaxCommentsForm.php: patch does not apply
    Checking patch src/TempStore.php...
    Checking patch src/Utility.php...
    Checking patch src/Controller/AjaxCommentsController.php...
    error: while searching for:
          ->getStorage('entity_view_display')
          ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.default')
          ->getComponent($field_name);
        $comment_display = $comment_field->view($display_options);
    
        // To avoid infinite nesting of #theme_wrappers elements on subsequent
        // ajax responses, unset them here.
    
    error: patch failed: src/Controller/AjaxCommentsController.php:160
    error: src/Controller/AjaxCommentsController.php: patch does not apply
    Checking patch src/Controller/AjaxCommentsController.php...
    Checking patch src/Form/AjaxCommentsForm.php...
    error: while searching for:
    
        /** @var \Drupal\comment\CommentInterface $comment */
        $comment = $form_state->getFormObject()->getEntity();
        /** @var \Drupal\ajax_comments\TempStore $tempStore */
        $tempStore = \Drupal::service('ajax_comments.temp_store');
        $view_mode = $tempStore->getViewMode($comment->getCommentedEntity()->getEntityType()->getLabel()->getUntranslatedString());
        // Check to see if this comment field uses ajax comments.
        $comment_formatter = $this->fieldSettingsHelper->getFieldFormatterFromComment($comment, $view_mode);
        if (empty($comment_formatter) || !$this->fieldSettingsHelper->isEnabled($comment_formatter)) {
    
    error: patch failed: src/Form/AjaxCommentsForm.php:134
    error: src/Form/AjaxCommentsForm.php: patch does not apply
    Checking patch src/TempStore.php...
    Checking patch src/TempStore.php...
    Checking patch ajax_comments.module...
    error: while searching for:
     * AJAX comments module file.
     */
    
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\ajax_comments\Controller\AjaxCommentsController;
    use Drupal\ajax_comments\Utility;
    use Drupal\comment\CommentInterface;
    
    error: patch failed: ajax_comments.module:5
    error: ajax_comments.module: patch does not apply
    Checking patch src/TempStore.php...
    Checking patch tests/modules/ajax_comments_test_views/ajax_comments_test_views.info.yml...
    Checking patch tests/modules/ajax_comments_test_views/config/install/views.view.comments_on_teasers.yml...
    Checking patch tests/src/FunctionalJavascript/AjaxCommentsFunctionalTest.php...
    
  • MR11 applied with composer but the page still needs to be refreshed.It didn't worked for me.

Production build 0.71.5 2024