By Joseph


2019-07-10 12:42:10 8 Comments

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?

2 comments

@Sébastien Gicquel 2019-07-10 13:38:35

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.

@Jdrupal 2019-07-10 13:07:42

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;

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How do I return a Twig template inside the build method of a block class?

  • 2019-09-13 10:06:37
  • user agent
  • 47 View
  • 2 Score
  • 1 Answer
  • Tags:   8 theming blocks

2 Answered Questions

[SOLVED] How to alter a form element's properties before rendering?

  • 2017-03-10 10:21:15
  • Gaetan Pralong
  • 7376 View
  • 5 Score
  • 2 Answer
  • Tags:   8 forms

1 Answered Questions

How do I capture a page as HTML?

3 Answered Questions

[SOLVED] How do I programmatically disable a form?

  • 2016-12-04 14:41:31
  • user2137454
  • 1687 View
  • 1 Score
  • 3 Answer
  • Tags:   forms 8

1 Answered Questions

[SOLVED] Form in a block with results is the same block

  • 2017-03-09 14:59:19
  • Kap
  • 127 View
  • 1 Score
  • 1 Answer
  • Tags:   forms blocks

2 Answered Questions

[SOLVED] How can I add a custom form to the output of a controller class?

1 Answered Questions

[SOLVED] Custom block template

  • 2015-11-19 01:03:46
  • Andy
  • 774 View
  • 1 Score
  • 1 Answer
  • Tags:   blocks 8

Sponsored Content