Load custom library when form contains special field type I'm writing a custom field in Drupal 8. I added a custom library that contains js and css to my module. They will be loaded on forms. Now I need a way to load the library only when some special field is placed in the form.

How can I check if a form contains a field with field type X?

function rate_field_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
   if(/*Check if form contains field with type X*/)
   {
      $form['#attached']['library'][] = 'rate_field/admin-star-field';
   }
}
I addedsome values in the render array of the formElement method in my customWidget class.

'#attached' =&gt; array(
    'library' =&gt; array(
      'core/jquery',
      'rate_field/admin-star-field',
    ),