Form in a block with results is the same block I have a custom module that uses a form in a block that will be displayed on a node page. This form is used to retrieve datas from a custom table.
What I want to achieve is :


The user arrives on the page : the form is displayed and the 10 lasts items retrieved from the database are also displayed
The user selects a data via the form
The user submits the form
The node page containing the block is refresehed with the 10 new items retrieved from database. I also need the form to be displayed on top of the block with previously selected values selected


I've been able to create a form class extending the FormBase class. Then I've created a block extending BlockBase class. In the build method I  get the form using the Drupal::formBuilder()->getForm() method

public function build(){

$form = \Drupal::formBuilder()-&gt;getForm('Drupal\my_module\Form\my_form');

  return array(
      '#theme' =&gt; 'my_theme',
      '#form' =&gt; $form,
    );
}


At that step, my node page + the block containing the form is correctly displayed, but I've not been able to render the default 10 last items. I've no idea on how to achieve this since the buildForm method of the form class is supposed to return an array to render the form. I've tried to implement it in the build method of my block :

return array(
      '#theme' =&gt; 'my_theme',
      '#items' =&gt; $items,
      '#form' =&gt; $form,
    );


It works, but after form submission, those first 10 default items are always displayed because the block build them on every refresh. Is there a way to know if the included form has been submitted ?

Concerning the form validation, redirection is definitly not the way I should go since a need the results of the database query to be displayed in the same page / block.

Based on this topic, I feel the $form_state->setRebuild() / $form_state->isRebuilding() methods are the way to go. Do you confirm ?

I've been able to refresh the item list after form submission doing so : 

  if ($form_state-&gt;isRebuilding()) {
  $myfield_value = $form_state-&gt;getValue('myfield ');

  $model = Drupal::service('my_module.model');
  $items = $model-&gt;getLastItems(10, $myfield_value );

  return array(
    '#theme' =&gt; 'my_theme',
    '#form' =&gt; $form,
    '#items' =&gt; $items,
  );


} 

This works, but only half of the time the form is submitted. The second time it's like it never passes in the if($form_state->isRebuilding()) {} statement. Moreover, the results are nested in the form markup. Maybe it's not the proper way to inject the form...

For information here is an example of my twig markup : 

{{ form }}
&lt;div class="row"&gt;
    {% for track in tracks %}
        {# print stuff...#}
    {% endfor %}
&lt;/div&gt;


To be able to inject the form in the if($form_state->isRebuilding()) statement of the buildForm of the form class, I must wait the form array is completly defined in code... Doing so, I'm unable to set fields values to the ones the user has submitted. 

In brief, my questions are :


How can I populate the default items list before the form is submitted ?
How can I properly inject the form from the if($form_state->isRebuilding() statement, so that everything works
How can I set the fields selected values after form submission.


Sorry for this long post, but I'm quite new to the drupal ecosystem, so any help would be greatly appreciated !...

Cheers

PS : I've also read about the BlockBase::blockForm and BlockBase::blockSubmit methods, but as far as I understand, it's more about block configuration froms. Does somebody confirm that ?
Thanks 4k4, the main point I was missing here is that a form is just a specific form of render array... I though it was only possible to define form elements to be rendered. But as you pointed me out, it's possible to define anything one wants to render, like... form result... Since what I need to render is a bit more complicated than simple markup, I've created a new template in my .module file :

function my_module_theme() {
return array(
'my_module_form' =&gt; array(
  'variables' =&gt; array(
    'form' =&gt; array(),
  ),
'template' =&gt; 'my-module-form',
),
 'my_module_results' =&gt; array( 
  'variables' =&gt; array(
    'tracks' =&gt; array(),
  ),
 }


And then, my buildForm method looks approximately that way : 

public function buildForm(array $form, FormStateInterface $form_state) {
// define default values...
$defaultValue = 3;

// define form fied + submit button...
 $form['my_field'] = array(
  '#type' =&gt; 'select',
  '#required' =&gt; true,
  '#default_value' =&gt; $defaultValue,
  '#options' =&gt; array(1 =&gt; 1, 2 =&gt; 2, 3 =&gt; 3)
);

$form['submit'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; 'Go'
);

// define field default value that will be overwritten whe  form is submitted 
$my_field_value = 1;


//get submitted value
if ($form_state-&gt;isRebuilding()){
    $my_field_value =  $form_state-&gt;getValue('my_field');
}

//get datas from database
$results = $model-&gt;getResultFromDb($my_field);

 $form['result'] = array(
    '#theme' =&gt; 'my_module_results',
    '#results' =&gt; $results,
    );

return $form;
}


Then is the "my_module_results" you can use the $results var as in any other template : 

&lt;ul&gt;
  {% for result in results %}
  &lt;li&gt; {{ result.title }}&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;


For the results not to be nested in the "form" markup, I had to build manually the form in its own template and call the result var where I needed the result template to be inserted : 

&lt;div id="form"&gt;
    &lt;form method="{{ form['#method'] }}"&gt;
        {{ form.form_build_id }}
        {{ form.form_token }}
        {{ form.form_id }}
        {{ form.my_field }}
        {{ form.submit }}
    &lt;/form&gt;             
&lt;/div&gt;
&lt;div id="results"&gt;
    {{ form.result }}
&lt;/div&gt;


Not sure it wes the only way to do that, but it works like a charm...

Hope this would help somebody...