class SimpleFormBlock
Same name in other branches
- 3.x modules/form_api_example/src/Plugin/Block/SimpleFormBlock.php \Drupal\form_api_example\Plugin\Block\SimpleFormBlock
- 4.0.x modules/form_api_example/src/Plugin/Block/SimpleFormBlock.php \Drupal\form_api_example\Plugin\Block\SimpleFormBlock
Provides a 'Example: Display a form' block.
This example demonstrates the use of the form_builder service, an instance of \Drupal\Core\Form\FormBuilder, in order to retrieve and display a form.
Plugin annotation
@Block(
id = "form_api_example_simple_form_block",
admin_label = @Translation("Example: Display a form")
)
Hierarchy
- class \Drupal\form_api_example\Plugin\Block\SimpleFormBlock extends \Drupal\Core\Block\BlockBase implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface
Expanded class hierarchy of SimpleFormBlock
File
-
form_api_example/
src/ Plugin/ Block/ SimpleFormBlock.php, line 23
Namespace
Drupal\form_api_example\Plugin\BlockView source
class SimpleFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Form builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('form_builder'));
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
SimpleFormBlock::$formBuilder | protected | property | Form builder service. |
SimpleFormBlock::build | public | function | |
SimpleFormBlock::create | public static | function | |
SimpleFormBlock::__construct | public | function |