Event dispatcher to pass block settings to a view

Created on 24 February 2021, almost 4 years ago
Updated 14 August 2024, 5 months ago

Problem/Motivation

We have a block that defines a "headline" field, which is a title that stores the value of the HTML element that should also be used to display it, similar to the Heading field module . The block also renders a view and displays it on the page. For each row of the view results, we would like to be able to set the title field to use the next HTML element down in the heading tag hierarchy. So, if the user selects h2 for the headline of the block, each result row's title field should display as an h3. When the page initially loads, we are able to accomplish this via HOOK_preprocess_block() by getting the $view and using $view->display_handler->setOption('heading_size') to set a value that we can access via the view template. However, when the view in question displays a pager and you click the pager to view the second set of results, HOOK_preprocess_block never runs because the block is not regenerated when a view is generated via AJAX request.

In our code, we are currently using a custom block that uses custom code to load and render the view, but this would also be an issue if we were using the built-in view block display. Using the ctools_views module as a guide, I have experimented with adding settings to the view display that can in turn be set by the user placing the block. Using Layout Builder, you might have the same block instance placed many times in one site. Therefore, it seems like a valid use case to be able to access block settings in the view should you want to have those settings affect the rendering. Another example of this would be setting Layout Builder Styles on the block instance and passing the results to its views to affect the display.

Proposed resolution

In addition to the solution being developed in this issue 🐛 Views Block Display skips preBlockBuild() call on ajax rebuild Needs work , I'm proposing to add an event dispatcher that is triggered during Drupal\views\Plugin\views\display\Block::preBlockBuild(). This would allow creating an event subscriber that can access block settings and pass them to the view. With the previously linked patch, this would also get triggered during views block display getting generated during an AJAX request.

Remaining tasks

Need to write the Test case for the ViewsBlock with event subscriber.

User interface changes

None, I think.

API changes

Not totally sure about this, but it seems like this wouldn't change any API's.

Data model changes

I don't think this would require any data model changes.

Feature request
Status

Needs work

Version

11.0 🔥

Component
Views 

Last updated 36 minutes ago

Created by

🇺🇸United States pyrello

Live updates comments and jobs are added and updated live.
  • 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.

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.

  • Open on Drupal.org →
    Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • @pyrello opened merge request.
  • 🇺🇸United States drupgirl

    After upgrading to core 10.3.1 this patch no longer applies, resulting in the following.

    The website encountered an unexpected error. Try again later.

    Error: Class "Drupal\views\Plugin\views\display\PreBuildBlockEvent" not found in Drupal\views\Plugin\views\display\Block->preBlockBuild() (line 407 of core/modules/views/src/Plugin/views/display/Block.php).
    Drupal\ctools_views\Plugin\Display\Block->preBlockBuild(Object) (Line: 27)
    Drupal\views\Plugin\Block\ViewsBlock->build() (Line: 106)
    Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender(Object, 'section_component.build.render_array', Object)
    call_user_func(Array, Object, 'section_component.build.render_array', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'section_component.build.render_array') (Line: 90)
    Drupal\layout_builder\SectionComponent->toRenderArray(Array, ) (Line: 88)
    Drupal\layout_builder\Section->toRenderArray(Array) (Line: 331)

  • First commit to issue fork.
  • 🇮🇳India arunkumark Coimbatore

    arunkumark changed the visibility of the branch 3200029-views-block-display-add-prebuild-event to hidden.

  • 🇮🇳India arunkumark Coimbatore

    arunkumark changed the visibility of the branch 11.x to hidden.

  • 🇮🇳India arunkumark Coimbatore

    arunkumark changed the visibility of the branch 3200029-views-block-display-prebuild-event to hidden.

  • 🇬🇧United Kingdom catch

    Restoring status. The patch not applying any more isn't a critical bug in itself. Are you sure your site actually needs the patch applied at all?

  • Pipeline finished with Failed
    5 months ago
    Total: 377s
    #247538
  • Status changed to Needs review 5 months ago
  • 🇮🇳India arunkumark Coimbatore
  • Pipeline finished with Failed
    5 months ago
    Total: 3387s
    #247851
  • Pipeline finished with Failed
    5 months ago
    Total: 2187s
    #247926
  • 🇺🇸United States drupgirl

    Thank you very much for you quick response. Yes the site does need this and your patch applied. The site pages that use this now load as expected.

    +1 RTBC

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

    Left comments on MR

    But also noticed it needs test coverage.

  • Pipeline finished with Failed
    5 months ago
    Total: 613s
    #253439
  • 🇮🇳India arunkumark Coimbatore
Production build 0.71.5 2024