0

I have created a draggable table and add a submit button below it. I am able to view the submit button but submit handler is not working. Below is the code.

function form_content_reorder($form, &$form_state)
{    
$form = array();
$form['items'] = array();
$form['items']['#tree'] = TRUE;
$form['#theme'] = 'usercontentranking_reorder_form'; 
$form['name'] = array(
 '#type' => 'item',
 '#markup' => 'Hello  '.get_genderval($user_data['gender']).$user_data['user_name'],);
$form['submit'] = array(
 '#type' => 'submit',
 '#value' => t('Save Preferences'),
 '#submit' => array('user_content_ranking_save'),);
 return $form;
 }

function usercontentranking_theme($existing, $type, $theme, $path) {
$themes = array(
'usercontentranking_reorder_form' => array(
  'render element' => 'element'
),
);
return $themes;
}

function theme_usercontentranking_reorder_form($vars) {
$element = $vars['element'];
drupal_add_tabledrag('draggable_table', 'order', 'sibling', 'weight-group');
$header = array(
 'label' => t(''), 
 'label2' => t(''),
 'weight' => t('Weight'),
 'operations' => t('Operations'),
); 
$rows = array();

foreach (element_children($element['items']) as $key) {
$row = array();
$row['data'] = array();
$row['data'][] = drupal_render($element['items'][$key]['title']);
$row['data'][] = drupal_render($element['items'][$key]['user_fields']);
$row['data'][] = drupal_render($element['items'][$key]['weight']);
$row['data'][] = drupal_render($element['items'][$key]['operation']);
$row['class'] = array('draggable');
$rows[] = $row;

$themes = drupal_render($element['name']);

$themes .=theme('table', array(
  'header' => $header, 
  'rows' => $rows,
  'attributes' => array('id' => 'draggable_table'),
));

$themes .= drupal_render($element['submit']);

return $themes;
}

function user_content_ranking_save($form, &$form_state)
{
  print "text";
}

The line $themes .= drupal_render($element['submit']); able to show up the button but the function user_content_ranking_save is not getting called. Please help me in this.

2

It looks like you are declaring your submit button's submit handler correctly. Rather than debugging with print(), try using drupal_set_message() or the Devel module's dpm().

Example with drupal_set_message():

function user_content_ranking_save($form, &$form_state) {
  drupal_set_message('Submit handler was called.');
}

Edit:

Also, if you are constantly refreshing the same form, Drupal may have cached it. Try clearing your cache and see if your submit handler is now being called.

Answer:

Are you sure the whole form is being rendered when using your theme function? It may be that the form is not being validated because the rendered output of the form does not contain the form_id, form_build_id, and/or form_token.

4
  • No it is not getting called. I am using Komodo IDE with XDEBUG. So debugger doesn't get hit in the submit region. If I am using the FORM API without theme function then all things get back to normal. But in my case submit handler is behaving abnormally. Mar 17 '13 at 6:13
  • @user1843970 - Are you sure the whole form is being rendered when using your theme function? It may be that the form is not being validated because the rendered output of the form does not contain the form_id, form_build_id, and/or form_token.
    – Aiias
    Mar 17 '13 at 6:47
  • @Aiias- Thanks Man. Your suggestion put me in right direction. Before $themes .= drupal_render($element['submit']); I have added following three lines $themes .= drupal_render($element['form_id']); $themes .= drupal_render($element['form_build_id']); $themes .= drupal_render($element['form_token']); and it worked like a charm. I marked your suggestion as answer. Mar 17 '13 at 10:48
  • @user1843970 - One thing to consider that may be more reliable is to just append the rest of the rendered form to your themed output using drupal_render($element). Elements already rendered will not get rendered again.
    – Aiias
    Mar 17 '13 at 18:46

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.