Feb 15, 2021 in Drupal 8

EntityForms are gaining importance in drupal 8 and 9. This is how to make sure you are in one. And how to get the current entity:

if ($form_state->getFormObject() instanceof EntityForm) {
    // Get the current entity:
    $entity = $form_state->getFormObject()->getEntity();
    // continue
}