Display results of a form I am trying to find a simple way to display the results of a simple checkbox form on the same page after submit. I know you can use the drupal_set_message, but I want to display the results formatted on the page, not in the message box. 

public function submitForm(array &amp;$form, FormStateInterface $form_state) {
        drupal_set_message($this-&gt;t('Your favorite car is @fav_car', array('@fav_car' =&gt; $form_state-&gt;getValue('fav_car'))));
      }
    }


Full code for form here;



/**
 * @file
 * Contains \Drupal\cars\Form\FavoriteCarForm.
 */

namespace Drupal\cars\Form:

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\cars\CarsStorage;

/**
 * Class FavoriteCarForm.
 *
 * @package Drupal\cars\Form
 */
class FavoriteCarForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'favorite_car_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['fav_car'] = array(
      '#type' =&gt; 'radios',
      '#title' =&gt; $this-&gt;t('What is your favorite Car!'),
      '#options' =&gt; array('Toyota' =&gt; $this-&gt;t('toyota'), 'Ford' =&gt; $this-&gt;t('Ford'), 'Subaru' =&gt; $this-&gt;t('Subaru')),
    );

    $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Submit'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    drupal_set_message($this-&gt;t('Your favorite car is @fav_car', array('@fav_car' =&gt; $form_state-&gt;getValue('fav_car'))));
  }
}
This is not different from what is done in Drupal 7:


When building the form, you verify in $form_state a value is present; if it is present you show its value
In the submission form handler, you tell Drupal to rebuild it


What changes is the code you actually use, since Drupal 8 introduces new classes Drupal 7 doesn't have.

class FavoriteCarForm extends FormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'favorite_car_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['fav_car'] = array(
      '#type' =&gt; 'radios',
      '#title' =&gt; $this-&gt;t('What is your favorite Car!'),
      '#options' =&gt; array('Toyota' =&gt; $this-&gt;t('toyota'), 'Ford' =&gt; $this-&gt;t('Ford'), 'Subaru' =&gt; $this-&gt;t('Subaru')),
    );

    if ($form_state-&gt;getValue('fav_car')) {
      // Show the value the user entered.
    }

    $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Submit'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $form_state-&gt;setRebuild();
  }
}


Calling $form_state-&gt;setRebuild() causes Drupal to call buildForm() again, passing in $form_state the values the user submitted and the values added from validation and submission handlers.

I didn't add the code to write the submitted value. You could:


Show it as description of the form element
Show it in a #markup element


I would rather not implement such code for just showing the selected value, though. I would use it to show the result of an elaboration done from the input given from users, for example showing a map of the town entered from users.