0

For the last few days and some of last week I've been trying to fix up the interface of a page we have on one of our modules since it was difficult to use. I decided to take this data and just display it in a table so that it's simple and organized by column.

Here is a very basic recreation of the code.

function _ss_menu_zone_test($form, $form_state) {

  $formfield = array(
    '#type' => 'textfield',
    '#default_value' => '10' // default value set
  );
  $formselect = array(
    '#type' => 'select',
    '#options' => array(1,2,3,4,5,6),
    '#default_value' => 3  // default value set
  );

  $tablesort_headers =array(
    array('data' => t('Price'), 'field' => 'price'),
  );

  $tablesort_rows = array(
    array('price' => 'test row format 1'), // example of basic string row
    array('price' => array('data' => 'test row format 2')), // example of simple string but using data
    array('price' => drupal_render($formfield)),
    array('price' => array('data' => $formfield)),
    array('price' => drupal_render($formselect)),
  );

  // render the tablesort of menu items.
  $form['category_' . $tid]['items'] = array(
    '#theme' => 'table',
    '#header' => $tablesort_headers,
    '#rows' => $tablesort_rows,
    '#sticky' => TRUE,
    '#attributes' => array('id' => 'zone-items-' . $tid)
  );

  return $form;
}

In this example I have one column for the test data and a couple rows to show the differences. The primary issue I've run into here is if I try to add a form API textfield to a cell somewhere it drops value for the #default_value. the field renders correctly with any other key I've specified.

Does anyone know how to go about getting this #default_value to actually populate the field like it does EVERYWHERE else in the Form API?

2

In FAPI (and renderable arrays in general), keys starting with a # are considered properties - so not renderable elements. This means their children aren't processed for content properly.

There are several methods to do what you want, but the easiest is probably references. In your specific example, it would look like this (I've trimmed out some stuff to make my changes more clear):

function _ss_menu_zone_test($form, &$form_state) {

  $tablesort_headers =array(
    array('data' => t('Price'), 'field' => 'price'),
  );

  // render the tablesort of menu items.
  $form['category_' . $tid]['items'] = array(
    '#theme' => 'table',
    '#header' => $tablesort_headers,
    '#rows' => array(),
    '#sticky' => TRUE,
    '#attributes' => array('id' => 'zone-items-' . $tid)
  );

  $formfield = array(
    '#type' => 'textfield',
    '#default_value' => '10' // default value set
  );

  //We need to include the field in the form so it's rendered. This wont display because we're using #theme => table.
  $form['category_' . $tid]['items'][] = array('formfield' => &$formfield);

  //These are your rows, note the byreference on the formfield.
  $tablesort_rows = array(
    array('price' => 'test row format 1'), // example of basic string row
    array('price' => array('data' => 'test row format 2')), // example of simple string but using data
    array('price' => array('data' => &$formfield)),
  );

  //Add your rows to the table.
  $form['category_' . $tid]['items']['#rows'] = $tablesort_rows;

  //Since we used references we want to unset the variables we created.
  unset($formfield);

  return $form;
}
  • Where is the Op using a # that doesn't allow the child items to be rendered? – kiamlaluno Dec 9 '15 at 21:45
  • #rows would be the property that is not rendering the form items properly. – EmGee Dec 9 '15 at 21:51
  • Does the default_value actually appear in the field in this example? I don't see why it's passed by reference. Drupal's internal theme function for table cells does a drupal render on the form api element which should just return the markup of the element. It does, however, the value isn't populated as expected. – Chad Payne Dec 9 '15 at 22:16
  • In my example in the initial post the following renders the element but with out the value i'm expecting. array('price' => array('data' => $formfield)), – Chad Payne Dec 9 '15 at 22:17
  • Also $form['category_' . $tid]['items'][] = array('formfield' => &$formfield); Adds the element to that array but it goes unused or would end up rendering the element after the table is themed out which is not what i'm looking for. – Chad Payne Dec 9 '15 at 22:18
1

I create the elements dynamicaly in ajax and i have common problem. The #default_value if not acting in render() function, but #value does. You can ensure it watching the form_select_options() Using #value is bad practice.

May be post i found (below) could be useful in your case for form generation? https://www.drupal.org/node/1000568#comment-3840362

Also, no need to use render() is $row[]=>('data'=>) drupal will render on his own

Also take a look at this elegant trick https:// passingcuriosity.com/ 2011/drupal-7-forms-tables/ (remove spaces please my reputation dont allow me to post more than 2 links)

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.