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->getFormObject() instanceof \Drupal\Core\Entity\EntityFormInterface) {
$entity = $form_state->getFormObject()->getEntity();
$entity_type = $entity->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.
$form['node']
Entity types each implemented their own form in D7, there wasn't a generic one. You're at the mercy of what the author of each bit of code decided at the time.
For example you can find consistency with some of the core entity types:
$form['#node']
$form['#user']
$form['#comment']
But not all the way through:
$form['#term']
You might find it easier to target the specific types of entity you're interested in, determine what the correct array key is for each, and store them in a map somewhere for later access.