Execute hook_block_view_alter when placing blocks with layout builder

Created on 24 June 2022, over 2 years ago
Updated 28 April 2023, over 1 year ago

Problem/Motivation

Hook block alter does not execute for blocks that have been placed in Layout builder which means layout builder displays blocks incorrectly without their alters from contrib.

Here's an example where layout builder not firing expected hooks has broken Easy Breadcrumb:

https://www.drupal.org/project/easy_breadcrumb/issues/3292422

Steps to reproduce

Install easy breadcrumb and place the block through layout builder. Then check the site for JSON LD

You can also replicate this by adding hook block alter to any block and then placing that block through layout builder.

Proposed resolution

Execute hook block alter for blocks placed in layout builder.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Layout builder 

Last updated 1 day ago

Created by

🇺🇸United States Greg Boggs Portland Oregon

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

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 Kingdom AndyF

    Thanks everyone for the work so far! I was going to take a look at the tests when I noticed something... the patch executes hook_block_view_alter() after the block has been built with \Drupal\Core\Block\BlockPluginInterface::build(), whereas \Drupal\block\BlockViewBuilder does the build in a pre-render, after the view alter. And thinking a bit further, any sites with an existing hook_block_view_alter() might be adding a #pre-render that expects #block to be set with the block config entity... and it could explode because we don't have one with LB.

    So I wonder if we can actually add this block.module hook to layout builder blocks without breaking BC?

    Thanks!

  • 🇫🇷France paul_leclerc Lausanne Switzerland

    I had the same issue #19 🐛 Execute hook_block_view_alter when placing blocks with layout builder Needs work is talking about.
    This patch fixed it. #14 🐛 Execute hook_block_view_alter when placing blocks with layout builder Needs work

    I now have the list of blocks of a layout builder page. in hook_block_view_alter and hook_block_view_BASE_BLOCK_ID_alter

    Thanks

  • 🇧🇪Belgium Tim Lammar

    I had the same issue.
    I wanted to hook in on layout builder rendering blocks.
    The patch was not yet a fix for me, but it might be edge case.
    Technically there is a small issue with this patch.
    We are launching an alter hook on block_view, but it is launched during the build of the block.
    In most cases, that nuance won't make a difference, but in my case it does as our blocks are loading with lazy loading.
    In short that makes it go like this
    1. Page load
    2. hook_block_build to set placeholders for the blocks.
    3. Response
    4. Ajax calls to set to contents per block
    5. hook_block_view to fill the contents
    6. Response per block

    If you aren't using lazy_builder, it doens't matter whether you hook into block_build or block_view.
    But to be correct, it has to be block_build as we are in the build stage of layoutbuilder:

    /**
       * Builds render arrays for block plugins and sets it on the event.
       *
       * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
       *   The section component render event.
       */
      public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    

    Hereby a patch that launches block_build_alter instead of block_view_alter.
    With this you can make alterations to the page, based on a block, before the page is loaded. (in our case, a page redirect)

  • last update about 1 year ago
    Patch Failed to Apply
  • 🇮🇳India _utsavsharma

    As previous patch failed, here is patch for 11.

  • last update about 1 year ago
    30,605 pass
  • 🇧🇪Belgium Tim Lammar

    Ah yes apologies, my patch is written in D10.

  • 🇮🇳India Akhil Babu Chengannur

    Tested #28 in drupal 10.1.6 and both hook_block_build_alter & hook_block_build_BASE_BLOCK_ID_alter gets called after applying the patch.

    Testing steps

    1. Enable layout builder module and use it for Basic page contenty type. Enable 'Allow each content item to have its layout customized.'
    2. Create new block content of type "Basic block". Give title and ddescription , the save
    3. Implement hook_block_build_alter and hook_block_build_BASE_BLOCK_ID_alter to change the content of previous block ($build['content']['body'][0]['#text'] = 'Nex text')
    4. Create a Basic page content->Edit the layout and place the block.
    5. Content of the block is not updated as hooks are not fired.
    6. Now apply the patch #28 ->clear cache and check the previously created basic page node.
    7. Block content has updated as hooks were fired


    +1RTBC

  • Status changed to RTBC about 1 year ago
  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • 🇮🇳India Akhil Babu Chengannur

    Akhil Babu changed the visibility of the branch 3292520-execute-hookblockviewalter-when to hidden.

  • 🇮🇳India Akhil Babu Chengannur

    Opened the patch in #18 as merge request as per #32

  • Status changed to Needs review about 1 year ago
  • Pipeline finished with Failed
    about 1 year ago
    Total: 649s
    #60513
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    This was tagged for tests which are still needed. Before moving to review please check the tags

  • 🇧🇪Belgium waropd

    Updated patch #28 to work with Drupal 10.3

  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 143s
    #259718
  • 🇺🇦Ukraine piratskul

    Here's patch for version 10.3.0 with same changes as in #40 MR for version 11.x

  • Merge request !9291Drupal 11.x re-rolled and Test cases → (Open) created by arunkumark
  • Pipeline finished with Failed
    4 months ago
    Total: 122s
    #261089
  • Pipeline finished with Failed
    4 months ago
    Total: 173s
    #261095
  • 🇮🇳India arunkumark Coimbatore

    Changing the version back to 11.x, The issue needs to be fixed for Drupal Core 11.x version. We can back-merge the patch for 10.3.x version. Updated PR with test cases.

  • Status changed to Needs review 4 months ago
  • 🇮🇳India arunkumark Coimbatore
  • Pipeline finished with Failed
    4 months ago
    Total: 849s
    #261158
  • 🇮🇳India arunkumark Coimbatore

    arunkumark changed the visibility of the branch 3292520-handle-block_build-and-block_view-alter-hooks to hidden.

  • 🇮🇳India arunkumark Coimbatore

    arunkumark changed the visibility of the branch 3292520-execute-hookblockviewalter-when to hidden.

  • Status changed to Needs work 4 months ago
  • 🇺🇸United States smustgrave

    Left a comment on the MR.

  • Assigned to shalini_jha
  • 🇮🇳India shalini_jha

    It seems the "Test-only" test is failing when it shouldn’t.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 148s
    #333223
  • Pipeline finished with Failed
    about 1 month ago
    Total: 675s
    #335395
  • Pipeline finished with Failed
    about 1 month ago
    Total: 406s
    #337529
  • Pipeline finished with Failed
    about 1 month ago
    Total: 593s
    #337558
  • Pipeline finished with Failed
    about 1 month ago
    Total: 636s
    #338274
  • Pipeline finished with Failed
    about 1 month ago
    Total: 1327s
    #338425
  • Pipeline finished with Failed
    about 1 month ago
    Total: 523s
    #338545
  • 🇮🇳India arunkumark Coimbatore

    Resolved most of the Test case issues, few pipeline issues are unable to fix.

Production build 0.71.5 2024