/** * Implements hook_field_widget_form_alter(). */ function {{ machine_name }}_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) { // Add a css class to widget form elements for all fields of type mytype. $field_definition = $context['items']->getFieldDefinition(); if ($field_definition->getType() == 'mytype') { // Be sure not to overwrite existing attributes. $element['#attributes']['class'][] = 'myclass'; } }