How do I get $entity in hook_form_alter()? hook_add_form() gets $entity_type and $entity as parameters, but hook_form_alter() does not have any of those parameters.

How do I get the entity type and the entity object in hook_form_alter()in Drupal 7?
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.