NormalBlock.php in Visually Impaired Support (module) 8
File
src/Plugin/Block/NormalBlock.php
View source
<?php
namespace Drupal\visually_impaired_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NormalBlock 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() {
$form = $this->formBuilder
->getForm('Drupal\\visually_impaired_module\\Form\\VINormalForm');
$classes = [];
$this->configuration['block_style'] ? $classes[] = 'vi-normal-block-image' : ($classes[] = 'vi-normal-block-text');
$form['visually-impaired-normal-block']['#attributes']['class'] = $classes;
return $form;
}
public function defaultConfiguration() {
return [
'block_style' => 1,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$active = [
0 => $this
->t('Text'),
1 => $this
->t('Image'),
];
$form['block_style'] = [
'#type' => 'radios',
'#title' => $this
->t('Block style'),
'#required' => TRUE,
'#options' => $active,
'#default_value' => $this->configuration['block_style'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['block_style'] = $form_state
->getValue('block_style');
}
}