1

In a custom form I created a table element:

  // Table results.
  $form['table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => ['id' => 'table-results'],
    '#empty' => t('No results.'),
    '#prefix' => '<div id="ajax-table-results">',
    '#suffix' => '</div>'
  ];

This table is ajaxified and updated when I click on a specific button.

I need to get the output HTML of this table in another function. If I use a dpm($form_state->getValue('table')); I get an empty array. Yet, the other values (of other fields) are accessible in the $form_state.

// My second function
public function secondfunction(array $form, FormStateInterface $form_state) {
  dpm($form_state->getValue('table')); // -> empty value...
  dpm($form_state->getValue('textfield')); // I get a value
}

What do I change in my code to solve this problem?

2

When you want to get the HTML of the table you must use the renderer service.

In Drupal 8:

$html = \Drupal::service('renderer')->render($form['table']);

Or better, use dependency injection :)

In Drupal 7:

$html = drupal_render($form['table']);

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.