SimpleFormBlock.php in Examples for Developers 8
File
form_api_example/src/Plugin/Block/SimpleFormBlock.php
View source
<?php
namespace Drupal\form_api_example\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\form_api_example\Form\SimpleForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SimpleFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $formBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $form_builder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('form_builder'));
}
public function build() {
$output = [
'description' => [
'#markup' => $this
->t('Using form provided by @classname', [
'@classname' => SimpleForm::class,
]),
],
];
$output['form'] = $this->formBuilder
->getForm(SimpleForm::class);
return $output;
}
}