Problem/Motivation

Since blocks can also be rendered from a twig template, we should be able to load the block using ajax by passing the settings to the drupal_block function.

Actually if I call a block passing the settings brought by the module, it doesn't load using ajax :

{{ drupal_block('my_block_id', { 'block_ajax': { 'is_ajax': 1, 'max_age': '0', 'show_spinner': 0, 'placeholder': '', 'ajax_defaults': { 'method': 'POST', 'timeout': '10000', 'others': { 'async': 'async', 'cache': 0 } } } }) }}

Comments

MacSim created an issue. See original summary.

geoanders’s picture

Version: 3.0.0 » 3.x-dev
Category: Support request » Feature request
geoanders’s picture

@MacSim

Looks like Twig Tweak doesn't call alter hooks which happen in the Ajax Block module. Seems like the module renders blocks differently.

See issue here: https://www.drupal.org/project/twig_tweak/issues/3215911

I have not tested out the patch, but seems like Ajax Block and Twig Tweak are currently not compatible right now.

geoanders’s picture

Status: Active » Postponed
Related issues: +#3215911: drupalBlock() should call hook_block_view_alter()

Postponing for now.

macsim’s picture

Anyway the AjaxBlockController is not handling plugin blocks.
If you call /block/ajax/user_login_form (or another plugin block id), the controller returns an empty response.
It might not be the main purpose of the module, but that could be useful if you want to load and display a block from a JS when a user triggers an event on an element.

The 'plugin.manager.block' service is injected but never used except in the constructor to instantiate $this->blockManager = $block_manager;

macsim’s picture

StatusFileSize
new3.81 KB

Just in case drupalBlock() would been able to call hook_block_view_alter() and in order to reply to #5 we might do something like what's provided in this patch.

macsim’s picture

StatusFileSize
new3.86 KB
geoanders’s picture

@MacSim

Thanks for the patch. Will adjust a bit, but falling back to the block plugin works for me. Have a few adjustments and cleanup to do in the controller anyways due to other tickets/issues. Will get this added in when I get some time.

geoanders’s picture

Status: Postponed » Needs review
geoanders’s picture

Title: Twig Tweak Support » Twig Tweak/Plugin Blocks support

  • geoanders committed 38e3222 on 8.x-2.x
    Issue #3315618, Issue ##3291413 - Add in support for block plugins and...

  • geoanders committed d3d590b on 3.x
    Issue #3315618, Issue ##3291413 - Add in support for block plugins and...

  • geoanders committed 02bc056 on 8.x-2.x
    Issue #3315618 - Fixes to make twig_tweak work with alter patch applied.
    

  • geoanders committed 227aa3f on 3.x
    Issue #3315618 - Fixes to make twig_tweak work with alter patch applied.
    
geoanders’s picture

@MacSim

Applying the twig_tweak patch (below) related to this issue and using the latest Ajax block 3.x dev branch, now allows the drupal_block twig function to work better with Ajax block.

Let me know if this works on your end.

Twig Tweak patch:
https://www.drupal.org/files/issues/2021-05-26/twig_tweak-block-view-alt...

macsim’s picture

Hey @geoanders
Sorry I've been sick last week.
It's good for me, I can load a block using the twig teak technique + the patch provided in #15.

Thus being said, if the block I load have a form in it with ajax behaviors on some form fields, those behaviors are broken.
The form validator is broken as well - it is triggered but the errors won't show up - I don't know if it is because of the ajax behavior on the submit button or not and I am not sure it would be related to the current issue.

geoanders’s picture

@MacSim

No worries and hope you feel better! Thanks for confirming.

I wonder if it's just a matter of attaching behaviours after AJAX call with the following code on success:

Drupal.attachBehaviors();
capellic’s picture

I've tested this patch and it works well. Thanks!

geoanders’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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