1

In Drupal 8, the following code renders the node creation form (node/add/page) when integrating it into a custom module:

$node = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->create(['type' => 'page']);

$form = \Drupal::service('entity.form_builder')->getForm($node);

print \Drupal::service('renderer')->render($form);

We know the module in question is the Node module, which generates the content type page. What should I do to render the user creation form (/admin/people/create)?

I can easily change the code to the following, and replace 'node' with 'user' and 'page' with 'user'.

  ->getStorage('user')
  ->create(['type' => 'user']);

This would now render the user creation form. For me to come to the conclusion that it would render the user form was only a guess and apparently it works. But this problem solving approach will not work if I wanted to render some other form that might not be so obvious.

What does one have to grep for in the code base or other investigative work to come to the conclusion of the following 2 lines in the above code?

9
  • I stand by my previous comment, it applies directly to this question too. By the time you know what to grep for, you'll already have more than enough context to tell you what class defines the form, making the grep redundant
    – Clive
    Aug 4, 2016 at 19:01
  • Ok, taking your stance of by the time you know what to grep for, you'll already have more than enough context... Then before you even grep it, you still have to compile all the hints from something. That something is the thing I want to know. Even if it requires 20 other steps before it to conclude on what to grep for. Reason why is being able to debug a form, you'd first have to know where it came from. How does a developer know where it came from? Does one use devel? Or does one just look for something in the html source? I'm trying to take a step by step approach to understanding this. Aug 4, 2016 at 19:58
  • I've already answered thst exact question from you 3 or more times now. Not doing it again, sorry, I don't have another way to phrase it :)
    – Clive
    Aug 4, 2016 at 20:00
  • :( I just don't agree that there is absolutely no way for someone coming to D8 to not be able to debug a form by saying there are absolutely no universal steps into debugging this. It could even be as simple as grep for the label of the input box. That could be one universal method. No worries Clive!!! I'll figure it out.... but your hints have given me a lead at least. I just want to understand this system better before pushing it to clients. Thanks for everything thus far. Aug 4, 2016 at 20:05
  • 1
    Happy to help - and also more than happy to be proven wrong by someone!
    – Clive
    Aug 4, 2016 at 20:07

1 Answer 1

1

To answer your explicit question (and to reiterate what Clive said), there's no one thing that you could grep to conclude the arguments to change for the getStorage() & create() methods.

The above solution assumes:

  1. You're looking for an entity form.
  2. You're aware that EntityFormBuilderInterface::getForm() needs an entity object.
  3. You can build an entity object with Drupal::entityTypeManager().

It's a specific solution to building entity forms. If you're looking for a method to find any arbitrary constructed form, this solution isn't going to help here.

The most generic thing one could search for to find such a form (an invoke it for rendering), is probably a label that is unique to the custom form, but it really depends on how the form is constructed (e.g. EntityFormBuilderInterface::getForm(), FormBuilderInterface::getForm(), or some arbitrary form array builder function).

If you're familiar with the pattern of construction, there might be some specific terms to search for (e.g. searching for classes implementing FormBuilderInterface or EntityFormBuilderInterface), but there's not going to be one search pattern that works for all forms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.