Support #attached settings in ViewAjaxController

Created on 18 April 2024, 6 months ago
Updated 19 May 2024, 5 months ago

Problem/Motivation

It is not possible for anything within the render array of the view (eg from a Handler render method) to use #attached settings. They are only set on initial load of the view.

Steps to reproduce

Attempt to add ['#attached']['drupalSettings'] to any render.

Proposed resolution

Check the rendered view for ['#attached']['drupalSettings'] and add an ajax SettingsCommand if found.

Remaining tasks

MR + Tests coming.

User interface changes

None

API changes

N/A

Data model changes

N/A

Release notes snippet

Views Ajax Controller now checks for Drupal Settings changes in the render of the View.

πŸ› Bug report
Status

Fixed

Version

10.3 ✨

Component
ViewsΒ  β†’

Last updated about 2 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom scott_euser

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

Merge Requests

Comments & Activities

  • Issue created by @scott_euser
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Merge request created + updated tests. Rather than create a new method in ViewAjaxControllerTest, I opted to add to ::testAjaxView() to keep it faster as I would need to repeat a chunk of what is in that, but happy to move this to a separate method if that is the preference.

  • Pipeline finished with Failed
    6 months ago
    Total: 1094s
    #150498
  • Status changed to RTBC 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for including tests from the start

    1) Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::testAjaxView
    Undefined array key 2
    /builds/issue/drupal-3441920/core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php:216
    /builds/issue/drupal-3441920/vendor/phpunit/phpunit/src/Framework/TestResult.php:729
    /builds/issue/drupal-3441920/vendor/phpunit/phpunit/src/Framework/TestSuite.php:685
    /builds/issue/drupal-3441920/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
    /builds/issue/drupal-3441920/vendor/phpunit/phpunit/src/TextUI/Command.php:146
    /builds/issue/drupal-3441920/vendor/phpunit/phpunit/src/TextUI/Command.php:99
    ERRORS!
    Tests: 10, Assertions: 57, Errors: 1.
    

    Issue summary appears complete.

    Extra condition appears fine to me.

  • Status changed to Needs work 6 months ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    "Views Ajax Controller" This should be the name of the class, ViewAjaxController as well as describe the fix, not the problem.

  • Status changed to Needs review 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Thanks, updated the title.

  • Status changed to RTBC 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Title seems fine.

  • Status changed to Needs work 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    I think there might be a more generic way to fix this - left a long-ish comment on the MR.

  • Pipeline finished with Failed
    6 months ago
    Total: 178s
    #156610
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Thanks for the tip!

    The code change itself should work in reality, but the test as is is definitely going to fail. I would appreciate if you have advice on this one:

    1. UnitTest: Mocking `\Drupal\core\Ajax\AjaxResponseAttachmentsProcessor` seems like a big rabbit hole, each time I think I've mocked everything, there is another service called somewhere in the pipeline that further needs to be mocked.
    2. KernalTest: If I switch to a KernalTest it would be easier, but a lot of the heavy lifting this ViewAjaxControllerTest would be repeated
    3. FunctionalJavascript: Or I can go for a FunctionalJavascript test and test it fully without any mocking and actually check that the response contains the correct thing (though I will probably need to add a test module to add #attached into a Handler).
  • πŸ‡¬πŸ‡§United Kingdom catch

    Replied on the MR since I am hopeful the test complexity stems from a possibly unnecessary line.

  • Assigned to scott_euser
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Thanks! Yes you are right, in a real site it works with just setAttachments. So probably I need to do some tweaking with the mocking to make sure that AjaxResponseAttachmentsProcessor is actually running as well. Will work on the test a bit more.

  • Pipeline finished with Failed
    6 months ago
    Total: 992s
    #156803
  • πŸ‡¬πŸ‡§United Kingdom catch

    The test could maybe just call ::getAttachments() on the response, to make sure the attachments are set?

    Running the commands ought to be handled by AJAX system tests themselves.

  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Right of course! Much simpler, and of course AjaxResponseSubscriber is tested elsewhere already so that does not need to be covered here. Tests passing now in ddev. Thank you very much for your help here.

  • Pipeline finished with Canceled
    6 months ago
    Total: 264s
    #158061
  • Pipeline finished with Success
    6 months ago
    #158064
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Okay tests passing now, thanks!

  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    To note, I did consider adding an attached libraries as well to the ::setupValidMocks() method, but I tested with my initial commit that only covered SettingsCommand rather than attachments and the tests pass anyways for the library, so I do not think it adds anything (whereas as is, test only fails as expected and with the change test passes).

  • Status changed to RTBC 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    Looks great. Ran the test only job too just to see the failures.

  • πŸ‡«πŸ‡·France nod_ Lille

    I was worried having drupalSetting added to the preview could break views ui somehow. Haven't found an explicit way it could yet.

    Not really my area of the code so leaving the commit to someone else. RTBC +1

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Committed and pushed 3e486f783a to 11.x and 4d0abee5f2 to 11.0.x and af7f5ed2e1 to 10.4.x and 12097985d8 to 10.3.x. Thanks!

    • alexpott β†’ committed 12097985 on 10.3.x
      Issue #3441920 by scott_euser, catch, smustgrave, quietone: Support #...
    • alexpott β†’ committed af7f5ed2 on 10.4.x
      Issue #3441920 by scott_euser, catch, smustgrave, quietone: Support #...
    • alexpott β†’ committed 4d0abee5 on 11.0.x
      Issue #3441920 by scott_euser, catch, smustgrave, quietone: Support #...
  • Status changed to Fixed 6 months ago
    • alexpott β†’ committed 3e486f78 on 11.x
      Issue #3441920 by scott_euser, catch, smustgrave, quietone: Support #...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024