Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. 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.
  2.  
  3. As a contrived example, that could be `'\Drupal\foo\Form\BarForm'`. Note the quotes, meaning it's a string.
  4.  
  5. So to load a form, you would use this (again pay attention to the quotes):
  6.  
  7. $form = \Drupal::formBuilder()->getForm('\Drupal\foo\Form\BarForm');
  8.  
  9. 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:
  10.  
  11. $form = \Drupal::formBuilder()->getForm(\Drupal\foo\Form\BarForm::class);
  12.  
  13. Notice the absence of quotes this time, as `::class` returns the string we're looking for.
  14.  
  15. 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement