Problem/Motivation

We want to enable people to nicely organize the frontend code. We aid that be providing well-defined naming pattern by default.

Proposed resolution

Improve defaults in custom-elements 3.x to follow the following pattern:

* For rendering content entities, we do not add a drupal- prefix. We apply the following pattern: `{ENTITY_TYPE}-{BUNDLE}-{VIEW_MODE}` or when the entity does not use bundles `{ENTITY_TYPE}-{VIEW_MODE}`. The view-mode suffix should be optional.
* For rendering any other special element like forms or views, we use a drupal- prefix.
* Additionally, we append form-IDs and view-names to those special blocks, to ease customizing them + improve the nuxt setup to make that easy.

This will result in the following custom-elements / vue components:
drupal-markup
drupal-layout
drupal-form-[...]
drupal-form-user-form
drupal-view-[...]
drupal-view-frontpage
node-[...]
node-article-full
node-article-teaser
node-page-full
node-page-teaser
node-page-search
media-[...]
media-image-thumbnail
paragraph-[...]
paragraph-text
paragraph-image
block-content-markup
block-content-custom
...

The [...] view component lookup logic is something we can work into our renderCustomElements() nuxt function and follows the known pattern of the nuxt page router where [...] acts as a fallback. It should take the full component name, and if no match is found, remove "-[a-z]+" suffixes until one is found. This gives the flexibility to have one component for all view modes or not.

There is some risk of conflict with this naming pattern, e.g. content types "article", "article_teaser" and view modes, "teaser", "teaser_full" - I think this is rather rare, so acceptable and can be worked around by renaming things or using a different pattern in conflicting cases. I don't think it's worth complicating the pattern to 100% avoid conflicts.

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

fago created an issue. See original summary.

fago’s picture

Issue summary: View changes
fago’s picture

Title: Consistingly prefix components with drupal- » Consistently prefix components with drupal-
turtlbbx’s picture

This would be a breaking change for existing Nuxt projects. One would have to move components in Nuxt projects in a sub-folder "Drupal" or write additional component config in nuxt config file or have prefixed component names with "Drupal". It adds another layer to think about when developing frontend, I don't like that.

What I'm doing right now in a Nuxt 3 project is, I put all drupal custom-elements in a subfolder "global". That way the components are grouped and are global (Dynamic Components) without the need for additional components config and can be rendered with Vue Render Function API (nuxtjs-drupal-ce renderCustomElements helper uses it). Other components, that are not rendered with Vue Render Function API, don't need to be global.

fago’s picture

Title: Consistently prefix components with drupal- » Define and establish best-practice CE naming pattern

yes, existing projects would have to follow, but only once the new naming pattern is adopted. with CE-UI configuration projects should be able to use whatever naming patterns are desired. still, we should define good defaults.

fago’s picture

Issue summary: View changes

Adding some proposal. I think it's not necessary to prefix all content entities with "drupal-" - that makes things to verbose and content-entities names are just a hand-full in Drupal sites, so those would be known and associated with drupal entities easy enough. The drupal- prefix would be there and used for everything which is not an entity, thus more drupally.

fago’s picture

fago’s picture

fago’s picture

Status: Active » Needs work

This is done. So what's missing is changing the remaining custom-element names to follow this pattern:

drupal-markup: DONE
drupal-layout-section -> drupal-layout (it's not a section in drupal speak so let's not invent that here)
drupal-form-FORM_ID
drupal-view-VIEW_NAME

fago’s picture

Issue summary: View changes
fago’s picture

  • fago committed e3aea69b on 1.x
    Issue #3351239 by fago: Improve custom element names of forms to include...
fago’s picture

implemented and tested changes for both forms and views.

the change is trivial, but for views its a backwards compatibility breaking change for existing frontends. It's easy to account for, but still something people will run into. so let's create a change record. Also it would be nice to be able to configure the custom element name in a view. Added issue.

  • fago committed e3aea69b on 3351239-views-ids
    Issue #3351239 by fago: Improve custom element names of forms to include...

  • fago committed d5e049f8 on 1.x
    Issue #3351239 by fago: Improve custom element names of views to include...
fago’s picture

Status: Needs work » Fixed

change record + feature-request created, remaining MRs merged - thus all done!

Status: Fixed » Closed (fixed)

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