SuggestionBlock.php in Autocomplete Search Suggestions 8
File
src/Plugin/Block/SuggestionBlock.php
View source
<?php
namespace Drupal\suggestion\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\suggestion\Form\SuggestionBlockForm;
use Drupal\suggestion\SuggestionHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SuggestionBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $formBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $formBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $formBuilder;
}
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 defaultConfiguration() {
return [
'action' => SuggestionHelper::getConfig('action'),
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form['action'] = [
'#title' => $this
->t('Action'),
'#description' => $this
->t('The form action.'),
'#type' => 'textfield',
'#default_value' => SuggestionHelper::getConfig('action'),
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
SuggestionHelper::setConfig('action', $form_state
->getValue('action'));
}
public function build() {
return $this->formBuilder
->getForm(SuggestionBlockForm::class);
}
}