You are here

public function SimpleFormBlock::build in Examples for Developers 8

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

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

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;
}