What exactly is the best way to accomplish this? I can pass the $form
object returned from getForm
to drupal_render()
and get the correct themed output fragment but the API docs say this function will be deprecated in D9 and to use the renderer service instead.
When I use:
\Drupal::service('renderer')->render($form)
I get
You have requested a non-existent service "renderer". Did you mean one of these: "render_html_renderer", "html_fragment_renderer", "html_page_renderer"?
When I use:
\Drupal::service('render_html_renderer')->render($form)
I get a valid object with a protected content()
method, which contains the string, but I can't seem to find any valid public functions to access this.
I can normally use the route builder to point to a form controller that I've created but, in this instance, I just want to pull an existing form, get the HTML content, and display it - hopefully as a variable in a preprocess function. I don't necessarily want to create a block, since I probably won't be reusing it.
What is the best way to do this? Keep using drupal_render()
until D9? Can somebody point me in the right direction?
getContent()
method.