- Firstly, you might be confused about what `FormBuilder::getForm` is expecting as a parameter. It's looking for a string representation of the class name, fully qualified with namespaces.
- As a contrived example, that could be `'\Drupal\foo\Form\BarForm'`. Note the quotes, meaning it's a string.
- So to load a form, you would use this (again pay attention to the quotes):
- $form = \Drupal::formBuilder()->getForm('\Drupal\foo\Form\BarForm');
- Now, since PHP 5.5, classes have an implicit static `::class` method, which returns the fully-qualified class name as a string. So the above can become:
- $form = \Drupal::formBuilder()->getForm(\Drupal\foo\Form\BarForm::class);
- Notice the absence of quotes this time, as `::class` returns the string we're looking for.
- The second method is better, as it's less prone to error. You'll get a fatal error from the interpreter if you reference an invalid class, rather than a runtime error if you use a string.
RAW Paste Data