Commit 322b1554 by Manzar Hussain

Merge branch 'feature-theme' into 'develop'

add new module

See merge request manzarH/dic-global-dev!440
parents b8f1f2e8 95099744
Provides a block type which renders views display exposed filters separately
from the view.
It's like [Views Block Exposed Filter Blocks]
(https://www.drupal.org/project/views_block_filter_block "Views Block Exposed Filter Blocks") module but works for all types of view display plugins (for example for [eva view displays](https://www.drupal.org/project/eva) which was what I needed) and solves the problem "the other way around". With this module you select the view and display with the exposed filters to render within the block configuration, not within the view.
If you only need exposed filters in blocks for a views block display plugin, I suggest to use https://www.drupal.org/project/views\_block\_filter_block or simply try out which of those two fits best.
Based on the implementations like: https://blog.werk21.de/en/2017/03/08/programmatically-render-exposed-filter-form or https://drupal.stackexchange.com/questions/236576/render-exposed-filter-without-creating-block Thanks to the authors!
Installation & use
------------------
1. Enable the module
2. Go to block layout (admin/structure/block)
3. Add a block of category "Views exposed filter blocks" - Simply click
"Place block" on the block administration page and search for
"Views exposed filter blocks". You may add as many of these blocks
as you need.
4. Select the view & display which holds the exposed filters
5. Place the block into the region where to display the exposed filters
and eventually configure display rules / paths.
6. Disable AJAX in the view you'd like to use (with ajax is untested)
7. Place block and result view on the same page so that the filter arguments
can be handled by the result view
Alternative modules
-------------------
* https://www.drupal.org/project/views\_block\_filter_block (Drupal 7 & 8 but only for views block displays)
Drupal 7
--------
This module will never have a Drupal 7 release.
Simply use the great https://www.drupal.org/project/views\_block\_filter_block
Development proudly sponsored by German Drupal Friends & Companies:
-------------------------------------------------------------------
[webks: websolutions kept simple](http://www.webks.de) (http://www.webks.de)
and [DROWL: Drupalbasierte Lösungen aus Ostwestfalen-Lippe](http://www.drowl.de) (http://www.drowl.de)
block.settings.views_exposed_filter_blocks_block:
type: block_settings
label: 'Views exposed filter block'
mapping:
view_display:
type: string
label: 'View & Display'
<?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;
/**
* Provides a separate views exposed filter block.
*
* @Block(
* id = "views_exposed_filter_blocks_block",
* admin_label = @Translation("Views exposed filter block")
* )
*/
class ViewsExposedFilterBlocksBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'view_display' => NULL,
];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['view_display'] = $form_state->getValue('view_display');
}
/**
* {@inheritdoc}
*/
public function blockValidate($form, FormStateInterface $form_state) {
$view_display = $form_state->getValue('view_display');
if (!empty($view_display)) {
// Check if the selected value is OK:
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 {
// Check if the view exists:
$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,
]));
}
}
}
}
/**
* {@inheritdoc}
*/
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);
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,
],
];
}
}
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
// Prevent the block from cached else the selected options will be cached.
return 0;
}
}
name: 'Views exposed filter blocks'
type: module
description: 'Provides separate exposed filter blocks for views which are not page views.'
core_version_requirement: ^8.8 || ^9
package: 'Views'
dependencies:
- drupal:views
# Information added by Drupal.org packaging script on 2020-06-08
version: '8.x-1.1'
project: 'views_exposed_filter_blocks'
datestamp: 1591636552
<?php
/**
* @file
* Module file for views_exposed_filter_blocks.
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment