Make a Field Label “visually-hidden” in Drupal 8
Using the Form API you can add a visually-hidden
class to a field using the “#title_display” property as shown here:
1 2 3 4 5 6 7 8 |
/** * Implements hook_form_FORM_ID_alter(). */ function mymodule_form_views_exposed_form_alter(&$form, $form_state, $form_id) { if ($form['#id'] == 'views-exposed-form-search-page-1') { $form['keys']['#title_display'] = 'invisible'; } } |
One Comment
Travis Miller
This doesn’t work for me. My $form doesn’t even have a key named ‘keys’.
The closest thing I can find in the $form array is $form[‘my_field_name’][‘widget’][0][‘#title_display’] – which has a value of “before”. But, changing this value doesn’t seem to affect anything.