Search code examples
drupaldrupal-themesahah

Drupal module function theming with ahah


My main question is:

Does the theme_hook() function gets called whenever the form gets rebuilt via ahah (ahah_helper) ?

I'm trying to show a select box, with some filtering options, when the user changes it, the table below it changes too.

I have this by now:

function veiculos_listar_form($form_state)
{
    $form = array();
    ahah_helper_register($form, $form_state);

    //biulds $options

    $form['listar_veics'] = array(
        '#type'   => 'fieldset',
        '#prefix' => '<div id="listar-veics-wrapper">', 
        '#suffix' => '</div>',
        '#tree'   => TRUE,
        );


    if (!isset($form_state['values']['listar_veics']['filial']))
        $form['#filial_veic'] = 1;
    else 
        $form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];

    $form['listar_veics']['filial'] = array(
        '#type' => 'select', 
        '#title' => "Listar veículos da filial", 
        '#options' => $filiais,
        '#default_value' => $form['#filial_veic'],
        '#ahah' => array(
            'event'     => 'change',
            'path'      => ahah_helper_path(array('listar_veics')),
            'wrapper'   => 'listar-veics-wrapper',
            'method'    => 'replace',
            ),
    );


    return $form;
} 

function veiculos_listar_form_submit($form, &$form_state)
{

}


function _listar_veiculos_tabela($filial)
{
    //builds $header and $data

    $table = theme_table($header, $data);
    return $table;
}


function theme_veiculos_listar_form($form) 
{
    $output = drupal_render($form);
    $filial = $form['#filial_veic'];
    $output .= '<br>' . $filial . '<br>';
    $output .= _listar_veiculos_tabela($filial);
    return $output;
}

function veiculos_theme() {
    return array(
        'veiculos_listar_form' => array(
            'arguments' => array('form' => NULL),),
    );
}

In my little and innocent world, it should work if theme_hook is called on every ahah event (change).

The problem is, the variable printed is always the same, like what the user is choosing isn't being stored. If the user select a different options, it shows the new option, but the $filial variable is always the same when the theme prints.

Like this:

http://img230.imageshack.us/img230/9646/62144334.jpg

Any suggestion on what i could do to make this work? I'm developing our own module, so using views module isn't a good idea.

Thanks.


Solution

  • You should redo code this way. Ahah callback I do not wrote, I think you would not have problem with it. Check some examples on drupal.org

    function veiculos_listar_form($form_state)
    {
        $form = array();
        ahah_helper_register($form, $form_state);
    
        //biulds $options
    
     // remove divs because we do not want to reload selector with ahah
        $form['listar_veics'] = array(
            '#type'   => 'fieldset',
            '#tree'   => TRUE,
        );
    
    
        if (!isset($form_state['values']['listar_veics']['filial']))
            $form['#filial_veic'] = 1;
        else 
            $form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];
    
      // add cover div here, because we will reload table
        $form['table'] = array(
            '#prefix' => '<div id="listar-veics-wrapper">', 
            '#suffix' => '</div>',
            '#type' => 'markup',
            '#value' => _listar_veiculos_tabela($form['#filial_veic']),
        );
    
        $form['listar_veics']['filial'] = array(
            '#type' => 'select', 
            '#title' => "Listar veículos da filial", 
            '#options' => $filiais,
            '#default_value' => $form['#filial_veic'],
            '#ahah' => array(
                'event'     => 'change',
                'path'      => ahah_helper_path(array('listar_veics')),
                'wrapper'   => 'listar-veics-wrapper',
                'method'    => 'replace',
                ),
        );
    
    
        return $form;
    } 
    
    function veiculos_listar_form_submit($form, &$form_state)
    {
    
    }
    
    
    function _listar_veiculos_tabela($filial)
    {
        //builds $header and $data
    
        $table = theme_table($header, $data);
        return $table;
    }
    
    
    function theme_veiculos_listar_form($form) 
    {
        $output = drupal_render($form);
        return $output;
    }
    
    function veiculos_theme() {
        return array(
            'veiculos_listar_form' => array(
                'arguments' => array('form' => NULL),),
        );
    }