5

Objective:

To combine AJAX Autocomplete with my Tableselect Form to create a key for users to select imported products that don't match existing products within your Drupal site. This new billing module would make it possible to use a specific 3rd party billing service.

Issue:

I am unable to get the following code to return the values inserted via the AJAX row. The array continues to send back the array keys of the row as both keys and values. Here is what the original selection is: [here]

And here is the returned values:

[See here]

As you can see the rows selected have their array key listed as the value on return, where as the non selected row has the value 0.

Question: How do you change the return value so that it is the value that was selected on my AJAX form field?

function Test_product_form($form, &$form_state)
{
  $form_state['page_num'] = 2;

  $form['Markup'] = array(
    '#markup' => t('Please select and synchronize Test Subscription Offers with your Drupal Commerce Product SKUS below:') . '</br>' . '</br>'
  );


  foreach ($form_state['sub_offers'] as $key => $offer) {
    if ($offer['Status'] == 'Active') {
      $product = commerce_product_load_by_sku($offer['OfferCode']) ? commerce_product_load_by_sku($offer['OfferCode']) : FALSE;

      if ($product === FALSE) {
        $rows[] = array(
          'Test Subscription Offers' => array(
            'data' => $offer['OfferCode'],
            'id' => 'row' . $key . '-aso',
            'class' => 'col-aso'
          ),
          'Drupal Product SKUS' => array(
            'data' => array(
              // drupal_get_form('commerce_product_line_item_add_form'),
              '#type' => 'textfield',
              '#title' => t('Product SKU'),
              '#description' => t('Enter the SKU of the product to add to the order.'),
              '#autocomplete_path' => 'commerce_product/autocomplete/commerce_product/line_item_product_selector/product',
              '#size' => 60,
              '#maxlength' => 255,
              //In order for AJAX within the Rows of tableselect form you must declare the 
              '#id' => $key . 'product_textfield'
            ),
            'id' => 'row' . $key . '-dps',
            'class' => 'col-dps'
          )
        );

      } else {
        $rows[] = array(
          'Test Subscription Offers' => array(
            'data' => $offer['OfferCode'],
            'id' => 'row' . $key . '-aso',
            'class' => 'col-aso'
          ),
          'Drupal Product SKUS' => array(
            'data' => array(
              '#type' => 'link',
              '#title' => $product->sku,
              '#href' => 'admin/commerce/products/' . $product->product_id
              // '#options' => $l_options,
              // '#suffix' => ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed))),
            ),
            'id' => 'row' . $key . '-dps',
            'class' => 'col-dps'
          )
        );
      }
    }
  }
  $hinput = array(
    'Test Subscription Offers' => 'aso',
    'Drupal Product SKUS' => 'dps'
  );


  foreach ($hinput as $hkey => $hvalue) {
    $header[$hkey] = array(
      'data' => $hkey,
      'id' => 'head-' . $hvalue,
      'class' => 'col-' . $hvalue
    );
  }

  $options = array();


  foreach ($rows as $key => $row) {
    $options[$key] = array(
      'Test Subscription Offers' => $row['Test Subscription Offers'],
      'Drupal Product SKUS' => $row['Drupal Product SKUS']
    );

  }

  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    //'#js_select' => TRUE,
    '#multiple' => TRUE
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#submit' => array(
      'Test_payment_submit_step_two'
    ),
    '#validate' => array(
      'Test_payment_validate_step_two'
    )
  );
  ddl_once($form);
  return ($form);

}
1
  • You should reduce your code into an easy to test fragment. There is no need to call your particular functions. Use i.e. '#autocomplete_path' => 'user/autocomplete', which works for everybody. In short weed out the ballast. Ping me when done ;) Oct 13, 2012 at 10:05

1 Answer 1

6

Short answer: you cannot use tableselect for submitting form elements.

Longer answer: the tableselect is for selecting one or multiple rows for further processing. You can check this through your submit handler only receiving one value for each row.

See http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#tableselect (it links to a full example on http://drupal.org/node/945102#drupal7).

If you really need a table form you could use dwillcox suggestion to use a theme function to render your form as described http://drupal.org/node/1587968

I'm not sure but have you thought about multiple forms on one page? That is each line from your tableselect attempt has it's own form. For this to work you need to implement a hook_forms(). I've used this for annotate module.

Some arguments about tableselect versus theming a form:

  • As table select adds radio or checkboxes it changes the complete form thus its meaning. The bad implementation of Drupal is it renders the form elements thus making the complete page looks like a form.
  • Theming your form is ugly as the exercise from http://drupal.org/node/1587968 shows.

Some nitpicking one your code:

  • mixed case function names : violation of the coding standards
  • mixed case array keys: this is not expected by most coders.
  • swapped the key and value for $hinput which makes it hard to maintain.

Let us know what solution you took.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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