Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
I've seen that in a block variant, when you add a block you can't set custom css classes and ids, like you can do in drupal 7 with content panes.
Proposed resolution
Use similar solution to this #2710169: Custom attributes in blocks
Remaining tasks
- Add form fields
- Update from submitter to save attributes
- Update builder to output classes and IDs
- Update schema
User interface changes
Add two textfields for block configuration form to insert classes and IDs for each block in panel variant.
API changes
No
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#94 | panels_custom_attributes_in_panels_blocks-2849867-92.patch | 21.71 KB | kevin.pfeifer |
#89 | panels_custom_attributes_in_panels_blocks-2849867-89.patch | 21.43 KB | szeidler |
| |||
#85 | not-working.png | 55.66 KB | Sseto |
#85 | error-message.png | 99.87 KB | Sseto |
#85 | working-styles.png | 33.4 KB | Sseto |
Issue fork panels-2849867
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
Comment #2
dstorozhukComment #3
dstorozhukAdd patch.
Comment #4
dstorozhukComment #5
elimw CreditAttribution: elimw commentedI tested this and it looks like it's working. Many thanks!
Comment #6
DamienMcKennaLets work on adding some tests for the functionality.
Comment #7
andypostdefaults should be defined in block config somehow
and require config schema for this properties
Comment #8
DamienMcKennaUpdated status per #6 and #7.
Comment #9
dstorozhukandypost, damienmckenna, can you guide me where to place the default setting? I did some investigation but didn't find the place where the default settings must be defined.
Thanks.
Comment #10
OnkelTem CreditAttribution: OnkelTem at Adyax commentedComment #11
jian he CreditAttribution: jian he commentedAdd CSS style attribute
Comment #12
jian he CreditAttribution: jian he commentedComment #13
moymilo CreditAttribution: moymilo as a volunteer and at Drupal Ukraine Community commentedComment #14
moymilo CreditAttribution: moymilo as a volunteer and at Drupal Ukraine Community commentedComment #15
dstorozhukNW.
1. Update configuration schema
2. Update path for exiting page variants
3. Test coverage for new functionality
Check if updated schema will provide a defaults for css_* properties.
Comment #16
b_sharpe CreditAttribution: b_sharpe at ImageX commentedI've been looking at this to move it forward, and I'm questioning why 1/2 are necessary? The settings are attached to the source block configuration just like region in which if not set returns NULL as well as there is no needed update path as they will just return exactly that result which neither breaks nor changes existing variants.
As far as I can see, only tests need to be written here unless someone can enlighten me about the other 2 points
Comment #17
jlballes CreditAttribution: jlballes commentedThe patch at comment #11 works fine!
Someone knows if it will be merged to the next release?
Thanks
Comment #18
ericmulder1980 CreditAttribution: ericmulder1980 as a volunteer commentedThis seems to work nicely when placing blocks in the Panelizer UI. It would however be even better if we can use this in the Panels IPE form so it is configurable for all blocks placed through IPE.
Comment #19
dbjpanda CreditAttribution: dbjpanda commentedWorks fine for me as well. But integration with IPE will be better. Also there should be some instruction or help text e.g should that class name begin with dot or without dot etc.
Comment #20
proweb.ua CreditAttribution: proweb.ua commented#11 works
Comment #21
aleevasThe patch from the #11 works for me too.
Was added warnings from #19
Please review
Comment #22
millionleaves CreditAttribution: millionleaves as a volunteer and commentedThis patch works on its own, but conflicts with the patch provided here:
https://www.drupal.org/project/panels/issues/2769099#comment-12279981 - Block visibility conditions
Both provide functionality that was in D7 Panels but which is missing in the D8 version.
They are able to co-exist, but both try to add lines to the same function,
public function submitForm(array &$form, FormStateInterface $form_state)
, in/src/Form/PanelsBlockConfigureFormBase.php
.I was able to manually add the failed lines from this patch into the function, below the lines added by the patch referenced above, and then both co-existed happily. For the record, those lines from this patch are:
The final version of the function includes these lines:
Comment #23
manuel.adanCSS injection has some security implications. Some kind of validation / filtering could be good at this. Anyway, the patch worked perfectly for me too in two different websites, thank you! Should be in RTBC but changing status to needs work due to pending tests.
Comment #24
nevergone CreditAttribution: nevergone commentedImprove #21 patch and add full Panels IPE support with tests.
Please test and review.
Comment #26
nevergone CreditAttribution: nevergone commentedComment #27
millionleaves CreditAttribution: millionleaves as a volunteer and commentedI'm following up on #22 and #24.
In short, the patch in #24 works for me on Drupal 8.5.5, with Panels 8.x-4.3. I am now using it in 6 sites in 2 separate Drupal instances. Thanks for the efforts that have got us this far - are we RTBC?
As noted in #22, the patch in this issue clashes when you try to apply it after applying the latest patch here:
https://www.drupal.org/project/panels/issues/2769099#comment-12279981 - Block visibility conditions
However, if you install the Block Visibility patch before this one, it is then relatively simple to resolve the two failures that occur when applying this patch (and it is much easier to do it that way compared to applying this patch then the Block Visibility patch).
Essentially, the reason the second patch fails is that when it tries to insert new lines into one of the files being patched, it finds lines from the first patch that it doesn't recognise, so it fails. However, the lines in the two patches don't conflict - they can co-exist - so all you have to do is examine the .rej file after applying the second patch, and add those lines manually.
Comment #28
AndyF CreditAttribution: AndyF at Fabb commentedThanks for the patch! Thought I'd help out with a test and review.
Cool to see tests included! The failed tests with 8.4 are because page manager no longer supports 8.4 IIUC. I think the failed tests with PHP 7.2 are not connected with this patch and are because
\Drupal\Tests\panels\Unit\panels_ipe\RequestHandlerTestBase
extends from a PHPUnit class directly rather than using Drupal's compatibility layer (and PHP 7.2 is tested with PHPUnit 6).Manual testing
Standard
Update classes ✓
Update ID ✓
Update styles ✓
Sanitise input ✓
IPE
Update classes ✓
Update ID ✓
Update styles ✓
Preview block ✓
Sanitise input ✓
No issues I could find.
Code review
(I'm not super-familiar with Panels btw!)
Would it make sense for this to be an array of strings?
text
is translatable; I think it should bestring
like the others.nitpick: missing newline
It might be worth trying to reuse the code from the main module rather than duplicating it in IPE. (Also could help with reuse in page manager.)
Did you try to alter
Drupal\panels_ipe\PanelsIPEBlockRendererTrait::buildBlockInstance()
at all? It might be a better place to add the attributes (one central place). It seems thatDrupal\panels_ipe\Controller\PanelsIPEPageController::getBlockModelData()
also uses the result ofbuildBlockInstance()
as part of the backbone app, but I'm not following the details.Instead of modifying all the blocks after they've been built, it might be easier to do it on a per-block basis in
Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder
?I think these are being added to the defaults for a panels variant plugin, not a block plugin.
It might be nicer to add a new test rather than modifying
\Drupal\panels\Tests\PanelsTest::testLayoutSettings()
.Hope that helps, thanks again!
Edited: Add more context to point 3.
Comment #29
andypostpoints 2 & 3 are debatable, sometimes it is very useful to have different classes for different languages
Comment #30
AndyF CreditAttribution: AndyF at Fabb commentedThanks @andypost. I'd never considered using translatable classes. Unless I'm missing an implication, point 2 isn't really about translation (although it would change what the translation string would be if we did make it translatable, which might be what you're getting at?). I could've added more context for point 3: it's for the inline styling, which I doubt ever makes sense to be translatable? (Maybe I'm not being imaginative enough!)
Comment #31
andypost@AndyF yep, I misread 2) - looks yep it could be a sequence but will require implode/explode processing so not sure if it's reasonable overhead
Comment #32
AndyF CreditAttribution: AndyF at Fabb commentedIt's already being exploded, so I think if anything it'd be (a bit) more performant?
Comment #33
nevergone CreditAttribution: nevergone commentedPatch updated and new feature: Custom styles for display variants.
Dependencies:
https://www.drupal.org/project/ctools/issues/3001713#comment-12786351
Test updated.
Comment #34
nevergone CreditAttribution: nevergone commentedComment #36
nevergone CreditAttribution: nevergone commented(coming soon)
Comment #37
nevergone CreditAttribution: nevergone commentedPatch refactored. Please review and feedback.
Comment #38
nevergone CreditAttribution: nevergone commentedShort array syntax.
Comment #39
nevergone CreditAttribution: nevergone commentedComment #42
nevergone CreditAttribution: nevergone commentedComment #43
nevergone CreditAttribution: nevergone commentedPlease fix project and issue metadata, wtf testing Drupal 8.4.x and PHP 5.5 :(
And related issue: https://www.drupal.org/project/panels/issues/3001708
Comment #44
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedThanks for your efforts. The patch is working well for me in a Drupal 8.6.1 setup with PHP 7.1.
I just found a small nitpick: The patch reuses the same form field description for the block configuration and the panel variant configuration.
In the Panel Variant configuration the first sentence is not accurate, because it's adding the css classes to the layout of the variant and not a block.
Comment #45
Denes.Szabo CreditAttribution: Denes.Szabo as a volunteer commentedI just tested this patch, works! Thank you!
I agree with szeider #44, the wording should be more neutral or customized for the layout variant.
Maybe: "Customize the element style by adding CSS classes."
Comment #46
nevergone CreditAttribution: nevergone commentedOk, labels modified.
Comment #48
Denes.Szabo CreditAttribution: Denes.Szabo as a volunteer commentedSeems the patch #46 works. Thanks!
Comment #49
proweb.ua CreditAttribution: proweb.ua commented#46 works
Comment #50
MaxMendez CreditAttribution: MaxMendez commented#46 works. Thanks!!!
Comment #51
rwilson0429 CreditAttribution: rwilson0429 commented#46 works great and is an extremely valuable enhancement to the functionality of the Panels module. Since Panels is mostly about layout, it seems a natural fit to be able to add css and other custom attributes in the ui.
Thanks for the patch.
Comment #52
knyshuk.vova CreditAttribution: knyshuk.vova at Internetdevels, Drupal Ukraine Community commentedThe patch #46 looks good and applies successfully. +1 for RTBC.
Comment #53
lesleyfernandes CreditAttribution: lesleyfernandes commentedHi guys, are you planning to commit the #46?
Comment #54
sannminwin CreditAttribution: sannminwin as a volunteer commented#46 works perfectly for me on Drupal 8.6.10, php 7.2
Thanks
Comment #55
nevergone CreditAttribution: nevergone commentedTest added with patch: #46
Comment #56
kcabbab CreditAttribution: kcabbab commentednevergone, you and everyone on this patch are awesome!
Since, Block Class module does not work with panels in drupal 8. This patch is a life saver...
#46 worked perfectly... I am a new-bee to Drupal 8 and git bash, so it took me awhile to figure out on how to apply this patch.
For other new-bees like myself, here is what I did to apply the patch in windows:
1) install git bash.
2) copy the #46 patch and put it into "modules/contrib/panels/(the patch name)"
3) open git bash and cd your way to panels.
4) type "ls" to list the files in the panels directory.
5) type "patch -p1 < (the patch file name)"
6) in drupal, "development/performance" clear the cache.
7) and in the varients/panels/content of your pages, in your blocks that you add or will add, you will see "style settings" to configure your class for the block.
Again, thank you so, so much everyone who contributed to this patch.
Oh, I forgot: Drupal 8.6.10, php 7.2, panels 8.4.3
Comment #57
iyyappan.govindWorking fine. Thanks for the patch
Comment #58
vijay.cgs CreditAttribution: vijay.cgs at UniMity Solutions Pvt Limited commentedThe patch #46 works perfectly before enables layout builder.
#46 not working after enabled layout builder module. Screenshot attached.
Comment #59
vijay.cgs CreditAttribution: vijay.cgs at UniMity Solutions Pvt Limited commentedHi all,
If you are working with Layout Builder module mean, Please gothrough the issue, #2998114: Integration with Drupal core's new Layout Builder. It is working fine with layout buider.
Thanks
Comment #60
eit2103 CreditAttribution: eit2103 commentedPatch #46 worked for me too, thank you! Would this patch be somehow used to add css element to variant body classes like we used to have it on Drupal 7?
Comment #61
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedThe patch from #46 now fails to apply due to updates to the module.
Comment #62
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedComment #63
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedI've re-rolled the patch and fixed to use the new tests directory.
Comment #64
andypostComment #65
andypostComment #66
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedIt helps if you create the patch with all of the changes you made...
Comment #67
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedComment #68
MaxMendez CreditAttribution: MaxMendez commentedHi,
I've migrated the panels_custom_attributes_in_panels_blocks-2849867-46.patch patch into the brach 8.x-4.x, and created this new version.
It only does not include the changes of files on /src/Tests, because the folder was removed.
Comment #69
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedPanelsTestTrait
was removed in #63 but we still have references to it.The attached patch removes those references and I hope it fixes test error.
Comment #71
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedActually looks like the issue was precisely to remove
PanelsTestTrait
so adding it back.Comment #72
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedThere was a namespace error on previous patch but now I think
generateCSSProperties()
function should better reside inPanelsIPETestTrait
as it is only used in panels_ipe test.Comment #74
dobrzyns CreditAttribution: dobrzyns as a volunteer commentedComment #75
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedAdded config schema fixes, test fix is still pending.
Comment #76
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedAttempting to fix test
Comment #78
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedAnother fix for test + coding standard
Comment #79
NWOM CreditAttribution: NWOM commentedThe patch worked great, however it only allowed adding custom attributes to blocks, rather than regions too.
In order to style Regions as well, I switched to the Panels Style contrib module that adds plugin support, so that older D7 plugins could be ported and work with it as well.
Also the Panels Extra Styles D8 module uses the previous patch (#2296437: Implement style plugin type) that the module is based on for compatibility. I made both modules compatible with D9 and also added support for the Panels Style module (rather than the patch).
Here is a combination of everything I use to make this work:
Panels Style
#3031437: Error when region style set to default
#3062867: D8.5^/D9 compatibility - User temp store moved to core
#3179208: D8.7^/D9 compatibility - ConfigurablePluginInterface is deprecated
#3179124: D9 Version Requirements
Panels Extra Styles D8
#3179234: Use Panels Style contrib module as a dependency rather than Panels patch
#3179230: D9 Compatibility
The only thing missing from this setup, is being able to add custom attributes to variants. Hopefully this can be added at some point.
I hope this helps others as well!
Edit: I may have spoken too soon. I cannot save block configurations with this setup, only regions. I currently don't recommend this setup until this is fixed.
Comment #80
joel_osc CreditAttribution: joel_osc at OpenPlus commentedI really needed to add classes to a variant as @NWOM mentions above: "The only thing missing from this setup, is being able to add custom attributes to variants. Hopefully this can be added at some point." Instead of making this issue even longer I created a new issue and a patch if anyone is interested #3186390: Add default class and ability to configure additional classes to variants. Thank-you to everyone for helping bring back this awesome functionality.
Comment #81
NWOM CreditAttribution: NWOM commented@joel_osc: Sorry, I think my comment above may have been confusing. Basically the setup with the contrib modules patches (if they worked correctly) would provide classes for blocks and panes, but not variants. However the patch in this issue provides classes for blocks and variants, but not panes. I hope this clears that up :)
Comment #82
joel_osc CreditAttribution: joel_osc at OpenPlus commentedThanks @NWOM for the clarification. I guess I will just keep watching this issue and the related issues and hope that panels soon returns to its former awesomeness. Cheers.
Comment #83
eit2103 CreditAttribution: eit2103 commentedWonder why this isn't included in the module itself but we're using a patch instead.
Comment #84
Sseto CreditAttribution: Sseto commentedHey guys,
I tried to apply 2 different patches for Panels and I'm getting an error.
Patch 1: [Block visbility conditions](https://www.drupal.org/project/panels/issues/2769099)
Patch 2: [Allow for block visibility rules](https://www.drupal.org/project/page_manager/issues/2858877)
`https://www.drupal.org/files/issues/2020-07-13/panels-block_visibility_c... (Block visibility conditions)`
`https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_... (Custom attributes in panels blocks and variants)`
`Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_...`
I tried swapping them around and still the same error.
The weird part is that Both patches are somehow still applied and working.
*EDIT: The UI to add `style settings` in https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_... is showing, but it actually doesn't add the CSS classes.
Comment #85
Sseto CreditAttribution: Sseto commentedI tried following @millionleaves posts #22 and #27 and it's sort of working now. I'm able to apply style settings outside the block layout only. When I try adding style settings to individual blocks, it gets an error message.
Comment #86
Sseto CreditAttribution: Sseto commentedI found out that adding Style Settings with In-page editor works. But as soon as I open page_manager and edit through there, all the CSS classes are removed.
Comment #87
Sseto CreditAttribution: Sseto commentedI figured it out!
After following @millionleaves #22 and realized that my .rej file is different from his. So I updated it with the latest patch.
Updated it with the following:
After refreshing cache, It still wasn't working. Then, I realize that StandardDisplayBuilder.php inside /src/Plugin/DisplayBuilder also had a .rej file. I updated to the following:
Also keep in mind you need to apply this patch "https://www.drupal.org/project/page_manager/issues/2858877" in order to get "Block Visbility Conditions" to work.
Hope this helps someone who is trying to use "Block Visbility Conditions" and this patch for Page_manager and Panels.
Comment #88
NWOM CreditAttribution: NWOM commented@Sseto: Here is a patch that already combines both patches in case you want to add it to composer: #3183258: Block visibility conditions and Custom attributes combined patch for composer.
As a summary, the patch in this issue works, however it is incompatible with the Block Visibility patch, until one gets committed and the other patch gets re-rolled. As a workaround, the combined patch that I created can be used.
Comment #89
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedReroll of patch #78 against current dev.
Comment #90
kevin.pfeifer CreditAttribution: kevin.pfeifer commented#89 now causes the following warning
Warning: Invalid argument supplied for foreach() in Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder->buildRegions() (line 165 of modules/contrib/panels/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php).
will try to adjust that patch to fix this
Comment #91
kevin.pfeifer CreditAttribution: kevin.pfeifer commentedComment #93
kevin.pfeifer CreditAttribution: kevin.pfeifer commentedForgot to add the PanelsStyleTrait
Comment #94
kevin.pfeifer CreditAttribution: kevin.pfeifer commentedComment #95
kevin.pfeifer CreditAttribution: kevin.pfeifer commentedNo idea why that test fails
Comment #96
s.kulyk CreditAttribution: s.kulyk commentedNew patches are not compatible with old ones because of schema changes. Updating 21 patch to be compatible with latest version because it works for me.
Comment #97
joseph.olstadRTBC patch from comment #94 labelled as patch 92 as it has tests, functionality works, great job! This is definately needed and should go in asap.
Patch #96 has one additional fail and is based on a much earlier implementation and lacks tests. Lets go with the patch from comment #94 please as the patch from #94 has tests and it works flawlessly.
Just to be clear, this is the patch I am setting as RTBC: https://www.drupal.org/files/issues/2023-01-11/panels_custom_attributes_...
Comment #98
joseph.olstadComment #99
joelpittetOk I committed #94 thanks all contributors, I tried to give credit where due and gave authorship to @nevergone for the tests because they tend to be the trickiest to get help with but are the real reason I'm committing this.
@eclipsegc mentioned (on slack) there are likely other modules out there that could accomplish this feature but bringing the expected features from previous versions seems reasonable.