1

What I'd like to do is build a page with two things:

  • On one half of the page, custom markup with data from Drupal.
  • On the other half, a custom form also submitted to Drupal.

There are a lot of tutorials on how to programmatically build pages in D8 (create route, extend controller, return render array, etc.). There are also a lot of tutorials on how to programmatically build forms on D8 (create route, extend form base, return render array, etc.). However, I could not find any resource on how to build both onto the same page.

How would I go about doing this?

  • Jdrupal has the answer below. Additionally, if you are rendering the form using FormBuilder, you don't need to create a route for it. – Charles Bamford Jul 10 at 17:15
2

You must use the formBuilder, like this:

$render = [];
$render['elememt1'] = [
  '#type' => 'markup',
  '#markup' => t('Test'),
];
$render['element2'] = \Drupal::formBuilder()->getForm('Drupal\MY_MODULE\Form\MyFormClass');// The destination of your form.

return $render;
1

Not sure if it fits to your project.

As a themer, I would create a form with Form Api in a custom module.

The I would create a custom block for this form :

src/Plugin/Block/HelloBlock.php

See here for a example :https://www.drupal.org/docs/8/creating-custom-modules/creating-custom-blocks/create-a-custom-block

I would then create a twig template for the page (what you call "custom markup with data from Drupal"). And Then I would integrate the custom block with the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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