I want bundle in hook_form_alter. In Drupal7 we can get all these in hook_form_alter.

I tried like this in D7:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  $bundle_type = $form['#bundle'];

Here I am getting bundle in D7. Whereas in D8 I am not getting any bundle like article/page/tags etc.,

  • So you want the node bundle basically?
    – Zuzuesque
    Aug 4, 2016 at 11:55
  • not only node bundle, i need that form bundle like article(node), tags(taxonomy, etc) in hook_form_alter().
    – Ajay Reddy
    Aug 4, 2016 at 12:00

1 Answer 1


This is the final solution:

use Drupal\Core\Entity\ContentEntityForm;

$bundle = FALSE;
$form_object = $form_state->getFormObject();
if ($form_object instanceof ContentEntityForm) {
  $bundle = $form_object->getEntity()->bundle();
  • 2
    Almost perfect, you should check if $form_object is an EntityFormInterface object. This will result in a fatal error on any other form like login, search and so on.
    – Berdir
    Aug 4, 2016 at 19:26
  • Thanks for your answer altough your answer was correct, but giving some fatal error like Berdir said. This works fine form me and no fatal errors. $bundle = FALSE; if ($form_object = $form_state->getFormObject()) { if ($form_object instanceof Drupal\Core\Entity\ContentEntityForm) { $bundle = $form_object->getEntity()->bundle(); } elseif ($form_object instanceof Drupal\field_ui\Form\FieldConfigEditForm) { $bundle = $form_object->getEntity()->bundle(); } }
    – Ajay Reddy
    Aug 5, 2016 at 5:40
  • I will edit right now! Thank you for the correction!
    – ssibal
    Aug 5, 2016 at 9:47

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.