Append views display type to element name

Created on 21 April 2025, 27 days ago

Motivation

Citing myself in the parent issue where we introduce views block displays:

Then, we probably want to improve the custom-element tag to append the display id. But since this is a change for the page also, I think this should be its own change notice.

When you have one view, let's say called "articles", to provide at least one block and a page. Atm, both outputs would get the same custom-element name "drupal-view-articles".

Now I see those common use-cases:
#1 - Customize output of both block and page
This works well, a component drupal-view-articles would work for both.

#2 - Customize the output of only block or page, or both differently
That's doable with a component drupal-view-articles since "displayId" is given as an attribute, but it's a bit ugly when you have to do it with conditionals within the component.

Proposed resolution

Make the custom element name drupal-view-{VIEW}-{DISPLAY_TYPE} where the display-type is shortened to be either "page" or "block". So for use-case #1 you could still use a component drupal-view-articles, but for use-case #2 one can now use components drupal-view-articles-page and drupal-view-articles-block also.

Not suggested alternative:
We could also do drupal-view-{VIEW}-{DISPLAY_ID} but that would result in rather long element-names like "drupal-view-articles-custom-elements-block-1" what is a bit ugly + I'd think the 90% case is that you want to theme all the blocks or pages within a view, so it is good enough. If you still need to differentiate by display (e.g. block1 vs block2) within the same view, that's still doable with conditionals on display-ID once 🐛 Views display plugin ID is output instead of display ID Active is fixed.

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇦🇹Austria fago Vienna

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @fago
  • Merge request !124Resolve #3520384 "Append views display" → (Merged) created by fago
  • 🇦🇹Austria fago Vienna

    implemented the proposed solution -> added MR and a draft change notice

  • 🇸🇮Slovenia useernamee Ljubljana

    I checked the code and I'm wondering whether there is no nicer way to get the plugin type than substracting the prefix from the plugin id

        // Remove "custom_elements_" prefix from display type.
        $display_type = strpos($view->getDisplay()
           ->getPluginId(), 'custom_elements_') === 0 ?
              substr($view->getDisplay()->getPluginId(), 16) :
              $view->getDisplay()->getPluginId();
    
  • 🇦🇹Austria fago Vienna

    Well, this "plugin type" is not an existing concept, this is just how I called it. The plugin IDs are custom_elements_page and custom_elements_block respective. But since with custom elements for a drupal-view it's pretty clear you are using one of those, repeating the custom_elements prefix seems stupid and makes just long ugly names. That's why I'd think we better remove it.

    But given that, yeah, there is no better api to do that.

  • 🇸🇮Slovenia useernamee Ljubljana

    I approved the PR. Should we document ce name drupal-view-{VIEW}-{DISPLAY_TYPE} somewhere?

  • Pipeline finished with Skipped
    24 days ago
    #481312
    • fago committed 24af9187 on 1.x
      Issue #3520384 by fago: Append views display type to element name.
      
  • 🇦🇹Austria fago Vienna

    thx, merged then!

    There is a change-notice which documents the change. Not sure where we would it else?
    I guess we should update the documentation about views at lupus-decopled.org!

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

Production build 0.71.5 2024