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.
I need to build a custom block with extra configuration fields. i use hook_block_configure to build it. The problem is that i need to have ajaxx in it to generate some fields according to a select field. I dont have access to the $form_state array.
Any help with this? It's odd that we don't have access to the $form_state array...
Comments
Comment #1
anouAn answer a year later....
For what I found, it will be possible in D8. Didn't find a way in D7. Check this post.
Comment #2
dianikol CreditAttribution: dianikol commentedI have found the solution a year back, I need to use hook_form_alter to access the form in block and to use ajax
Comment #3
DHL CreditAttribution: DHL commentedHi dianikol,
If you use hook_form_alter to create the form, how to pass it to hook_block_configure ?
Comment #4
anouHello DHL,
You do not create the form with hook_form_alter() but within hook_block_configure() and alter it with hook_form_alter()
And by the way, thanks dianikol for the answer ;-)
Comment #5
DHL CreditAttribution: DHL commentedHello anou,
Do you mean place the hook_form_alter inside hook_block_configure like below ?
Comment #6
anouNope. Not at all.
You must have the 2 functions separate.
Hope this will help you to achieve your goal.
Comment #7
DHL CreditAttribution: DHL commentedI have a question about the form_id
No matter which block, I found that their form_id are all the same - "block_admin_configure"
About the code you suggest, I try them before and there are 2 problem.
First, its not work.
Second, those forms I defined in hook_form_alter appear below the "Save block" button at the block configure page. Since I mention that all block configure page has the same form_id. So, those forms appear below the "Save block" button in all different block configure page.
Comment #8
anouFor the order, you must study this : weight
Second: the code does work, believe me :-)
Third: you must install the Devel module and then you'll be able to print the $form variable and find maybe other values to narrow the lack of precision you have with $form_id for blocks...
Comment #9
DHL CreditAttribution: DHL commentedI manage to make Ajax work in block configure page. But it not look good.
Please download the demo module I made from the link below
demo module - Ajax test
This code in this module was copy from the D7 "Example" module. And use one of it demo - Simplest AJAX Example
https://drupal.org/project/examples
Inside the zip file include the screen capture of the problem I facing.
1. Duplicate form display in block configure page.
2. Same form_id among all different block configure page.
Comment #10
dianikol CreditAttribution: dianikol commentedYou must use in
hook_form_alter
not the form_id but the block dellta to narrow down the current form.i.e.
I'm not sure $form['delta'] exists in the $form variable. Just to get the idea
Comment #11
DHL CreditAttribution: DHL commentedTo find the module or delta name, I have another method
This successfully make the form appear in the target block configure page only.
But at the same time the Ajax not work anymore. I don't know why.
Comment #12
dianikol CreditAttribution: dianikol commentedYou should check the delta in the $form array variable. It is safer.
Post your whole code to check it out