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

0 Answered Questions

How many different ways are there to create a "page"

  • 2019-04-23 16:14:58
  • Burton Kent
  • 25 View
  • 0 Score
  • 0 Answer
  • Tags:   8

2 Answered Questions

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

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

0 Answered Questions

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
  • 1330 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
  • 120 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?

0 Answered Questions

Get a list of custom form subclasses in a namespace

  • 2016-09-06 10:53:28
  • Storsey
  • 25 View
  • 0 Score
  • 0 Answer
  • Tags:   forms 8

1 Answered Questions

[SOLVED] Custom block template

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

0 Answered Questions

How can I use user input to build the URL of a GET form submission?

  • 2012-07-25 00:01:37
  • ldweeks
  • 259 View
  • 0 Score
  • 0 Answer
  • Tags:   7 forms

Sponsored Content