How do I get $entity in hook_form_alter()?
I think that it should be possible to do something like this.

if ($form_state-&gt;getFormObject() instanceof \Drupal\Core\Entity\EntityFormInterface) {
  $entity = $form_state-&gt;getFormObject()-&gt;getEntity();
  $entity_type = $entity-&gt;bundle();
}   


UPDATE: In Drupal 7 it should be done like this:

$node = $form['#entity_type']; // E.g this could be, (#node, #user etc.)


Take a look at this site, drupal discussion.