Commit ca1639ff authored by marthinal's avatar marthinal
Browse files

#2660624 Show/hide Content type and Language filters when using facets.

parent 6904e340
......@@ -17,3 +17,4 @@ facets.facet_source.*:
url_processor:
type: string
label: 'Url processor'
third_party_settings: {}
......@@ -6,11 +6,11 @@
*/
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSourceInterface;
/**
* Implements hook_query_TAG_alter(): tag search_$type with $type node_search.
*
* @param \Drupal\Core\Database\Query\AlterableInterface $query
*/
function core_search_facets_query_search_node_search_alter(AlterableInterface $query) {
// Obtain the Facet Source id for the current search.
......@@ -28,24 +28,45 @@ function core_search_facets_query_search_node_search_alter(AlterableInterface $q
}
/**
* Implements hook_search_plugin_alter(). Alter search plugin definitions.
*
* @param array $definitions
* The array of search plugin definitions, keyed by plugin ID.
* Implements hook_search_plugin_alter().
*
* @see \Drupal\search\Annotation\SearchPlugin
* @see \Drupal\search\SearchPluginManager
* Alter search plugin definitions.
*/
function core_search_facets_search_plugin_alter(array &$definitions) {
// Replace the Search Plugin class to alter the search form on a different
// way because we don't need content types or languages as advanced search
// filters.
// @TODO wait until we can add configuration to a facet source.
// Replace the Search Plugin class to alter the search form adding the
// possibility to show or hide the Content Type and Language advanced filters.
if (isset($definitions['node_search'])) {
//$definitions['node_search']['class'] = 'Drupal\core_search_facets\Plugin\Search\NodeSearchFacets';
$definitions['node_search']['class'] = 'Drupal\core_search_facets\Plugin\Search\NodeSearchFacets';
}
}
/**
* Implements hook_form_FORM_ID_alter() for facet_source_edit_form.
*
* Adds a checkbox to show or hide some advanced filters for the core search.
*/
function core_search_facets_form_facet_source_edit_form_alter(&$form, FormStateInterface $form_state) {
$request = \Drupal::requestStack()->getMasterRequest();
$facet_source_id = str_replace(":", "__", $request->attributes->get('source_id'));
if (strpos($facet_source_id, 'core_node_search') !== FALSE) {
$form['advanced_filters'] = array(
'#type' => 'checkbox',
'#title' => t('Show Advanced Filters.'),
'#default_value' => \Drupal::config("facets.facet_source.{$facet_source_id}")->get('third_party_settings.core_search_facets.advanced_filters'),
);
$form['#entity_builders'][] = 'core_search_facets_facet_source_form_form_builder';
}
}
/**
* Entity builder for the facet source form edit form with third party options.
*
* @see core_search_facets_form_facet_source_edit_form_alter()
*/
function core_search_facets_facet_source_form_form_builder($entity_type, FacetSourceInterface $facet_source, &$form, FormStateInterface $form_state) {
$facet_source->setThirdPartySetting('core_search_facets', 'advanced_filters', $form_state->getValue('advanced_filters'));
}
/**
* Implements hook_facets_core_allowed_field_types().
*/
......
......@@ -7,72 +7,15 @@
namespace Drupal\core_search_facets\Plugin\Search;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\node\Plugin\Search\NodeSearch;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Handles searching for node entities using the Search module index.
*/
class NodeSearchFacets extends NodeSearch {
protected $facetSource;
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Connection $database,
EntityTypeManagerInterface $entity_manager,
ModuleHandlerInterface $module_handler,
Config $search_settings,
LanguageManagerInterface $language_manager,
RendererInterface $renderer,
FacetSourcePluginManager $facet_source_plugin_manager,
RequestStack $request_stack,
AccountInterface $account = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $database, $entity_manager, $module_handler, $search_settings, $language_manager, $renderer, $account);
/** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
if ($search_page = $request_stack->getMasterRequest()->attributes->get('entity')) {
/** @var \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager */
$this->facetSource = $facet_source_plugin_manager->createInstance('core_node_search:' . $search_page->id());
}
}
/**
* {@inheritdoc}
*/
static public function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('config.factory')->get('search.settings'),
$container->get('language_manager'),
$container->get('renderer'),
$container->get('plugin.manager.facets.facet_source'),
$container->get('request_stack'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
......@@ -141,8 +84,7 @@ class NodeSearchFacets extends NodeSearch {
'#weight' => 100,
);
// Only add node types and language filters when a facet source has facets.
if (!$this->facetSource->hasFacets()) {
if (\Drupal::config("facets.facet_source.core_node_search__{$this->searchPageId}")->get('third_party_settings.core_search_facets.advanced_filters')) {
// Add node types.
$types = array_map(array('\Drupal\Component\Utility\Html', 'escape'), node_type_get_names());
$form['advanced']['types-fieldset'] = array(
......
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