Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 313815

Event dispatcher to pass block settings to a view

$
0
0

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, 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

TBD

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.


Viewing all articles
Browse latest Browse all 313815

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>