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_views module's display extender - implying that the used component uses {{ attributes }} output for its wrapper HTML attributes
  • Check frontend output -> no js-view-dom-id-XXXXXXX CSS 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 the js-view-dom-id-XXXXXXX CSS 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

Command icon 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

hctom created an issue. See original summary.

hctom’s picture

Issue summary: View changes
hctom’s picture

Issue summary: View changes

mogtofu33 made their first commit to this issue’s fork.

mogtofu33’s picture

Assigned: Unassigned » pdureau
Status: Active » Needs review

The 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-id class for Javascript Ajax views support, or is it ok to apply the default views class and the configuration one?

pdureau’s picture

Assigned: pdureau » Unassigned
Status: Needs review » Needs work
Issue tags: +display_builder-1.0.0-beta

Hello @mogtofu33,

@pdureau, what do you think? should we apply only the js-view-dom-id class for Javascript Ajax views support, or is it ok to apply the default views class and the configuration one?

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:

 $variables['content'] =[
  '#type' => 'html_tag',
  '#tag' => 'div',
   '#attributes' => [
    'class' => [ ... the wrappers ],
  ],
] + $variables['content'];

It would be better to merge the attributes with something like that (naive proposal, not the solution):

$variables['content'][0]['#attributes'] += [
  'class' => [
     'view',
     Html::getClass('view-' . $view->id()),
     Html::getClass('view-display-id-' . $variables['display_id']),
  ],
];

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.

pdureau’s picture

A related issue which may mitigate the problem: #3591025: Add a ViewTemplateSource for slots

mogtofu33’s picture

Assigned: Unassigned » mogtofu33
mogtofu33’s picture

mogtofu33’s picture

mogtofu33’s picture

mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Needs work » Closed (duplicate)
Issue tags: -display_builder-1.0.0-beta7

Probably better to resolve completely in #3591025: Add a ViewTemplateSource for slots

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mogtofu33’s picture

Parent issue: #3613194: [meta] Next beta »
mogtofu33’s picture

Status: Closed (duplicate) » Active
Issue tags: +display_builder-1.0.0-beta8

No 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.

pdureau’s picture

I am reconsidering ma position shared in comment https://www.drupal.org/project/display_builder/issues/3583128#comment-16...

There are 2 kinds of wrappers:

  • the ones with theme hooks, templates, and all the ThemeManager mess (views-view, user, node, field, block...): we want to avoid them as much and we are already doing that
  • the ones like the proposition in the MR, where is is simply a render element were we apply classes. Of course, it would better to avoid a render tree layer, but it is not the same deal

So, the MR proposal is not great but good enough and a pragmatic middle-ground:

 $variables['content'] =[
  '#type' => 'html_tag',
  '#tag' => 'div',
   '#attributes' => [
    'class' => [ ... the wrappers ],
  ],
] + $variables['content'];
pdureau’s picture

Assigned: Unassigned » mogtofu33
Status: Active » Needs review

I have rebased the MR and check manually.

The View HTML classes are added as expected:

<div class="view view-articles view-display-id-page-1 js-view-dom-id-5eae0d1409b4ea6098d4b8a6971e48da238e80d213f832ea8f2bafe217e24781"
<div data-component-id="ui_suite_daisyui:card" class="card">
      <div class="card-body">
...

There is something else to check?

mogtofu33’s picture

Assigned: mogtofu33 » pdureau

Back 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?

pdureau’s picture

Assigned: pdureau » mogtofu33
Status: Needs review » Reviewed & tested by the community

Let's try this.

  • mogtofu33 committed 655c27d4 on 1.0.x
    fix: #3583128 Views DOM ID class is missing for Ajax-enabled views
    
    By:...
mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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