1

I have a custom form generated in a module.

I want to style the individual inputs by adding classes to them.

Can I add them through the buildform()?

public function buildForm(array $form, FormStateInterface $form_state) {
  //Return array of form API elements
  $form['location'] = array(
    '#type' => 'textfield',
    '#placeholder' => 'Enter your suburb or postcode...',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Go'),
  );
  return $form;
}

If yes, how would I do this? I can't seem to find a good reference for parameters that can be passed into the $form array.

Alternatively, is there a way to pass the elements to a theme template like can be done with custom blogs or controllers?

4

You can add classes in #attributes:

$form['submit'] = array(
     '#type' => 'submit',
     '#value' => $this->t('Go'),
     '#attributes' => ['class' => ['button']],
);

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.