When testing https://www.drupal.org/project/facets/issues/3052574 I discovered that block attributes and contextual links are reset when using twig tweak.

Comments

aspilicious created an issue. See original summary.

aspilicious’s picture

StatusFileSize
new796 bytes
chi’s picture

Status: Active » Postponed (maintainer needs more info)

This needs steps to reproduce.

chi’s picture

aspilicious’s picture

Status: Postponed (maintainer needs more info) » Needs review

See linked issue

1) Install twig tweak
2) Create a search view (ajax)
3) Create a facet linked to the view
4) Create a custom block with a twig template
5) Add following template

{{ drupal_block('facet_block:facet_id') }}
{{ drupal_view('view_name', 'block') }}

chi’s picture

Contextual links for embed views are not supported. See the issue referenced in #4.

aspilicious’s picture

? I don't understand your comment.

I can remove the contextual link part from the patch if you want.

chi’s picture

I think the patch needs a test that proves the bug.

aspilicious’s picture

Fair, but with 3 little kids at home I have little time at the moment to write the test.
So if someone want to help this issue forward, I think looking at the current test the easiest approach is

1) Create a hook_block_view_alter in the test module
2) Add a random class in #attributes
3) Add an extra xpath check in the functional test file to verify it's available

mikechr’s picture

I was having the same issue and tracked down the problem to
twig_tweak/src/TwigExtension::drupalBlock
As seen below the attributes and contextual_links of the block are removed. The patch #2 provided fixed the problem.

$build = [
          '#theme' => 'block',
          '#attributes' => [],
          '#contextual_links' => [],
          '#configuration' => $block_plugin->getConfiguration(),
          '#plugin_id' => $block_plugin->getPluginId(),
          '#base_plugin_id' => $block_plugin->getBaseId(),
          '#derivative_plugin_id' => $block_plugin->getDerivativeId(),
          'content' => $content,
];
chi’s picture

Status: Needs review » Needs work

I've just found a way to reproduce it without contributed modules. The core Language block plugin does set attributes. However simple coping them to the build array may produces duplicated attributes in an inner element. I think the proper way to handle this is moving attributes and contextual links from $build['content'] to $build. That's what Drupal core does.
https://git.drupalcode.org/project/drupal/-/blob/9.0.0-beta2/core/module...

  • Chi committed 30472b7 on 3.x authored by aspilicious
    Issue #3068078 by aspilicious: Block attributes and contextual links are...
chi’s picture

Version: 8.x-2.x-dev » 3.x-dev
Status: Needs work » Fixed

Fixed in 3.x. I don't think that can be backported to 2.x as moving attributes may break CSS selectors on existing sites which is a BC break.

Thank you.

Status: Fixed » Closed (fixed)

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

eliaspapa’s picture

I believe this needs to be re-opened. The patch will only work up to version 2.6 and not up to 2.9 which is the latest version. I think some minor modifications need to be done to the patch, maybe the file changed a bit in those versions. If this has been indeed fixed in version 3.x then all we need is this minor change to this patch.

chi’s picture

@EliasPapa that can cause BC break. See note #13.

sany_86@bk.ru’s picture

About contextual links, I was able to do it in this way for Block Content in node template:

        <div data-quickedit-entity-id="block_content/1008" class="contextual-region" data-once="quickedit" data-quickedit-entity-instance-id="0">
          {{ drupal_contextual_links('block_content:block_content=1008:langcode:' ~ langcode) }}
          {{ drupal_entity('block_content', 1008) }}
        </div>

Maybe it will be helpful.

ludo.r’s picture

Patch re-rolled against 8.x-2.x-dev