I want to add a CSS class to a label on a page that is using Drupal 8 Form API. I have been unable to find a reference online on how to apply that. I have used the following workaround, but it produces some strange results.
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['label1'] = array(
'#type' => 'label',
'#title' => $this->t('QUESTIONNAIRE'),
'#id' => 'lbl1',
'#prefix' => '<div class="caption1">',
'#suffix' => '</div>',
) ;
and the HTML rendered is:
<div class="caption1"><label for="lbl1" class="control-label">
<div class="caption1"></div>QUESTIONNAIRE
</label>
Not only is the div statement in the wrong place but it is rendered twice.
I had found postings from several years ago that show this was not possible, but I am hoping that since then, and with D8, it has been fixed. I do not want to do it with prefix/suffix, but as a separate array element.
PS: this site is Drupal 8.0.0-rc2