Skip to content

Allow sorting classes inside function calls in Twig templates #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

bronisMateusz
Copy link

@bronisMateusz bronisMateusz commented Apr 16, 2025

Support for prettier-plugin-tailwindcss-drupal

Description

This PR adds support for the prettier-plugin-tailwindcss-drupal by including it in the plugin's supported file types and processors.
The Drupal-specific plugin extends the sorting logic to match Tailwind conventions commonly used in Drupal theme development, including handling template attributes such as attributes, title_attributes or content_attributes.

Problem

Tailwind projects built within Drupal environments often rely on custom attributes, classes, and template structures (e.g., .html.twig files).
The default Tailwind Prettier plugin does not handle these files or Drupal-specific class patterns out-of-the-box.

Solution

  • Register .html.twig and other Drupal-related file types as supported.
  • Delegate class sorting and attribute handling in those files to prettier-plugin-tailwindcss-drupal, which is built on top of the core plugin but tailored to the Drupal ecosystem.
  • Ensure support for Drupal’s template attribute system (e.g., attributes.addClass()), commonly used for dynamic class injection.

Testing

  • Verified sorting and attribute handling behavior in .html.twig templates with embedded Tailwind classes.
  • Ensured no regressions in other supported file types.
  • Ran npm test to confirm plugin compatibility.

…e formatting consistency

- Added support for `attributes.addClass()` in Drupal Twig templates.
- Updated test cases to validate new `addClass` functionality.
- Improved formatting consistency in TypeScript code for better readability.
- Added .DS_Store to .gitignore to prevent unnecessary files from being tracked.
@thecrypticace thecrypticace self-assigned this May 23, 2025
@bronisMateusz
Copy link
Author

@thecrypticace Thanks for the code revisions. Any plans to release this feature in the near future?

@thecrypticace thecrypticace added this to the 0.7.0 milestone Jul 23, 2025
@thecrypticace thecrypticace changed the title feat: Enhance Twig template handling with addClass() support for Drupal syntax Allow sorting classes inside function calls in Twig templates Jul 31, 2025
@bronisMateusz
Copy link
Author

bronisMateusz commented Jul 31, 2025

I think we could also consider support for removeClass(). It is not as common as addClass(), but it happens in some projects, and I had forgotten about it. @thecrypticace If you want, I can prepare the corresponding code changes.

https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates#s-attributesremoveclass

@thecrypticace
Copy link
Contributor

I still have to do some thinking about the implementation as there's a disconnect between how call expressions are walked in JSX and Twig. That might be fine to have a slight difference in behavior but not sure.

I think we could also consider support for removeClass().

This implementation supports any function call. I made it generic. Our plugin already has affordances for sorting in function calls via the tailwindFunctions prettier option.

See the updated tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants