SearchApiPageBlock.php in Search API Pages 8
File
src/Plugin/Block/SearchApiPageBlock.php
View source
<?php
namespace Drupal\search_api_page\Plugin\Block;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\search_api_page\Form\SearchApiPageBlockForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SearchApiPageBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $searchApiPageBlockForm;
protected $searchApiPageStorage;
protected $formBuilder;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('search_api_page'), $container
->get('form_builder'), $container
->get('block_form.search_api_page'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $searchApiStorage, FormBuilderInterface $formBuilder, SearchApiPageBlockForm $searchApiPageForm) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->searchApiPageBlockForm = $searchApiPageForm;
$this->searchApiPageStorage = $searchApiStorage;
$this->formBuilder = $formBuilder;
}
public function blockForm($form, FormStateInterface $form_state) {
$options = [];
$search_api_pages = $this->searchApiPageStorage
->loadMultiple();
foreach ($search_api_pages as $search_api_page) {
$options[$search_api_page
->id()] = $search_api_page
->label();
}
$form['search_api_page'] = [
'#type' => 'select',
'#title' => $this
->t('Search page'),
'#default_value' => !empty($this->configuration['search_api_page']) ? $this->configuration['search_api_page'] : '',
'#description' => $this
->t('Select to which search page a submission of this form will redirect to'),
'#options' => $options,
'#required' => TRUE,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['search_api_page'] = $form_state
->getValue('search_api_page');
}
public function calculateDependencies() {
$search_api_page = $this->searchApiPageStorage
->load($this->configuration['search_api_page']);
if ($search_api_page === NULL) {
return [];
}
return [
'config' => [
$search_api_page
->getConfigDependencyName(),
],
];
}
public function build() {
$this->searchApiPageBlockForm
->setPageId($this->configuration['search_api_page']);
return \Drupal::formBuilder()
->getForm($this->searchApiPageBlockForm);
}
}