Problem/Motivation
Create a subtheme from bootstrap5 theme, which Drupal doesn't list under /admin/appearance
Steps to reproduce
Create a subtheme from bootstrap5: /admin/appearance/settings/bootstrap5/. The subtheme folder exists. But drupal doesn't list it in: /admin/appearance/
Proposed resolution
Add checks in the code so that all the rules on Naming and placing your Drupal module are followed.
The method used in the code (regex, strlen, or in_array) is shown after each rule, and the existing check is stroked-through:
- It must start with a letter. (
regex)
- It must contain only lower-case letters, digits, and underscores. (
regex)
- It must not contain any spaces. (
regex)
- It must not be longer than 50 characters. (
strlen)
It must be unique. Your module should not have the same short name as any other module, theme, theme engine, or installation profile you will be using on the site. (in_array DONE)
- It should not be any of the reserved terms :
src, lib, vendor, assets, css, files, images, js, misc, templates, includes, fixtures, Drupal.(in_array)
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
fvd commentedComment #3
abyss commentedHi @FvD, have you started fixing this or can I start fixing this issue?
Comment #4
abyss commentedComment #5
fvd commentedHi @Abyss, do you start to fix it. (Im very new to drupal) thanks
Comment #7
abyss commentedI added a function for replacing a space on the underline for the subtheme machine name field. Now, if the user enters an incorrect machine name for the subtheme, it will be corrected during its generation.
Comment #8
schiz0id commented@Abyss thanks for looking into this.
Your commit does fix the underscore issue. I tested it, and it works fine.
However, I tried testing it out a couple different ways to try and break it. The machine name for a theme should only consist of lowercase letters, underscores, and numbers. They should also start with a letter. Even with your fix in place, I'm still able to create a theme with a machine name that begins with a number (8theme_test, for example).
The reason this is problematic is because the subtheme has to use theme hooks, which are PHP functions, and the function names have to start with the machine name. If you allow a machine name to begin with a number, PHP is going to throw syntax errors because this function/theme hook would be invalid:
function 8theme_test_css_alter()---
Edit: I got bored and decided to write a regex form validation for this. Attaching a patch.
Comment #9
schiz0id commentedComment #10
schiz0id commentedComment #11
schiz0id commentedComment #13
ressaThanks @schiz0id, I agree that using regex is a more robust solution. Based on your patch, I have updated the 3329010-dont-see-created fork.
I added the regex check as a separate function and simplified the regex a little bit, I think this should also work.
I also added a check for machine name length is 50 characters, and for the reserved terms listed on Naming and placing your Drupal module.
Comment #14
ressaComment #15
fvd commentedThank you, @ressa, @schiz0id, @Abyss.
It works for me
Comment #16
ressaThanks for testing the patch @FvD, and also for posting the bug in the first place, since it showed that the missing checks allowed malformed sub-theme machine names, in many different ways.
Feel free to change the Status to Reviewed & tested by the community if you think the code can move on, and get committed.
Comment #17
schiz0id commentedThanks, @ressa! I didn't know I could get access to the forked repo or I would have made a commit. I'll keep that in mind next time.
I went ahead and pulled down your changes and manually ran through all of the validations we put together, and they work! Thanks again for pushing this across the finish line.
Comment #18
schiz0id commentedComment #19
ressaYou're welcome @schiz0id! There are so many things to keep in mind with the (not so) new Gitlab system. I kept forgetting that I need to click "Get push access" to and existing forks, and then log in at Gitlab, and THEN I can push to existing branches. After much repetition, I think I finally got the steps down.
Thanks for reviewing the code, this will be a nice improvement to the Boostrap5 sub-theme tool. Have a nice day!
Comment #24
vladimirausThank you. 🙏
Committed to both 2 and 3 and addresses
t()function.Comment #25
ressaPerfect, thanks @VladimirAus!