I need to obtain the content type name inside an implementation of the hook_form_BASE_FORM_ID_alter() hook for the node edit/add form to made some validations.

I can't find any node object reference using kint() to access to the $node->getType() value.

I need to do a validation like this:

if ($node->getType() == 'article') {

So, how can I get the content type name inside the hook_form_BASE_FORM_ID_alter() hook?

  • What are you using for BASE_FORM_ID?
    – Jaypan
    Apr 6, 2017 at 4:34
  • @Jaypan is for the node edit/create page Apr 6, 2017 at 12:29

2 Answers 2


You can get the node from the form object.

use Drupal\Core\Form\FormStateInterface;

 * Implements hook_form_BASE_FORM_ID_alter().
function yourmodule_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /* @var \Drupal\node\NodeInterface $node */
  $node = $form_state->getFormObject()->getEntity();

  if (!$node->getType() == 'article') {
    // ....

Note that getEntity() doesn't exist on \Drupal\Core\Form\FormInterface, which is why it doesn't show up as a method in an IDE. It only exists on \Drupal\Core\Entity\EntityFormInterface, you can add an explicit instance of for that.

  • 1
    Thanks, I see now why I never found it, how can I add the interface? Apr 6, 2017 at 12:32
  • Hi you says in your answer: "you can add an explicit interface of for that." but how I can do it? Nov 1, 2018 at 21:32
  • 1
    That should have said instanceof, not interface os, aka if ($form_state instanceof EntityFormInterface), you can use that with a generic hook_form_alter() to identify/alter any entity form.
    – Berdir
    Nov 2, 2018 at 20:17
  • This works. Thank you so much, Berdir
    – Binny
    Apr 26, 2020 at 5:41

The only way that I found using the kint() output with the help of the Search Kint module to find the content type name is this:


Is working, but is not the best way to obtain the value.

if ($form['#process'][1][0]->getTargetBundle() == 'article') {

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.