View source
<?php
namespace Drupal\views_exposed_filter_blocks\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Views;
class ViewsExposedFilterBlocksBlock extends BlockBase {
public function defaultConfiguration() {
return [
'view_display' => NULL,
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form['view_display'] = [
'#type' => 'select',
'#options' => Views::getViewsAsOptions(FALSE, 'enabled'),
'#title' => $this
->t('View') . ' & ' . $this
->t('Display'),
'#description' => nl2br($this
->t("Select the view and its display with the exposed filters to show in this block.\nYou should disable AJAX on the selected view and ensure the view and the filter are on the same page.\nFor view displays of type 'page' better use the view built-in functionality for exposed filters in blocks.")),
'#default_value' => $this->configuration['view_display'],
'#required' => TRUE,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['view_display'] = $form_state
->getValue('view_display');
}
public function blockValidate($form, FormStateInterface $form_state) {
$view_display = $form_state
->getValue('view_display');
if (!empty($view_display)) {
list($view_id, $display_id) = explode(':', $view_display);
if (empty($view_id) || empty($display_id)) {
$form_state
->setErrorByName('view_display', t('View or display coult not be determined correctly from the selected value.'));
}
else {
$view = Views::getView($view_id);
if (empty($view)) {
$form_state
->setErrorByName('view_display', t('View "%view_id" or its given display: "%display_id" doesn\'t exist. Please check the views exposed filter block configuration.', [
'%view_id' => $view_id,
'%display_id' => $display_id,
]));
}
}
}
}
public function build() {
$view_display = $this->configuration['view_display'];
if (!empty($view_display)) {
list($view_id, $display_id) = explode(':', $view_display);
if (empty($view_id) || empty($display_id)) {
return;
}
$view = Views::getView($view_id);
if (!empty($view)) {
$view
->setDisplay($display_id);
$view
->initHandlers();
$form_state = (new FormState())
->setStorage([
'view' => $view,
'display' => &$view->display_handler->display,
'rerender' => TRUE,
])
->setMethod('get')
->setAlwaysProcess()
->disableRedirect();
$form_state
->set('rerender', NULL);
$form = \Drupal::formBuilder()
->buildForm('\\Drupal\\views\\Form\\ViewsExposedForm', $form_state);
if ($view->display_handler
->getOption('link_display') == 'custom_url' && !empty($view->display_handler
->getOption('link_url'))) {
$form['#action'] = $view->display_handler
->getOption('link_url');
}
return $form;
}
else {
$error = $this
->t('View "%view_id" or its given display: "%display_id" doesn\'t exist. Please check the views exposed filter block configuration.', [
'%view_id' => $view_id,
'%display_id' => $display_id,
]);
\Drupal::logger('type')
->error($error);
return [
'#type' => 'inline_template',
'#template' => '{{ error }}',
'#context' => [
'error' => $error,
],
];
}
}
}
public function getCacheMaxAge() {
return 0;
}
}