1

When in a function (e.g. a hook_form_alter) is there a reliable way to determine if the form is an entity form and if so, what type of entity?

1 Answer 1

9

You can check if the form is an instance of EntityForm and then get the entity to check the entity type:

if ($form_state->getFormObject() instanceof \Drupal\Core\Entity\EntityFormInterface) {
  $entity_type = $form_state->getFormObject()->getEntity()->getEntityTypeId();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.