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

anou’s picture

An answer a year later....
For what I found, it will be possible in D8. Didn't find a way in D7. Check this post.

dianikol’s picture

I have found the solution a year back, I need to use hook_form_alter to access the form in block and to use ajax

DHL’s picture

Hi dianikol,
If you use hook_form_alter to create the form, how to pass it to hook_block_configure ?

anou’s picture

Hello 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 ;-)

DHL’s picture

Hello anou,
Do you mean place the hook_form_alter inside hook_block_configure like below ?

function example_block_configure($delta = '') {
  $form = array();
  if ($delta == 'example') {
    function example_form_alter(&$form, &$form_state, $form_id) {
      $form['example_block_text'] = array(
	'#type' => 'textarea',
	'#title' => t('This is the title'),
	'#default_value' => variable_get('example_block_text', ''),
      }
    );
  }
  return $form;
}
anou’s picture

Nope. Not at all.
You must have the 2 functions separate.

<?php
function example_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
   case 'example':
     //you define here all form elements cf.https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
      $form['example_block_text'] = array(
         '#type' => 'textarea',
         '#title' => t('This is the title'),
         '#default_value' => variable_get('example_block_text', ''),
      );
   break; 
  }
  return $form;
}

function example_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the-id-of-your-example-form') {
     //here some code to alterate the form you've defined previously and in particular act on it with ajax...
  }
}
?>

Hope this will help you to achieve your goal.

DHL’s picture

I 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.

anou’s picture

For 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...

DHL’s picture

I 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.

dianikol’s picture

You must use in hook_form_alter not the form_id but the block dellta to narrow down the current form.

i.e.

function mymodule_form_alter(&$form,&$form_state,$form_id) {
  if ($form_id == 'block_admin_configure' ) {
    // Find the delta in the $form variable
    if ($form['delta'] == 'the_delta_you_are_looking_for') {
      // do your changes
    }
  }
}

I'm not sure $form['delta'] exists in the $form variable. Just to get the idea

DHL’s picture

To find the module or delta name, I have another method

<?php
function mymodule_form_alter(&$form,&$form_state,$form_id) {
  if ($form_id == 'block_admin_configure' && arg(4) == 'mymodule') {
      // do your changes
    }
  }
}
?>

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.

dianikol’s picture

You should check the delta in the $form array variable. It is safer.

Post your whole code to check it out

Version: 7.14 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.