سؤال

I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).

I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.

The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:

$form['employees'] = array(
  '#type' => 'fieldset',
  '#title' => t('Employees'),
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,
);

$employee_query = db_query("SELECT * FROM {employees} WHERE  id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
    $employee[] = $row;
}

$header = array(
    t('Year'),
    t('Total'),
    t('Internal'),
    t('External'),
    t('Aerospace')
);

$em_delta = 0;
$rows = array();

foreach($employee as $em_delta => $value) {
    $form['employees'][$em_delta]['year'] = array(
        '#title' => '',
        '#type' => 'date_select', // Comes with the date module
        '#date_format' => $format_year,
        '#date_label_position' => 'within',
        '#date_year_range' => '-50:+3',
        '#default_value' => $value[$em_delta]['year'],
        '#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
        '#name' => 'employees['.$em_delta.'][year]', // Same here
    );      
    $form['employees'][$em_delta]['total'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#default_value' => $value['total'],,
        '#size' => 1,
        '#id' => 'edit-employees-' . $em_delta . '-total', 
        '#name' => 'employees['.$em_delta.'][total]'
    );
    $form['employees'][$em_delta]['internal'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#default_value' => $value[$em_delta]['internal'],
        '#size' => 1,
        '#id' => 'edit-employees-' . $em_delta . '-internal', 
        '#name' => 'employees['.$em_delta.'][internal]',
    );
    $form['employees'][$em_delta]['external'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#default_value' => $value[$em_delta]['external'],
        '#size' => 1,
        '#id' => 'edit-employees-' . $em_delta . '-external', 
        '#name' => 'employees['.$em_delta.'][external]',
    );      
    $form['employees'][$em_delta]['aero'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#default_value' => $value[$em_delta]['aero'],
        '#size' => 1,
        '#id' => 'edit-employees-' . $em_delta . '-aero', 
        '#name' => 'employees['.$em_delta.'][aero]',
    );      

    $rows[] = array(
        drupal_render($form['employees'][$em_delta]['year']),
        drupal_render($form['employees'][$em_delta]['total']),
        drupal_render($form['employees'][$em_delta]['internal']),
        drupal_render($form['employees'][$em_delta]['external']),
        drupal_render($form['employees'][$em_delta]['aero']),
    );
}

$form['employees']['table'] = array (
    '#value' => theme('table', $header, $rows, array(), NULL)
);

Here are the problems I am encountering:

  • ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
  • Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
  • The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).

Let's hope that's it =) Can anybody help? What am I doing wrong?

هل كانت مفيدة؟

المحلول

A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.

Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.

Stupid me =)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top