The structure of the Form API elements returned by the buildForm function is almost completely the same as it was in Drupal 7. The attributes for the form elements are generally identical. One parameter that has changed is #attached, which is used to add libraries, arbitrary JavaScript, HTML head links and tags, and so on. For example, if you wanted to ensure that the backbone was loaded with the configuration form earlier, you would attach it to a form element like this:
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('mastering_drupal_8.settings');
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#default_value' => $config->get('name'),
'#attached' => array(
'library' => array('core/backbone')
)
);
return parent::buildForm($form, $form_state);
}
One attribute that was added was #cache, to enable customization of the cache contexts, tags, and max-age attributes. If the form varies by request context, such as language, or depends on modifiable information, such as a node or other entity, or is only valid for a limited time, you might edit these attributes. So, if the configuration form was expected to change based on the user's language, you will add the cache attribute as follows:
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('mastering_drupal_8.settings');
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#default_value' => $config->get('name'),
'#attached' => array(
'library' => array('core/backbone')
),
'#cache' => array(
'contexts' => ['language'],
),
);
return parent::buildForm($form, $form_state);
}