Why Won’t If “Drupal\node\NodeForm” Work?

PROBLEM
I need to determine what kind of object a form is. When I dpm the class, it says Drupal\node\NodeForm . So, the following should work:

if (get_class($form_state->getFormObject()) == "Drupal\node\NodeForm") {

But it does not! I’ve been forced to do stupid things like this:

if (get_class($form_state->getFormObject()) != "Drupal\node\Form\DeleteMultiple") {

SOLUTION
DUH MOMENT! I finally figured it out today. VS code highlighted this out of the above first line of code:

\n

DUH! PHP is reading this as a new line! Wow.

New code that works is:

if (get_class($form_state->getFormObject()) == "Drupal\\node\NodeForm") {

This does make sense now that I think about it!

Leave a Reply

Your email address will not be published.