I have custom field widget with two fields:

  • product_lines (checkboxes)
  • sales_partners (tableselect)

I need to ajax-reload sales_partners tablesect values depending on product_lines input

In my Widget->formElement() i have:

$sp_table = $this->getSalespartnerTable($product_lines);

$element['product_lines'] = array(
  '#type' => 'checkboxes',
  '#title' => $this->t('Select product line'),
  '#default_value' => $product_lines,
  '#options' => $this->getProductLinesData(),
  '#ajax' => array(
    'callback' => [$this, 'updateSalesPartnersTable'],
$element['sales_partners'] = array(
  '#type' => 'tableselect',
  '#header' => $sp_table['header'],
  '#default_value' => $sales_partners,
  '#options' => $sp_table['options'],
  '#empty' => $this->t('No sales partners found'),

in Widget->updateSalesPartnersTable($product_lines) I have:

$table_data = $this->getSalespartnerTable($allowed_product_lines);
$sp_field = &$form['field_access_settings']['widget'][0]['sales_partners'];
$sp_field['#options'] = $table_data;
$sp_field = \Drupal::service('renderer')->render($sp_field);

$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#edit-field-access-settings-0-sales-partners', $sp_field));
return $response;

So, I use the same function to generate values array for tableselect and I am sure it returns valid data. The widget works fine on initial page load: enter image description here

but when the salespartners table is reloaded by ajax it displays empty rows: enter image description here

The culprit, I think, is this string (returns empty table rows despite valid $sp_field table array):

$sp_field = \Drupal::service('renderer')->render($sp_field);

but I have no clue how to solve this.


Your Answer

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

Browse other questions tagged or ask your own question.