// Get the current form state object $form_state = \Drupal::formState(); // Get an array of errors $errors = $form_state->getErrors();
// Get the current form state object $form_state = \Drupal::formState(); // Check if there are any errors if($form_state->hasAnyErrors()){ // Get all the errors as string $errors = $form_state->getErrors(); $error_string = implode(', ', $errors); drupal_set_message($error_string, 'error'); }Description: In this code block, we first retrieve the current form state object using Drupal's dependency injection container. We then check if there are any errors using the hasAnyErrors method. If any errors are found, we retrieve them using the getErrors method and create a string containing all the errors. We then display this error message to the user using Drupal's message system. Package Library: Drupal\Core\Form\FormStateInterface (part of Drupal's Core Form library)