function SimpleFormBlock::build

Same name in other branches
  1. 3.x modules/form_api_example/src/Plugin/Block/SimpleFormBlock.php \Drupal\form_api_example\Plugin\Block\SimpleFormBlock::build()
  2. 4.0.x modules/form_api_example/src/Plugin/Block/SimpleFormBlock.php \Drupal\form_api_example\Plugin\Block\SimpleFormBlock::build()

File

form_api_example/src/Plugin/Block/SimpleFormBlock.php, line 55

Class

SimpleFormBlock
Provides a 'Example: Display a form' block.

Namespace

Drupal\form_api_example\Plugin\Block

Code

public function build() {
    $output = [
        'description' => [
            '#markup' => $this->t('Using form provided by @classname', [
                '@classname' => SimpleForm::class,
            ]),
        ],
    ];
    // Use the form builder service to retrieve a form by providing the full
    // name of the class that implements the form you want to display. getForm()
    // will return a render array representing the form that can be used
    // anywhere render arrays are used.
    //
    // In this case the build() method of a block plugin is expected to return
    // a render array so we add the form to the existing output and return it.
    $output['form'] = $this->formBuilder
        ->getForm(SimpleForm::class);
    return $output;
}