0

i'm new on drupal, i've create a module that shows a form (with tableselect, list select and a submit button) the tableselect shows data from the database, but i want to filter what i want to see on the table by selecting what i want from the list select and submit. i've create the submit function (it contains the query with condition) and when i use dsm($form['tableselect'] everything works (filter works), but how can i show the result's select on the tableselect ? because it doesn't change

here's my form :

$form['options_state'] = array(
    '#type' => 'value',
    '#value' => array (
        'all'=>t('All'),
        'Valid'=>t('Valid'),
        'published'=>t('published'),
        'not published'=>t('not published')

        )

    );


$form['state_book'] = array(
    '#type' => 'select',
    '#title' => t('state :'),                     
    '#options' => $form['options_state']['#value'],        
);


// filter submit button

$form['filter'] = array(
    '#type' => 'submit',
    '#value' => t('filter')                
    );

and this is my table select (the first one shows all records) ;

$header  = array(
    'book_title' => t('title'),
    'book_state' => t('state'),   
);

    $sql = db_select('field_data_field_title','ta');
    $sql->join('field_data_field_state','st','st.entity_id = ta.entity_id');


    $sql
        ->fields('ta', array('field_titre_en_arabe_value','entity_id'))       
        ->fields('st',array('field_state_value'));
        $result = $sql->execute();


    $rows = array();



foreach ($result as $res){ 

$rows [] = array(
        'book_title' => l($res->field_title_value, 'node/'.$res->entity_id),
        'book_state' => $res->field_state_value           
      );  

}

$form['table1'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('empty !'),

);


return $form;

}

table1 works, but it shows all records (book published and not published) now here's my submit function :

function book_form_submit($form, &$form_state){



$cond = $form_state['values']['state_book'];


$header  = array(
    'book_title' => t('title'),
    'book_state' => t('state'),
);

$sql = db_select('field_data_field_title','ta');
$sql->join('field_data_field_state','st','st.entity_id = ta.entity_id');

$sql
    ->fields('ta', array('field_title_value'))        
    ->fields('st',array('field_state_value'));


    $sql->condition('st.field_state_value', $cond, '=');       



$quer = $sql->execute();


$rows2 = array();

foreach ($quer as $q){ 

    array_push($rows, array(
        'book_title' => $q->field_title_value,
        'book_state' => $q->field_state_value,

    ));

}    
$form['table2'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('empty!')
);   
}

Now how can i put result of submit on table1 to show what the query returns ??

Thanks

  • Any reason you are not using the Views module? See also: api.drupal.org/api/drupal/includes!form.inc/function/… – Citricguy Jun 21 '15 at 1:50
  • Hello & thanks for helpin. if i want to use drupal_rebuild_form, i'm supposed to be using ajax on my submit button right ? – Mitch Jun 21 '15 at 7:23
  • Views module is used for front i think ? maybe i'm wrong, cause i'm working on a backoffice of a web site so i've to create a module to deal with books – Mitch Jun 21 '15 at 7:44
  • i'm working with ajax, i've added a callback + wrapper to the submit button, the tableselect seems to change BUT only : #header,#empty change, the #options dont change, any solution please ? – Mitch Jun 21 '15 at 14:49
  • I'm having a bit of trouble following the code, but I believe you're trying to modify the $form array in the submit callback. This is not passed by reference, and is not being returned. You may need to place it into $form_state which is passed by reference and then force a form reload with $form_state['rebuild'] = TRUE; until your submit function is indeed complete. If you will simplify your example code a bit I'll try to debug with you. At a glance however, I would think the Views module would be very capable here. – Citricguy Jun 21 '15 at 19:03
1

Digging in, and trying to run some pseudo code it looks like there may be a problem with AJAX callbacks and tableselect fields regarding Drupal Core.

https://www.drupal.org/node/1458824 (7x and 8x)

I would consider using the Views module with exposed filters.

You could fairly easily create a view that displays 2 articles, which exposes a state drop down list. You could use a page or block view for the front end.

If you update your question with what you are ultimately trying to accomplish I can try to put together a more appropriate Views setup.

  • hi, yes the trouble is 100% on AJAX tableselect fields, my tableselect is composed of a type,header,options, empty, now when i click on the submit button only type,header and empty change, but not the options (that is an array) – Mitch Jun 22 '15 at 7:55
  • The fact is my db query works, the rows (options) aren't empty (dsm shows it) ! – Mitch Jun 22 '15 at 7:57
0

i finally set a $form_state['storage']['state_book'] and i set the $form_state['rebuild'] on the submit function after doing :$form_state['storage']['state_book'] = $form_state['values']['state_book']; Do not forget to set default_value on $form[''] like this :

$form['state_book'] = array(
    '#type' => 'select',
    '#title' => t('state of book :'),                     
    '#options' => $form['options_state']['#value'],
    '#default_value' => isset($form_state['storage']['state_book']) ? $form_state['storage']['state_book'] : '',       

);

I've even do :

if (isset($form_state['storage']['state_book'])){
            $resultat->condition('st.field_state_book_value', $form_state['values']['state_book']); }

Thanx all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.