Problem/Motivation
Drupal core adds the js-view-dom-id-XXXXXXX CSS class in its views-view.html.twig template for Ajax-enabled views. Currently this step is missing for views using display_builder_views for displaying.
Steps to reproduce
- Create an Ajax-enabled view and display it via
display_builder_viewsmodule's display extender - implying that the used component uses{{ attributes }}output for its wrapper HTML attributes - Check frontend output -> no
js-view-dom-id-XXXXXXXCSS class available on the component used for displaying the view and thus Ajax functionality is not working
My current workaround is a custom preprocess hook to have the DOM ID class available for Ajax:
namespace Drupal\MODULE_NAME\Hook;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Hook\Order\OrderAfter;
/**
* Implements hook_theme().
*/
class PreprocessViewsViewHook {
/**
* Implements hook_preprocess_HOOK().
*/
#[Hook('preprocess_views_view',
order: new OrderAfter(
modules: [
'display_builder_views',
],
)
)]
public function preprocess(array &$variables): void {
// Ensure views DOM ID for Ajax usage.
$dom_id = $variables['dom_id'] ?? NULL;
if (isset($dom_id) && isset($variables['content'][0])) {
$variables['content'][0]['#attributes'] = $variables['content'][0]['#attributes'] ?? [];
$variables['content'][0]['#attributes']['class'] = $variables['content'][0]['#attributes']['class'] ?? [];
$variables['content'][0]['#attributes']['class'][] = 'js-view-dom-id-' . $dom_id;
}
}
}
Proposed resolution
- Extend
Drupal\display_builder_views\Hook\PreprocessViewsView::preprocessViewsView()to add thejs-view-dom-id-XXXXXXXCSS class to the display builder output - Check if there are more attributes that may be passed to the display builder output - e.g. more custom CSS classes set for a view in advanced settings
Remaining tasks
- Check if more views HTML attributes have to be preserved
- Create an issue fork and MR to fix the issue
User interface changes
n/a
API changes
n/a
Data model changes
n/a
Issue fork display_builder-3583128
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
hctomComment #3
hctomComment #6
mogtofu33 commentedThe fact that we ignore CSS class configuration from views was known but undocumented, so thanks for opening this issue which is a reminder of that.
Your code add the class for every source created inside content, but seems we need to wrap the content to have the required class.
So I will go for a wrapper, normally we do not want to force any class but it is a standard Drupal usage.
@pdureau, what do you think? should we apply only the
js-view-dom-idclass for Javascript Ajax views support, or is it ok to apply the default views class and the configuration one?Comment #7
pdureau commentedHello @mogtofu33,
Our strategy is not really to ignore CSS class configuration from views or any display, it is to ignore the "wrappers" like
views-view.html.twig,page.html.twig,node.html.twig,user.html.twig... Because we believe those wrappers are both "markup noise" going in the way of the design implementation, and a legacy Drupal mechanism we must stop relying on.It is already "documented" here: https://display-builder-b6bde3.pages.drupalcode.org/faq/ but I guess we can do better.
However, we can keep the attributes of those wrappers, as it was already suggested in #3534619: Support for legacy preprocesses, we can merge the attributes of the root level of the built display (straightforward if there is a single source, tricky if many) with the attributes provided by the Drupal display.
The current state of the MR is not OK because it doesn't merge the attributes but recreate a wrapper:
It would be better to merge the attributes with something like that (naive proposal, not the solution):
But once again, the tricky part is we don't know what is the first source (is it accepting attributes?) and how to manage a display with many sources at the root.
Comment #8
pdureau commentedA related issue which may mitigate the problem: #3591025: Add a ViewTemplateSource for slots
Comment #9
mogtofu33 commentedComment #10
mogtofu33 commentedComment #11
mogtofu33 commentedComment #12
mogtofu33 commentedComment #13
mogtofu33 commentedProbably better to resolve completely in #3591025: Add a ViewTemplateSource for slots
Comment #15
mogtofu33 commentedComment #16
mogtofu33 commentedNo more a duplicate, It is an open decision now to support or not views custom settings for class and ajax, and if not for now, state it clearly or give an alternative, like perhaps a custom component.
Comment #17
pdureau commentedI am reconsidering ma position shared in comment https://www.drupal.org/project/display_builder/issues/3583128#comment-16...
There are 2 kinds of wrappers:
views-view,user,node,field,block...): we want to avoid them as much and we are already doing thatSo, the MR proposal is not great but good enough and a pragmatic middle-ground:
Comment #18
pdureau commentedI have rebased the MR and check manually.
The View HTML classes are added as expected:
There is something else to check?
Comment #19
mogtofu33 commentedBack to this issue, seems we could avoid the php part and be in the template we already override to be closer to views in: views-view.html.twig
Pushed in the mr, wdyt?
Comment #20
pdureau commentedLet's try this.
Comment #22
mogtofu33 commented