How can I render form elements?
Question
In Drupal 6, I am using the following code.
$rows[] = array(
array('data' => drupal_render($form['collected'])),
array(
'data'=> drupal_render($form['count']) . drupal_render($form['submit']),
'colspan' => 3
)
);
In Drupal 8, I am trying to use the following code, but I see the HTML markup of $form['count']
.
$rows[] = [
['data' => \Drupal::service('renderer')->render(($form['collected'])],
[
'data' => \Drupal::service('renderer')->render($form['count']) . \Drupal::service('renderer')->render($form['submit']),
'colspan' => 3,
]
];
Solution
In Drupal 8+, you do not need to use a separate theming function as was required in Drupal 6. You can put the form element directly into the table without rendering, and Drupal will handle that on the back-end. It's much simpler:
$row = [];
$row[] = [
'data' => [
'something' => $form['something'],
'something_else' => $form['something_else'],
],
];
$rows[] = $row;
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange