diff --git a/modules/islandora_advanced_search/js/facets/facets-views-ajax.js b/modules/islandora_advanced_search/js/facets/facets-views-ajax.js index 9ed6d965..0b04ea33 100644 --- a/modules/islandora_advanced_search/js/facets/facets-views-ajax.js +++ b/modules/islandora_advanced_search/js/facets/facets-views-ajax.js @@ -71,7 +71,8 @@ } }); - /*** Push state on form/pager/facet change. + /** + * Push state on form/pager/facet change. */ Drupal.behaviors.islandoraAdvancedSearchViewsAjax = { attach: function (context, settings) { diff --git a/modules/islandora_advanced_search/js/islandora_advanced_search.form.js b/modules/islandora_advanced_search/js/islandora_advanced_search.form.js index 70c2eae8..7b068bb4 100644 --- a/modules/islandora_advanced_search/js/islandora_advanced_search.form.js +++ b/modules/islandora_advanced_search/js/islandora_advanced_search.form.js @@ -18,6 +18,9 @@ } // Remove Recurse parameter. delete params[recurse_parameter]; + // Remove the page if set as submitting the form should always take + // the user to the first page (facets do the same). + delete params["page"]; return params; } @@ -93,7 +96,7 @@ // Prevent form submission and push state instead. // // Logic server side / client side should match to generate the - // approprirate URL with javascript enabled or disable. + // appropriate URL with javascript enabled or disable. $form.submit(function (e) { e.preventDefault(); e.stopPropagation(); diff --git a/modules/islandora_advanced_search/src/Plugin/Block/AdvancedSearchBlockDeriver.php b/modules/islandora_advanced_search/src/Plugin/Block/AdvancedSearchBlockDeriver.php index fdb7205c..d4441e92 100644 --- a/modules/islandora_advanced_search/src/Plugin/Block/AdvancedSearchBlockDeriver.php +++ b/modules/islandora_advanced_search/src/Plugin/Block/AdvancedSearchBlockDeriver.php @@ -2,90 +2,16 @@ namespace Drupal\islandora_advanced_search\Plugin\Block; -use Drupal\Component\Plugin\PluginBase; -use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use Symfony\Component\DependencyInjection\ContainerInterface; - /** - * This deriver creates a block for every search_api.display. + * Deriver for AdvancedSearchBlock. */ -class AdvancedSearchBlockDeriver implements ContainerDeriverInterface { - - use StringTranslationTrait; - - /** - * List of derivative definitions. - * - * @var array - */ - protected $derivatives = []; - - /** - * The entity storage for the view. - * - * @var \Drupal\Core\Entity\EntityStorageInterface - */ - protected $storage; - - /** - * The display manager for the search_api. - * - * @var \Drupal\search_api\Display\DisplayPluginManager - */ - protected $displayPluginManager; - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, $base_plugin_id) { - $deriver = new static($container, $base_plugin_id); - $deriver->storage = $container->get('entity_type.manager')->getStorage('view'); - $deriver->displayPluginManager = $container->get('plugin.manager.search_api.display'); - return $deriver; - } +class AdvancedSearchBlockDeriver extends SearchApiDisplayBlockDeriver { /** * {@inheritdoc} */ - public function getDerivativeDefinition($derivative_id, $base_plugin_definition) { - $derivatives = $this->getDerivativeDefinitions($base_plugin_definition); - return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL; - } - - /** - * {@inheritdoc} - */ - public function getDerivativeDefinitions($base_plugin_definition) { - $base_plugin_id = $base_plugin_definition['id']; - - if (!isset($this->derivatives[$base_plugin_id])) { - $plugin_derivatives = []; - - foreach ($this->displayPluginManager->getDefinitions() as $display_definition) { - $view_id = $display_definition['view_id']; - $view_display = $display_definition['view_display']; - // The derived block needs both the view / display identifiers to - // construct the pager. - $machine_name = "${view_id}__${view_display}"; - - /** @var \Drupal\views\ViewEntityInterface $view */ - $view = $this->storage->load($view_id); - $display = $view->getDisplay($view_display); - - $plugin_derivatives[$machine_name] = [ - 'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name, - 'label' => $this->t('Advanced Search'), - 'admin_label' => $this->t(':view: Advanced Search for :display', [ - ':view' => $view->label(), - ':display' => $display['display_title'], - ]), - ] + $base_plugin_definition; - } - - $this->derivatives[$base_plugin_id] = $plugin_derivatives; - } - return $this->derivatives[$base_plugin_id]; + protected function label() { + return $this->t('Advanced Search'); } } diff --git a/modules/islandora_advanced_search/src/Plugin/Block/SearchApiDisplayBlockDeriver.php b/modules/islandora_advanced_search/src/Plugin/Block/SearchApiDisplayBlockDeriver.php new file mode 100644 index 00000000..e1572e89 --- /dev/null +++ b/modules/islandora_advanced_search/src/Plugin/Block/SearchApiDisplayBlockDeriver.php @@ -0,0 +1,97 @@ +storage = $container->get('entity_type.manager')->getStorage('view'); + $deriver->displayPluginManager = $container->get('plugin.manager.search_api.display'); + return $deriver; + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinition($derivative_id, $base_plugin_definition) { + $derivatives = $this->getDerivativeDefinitions($base_plugin_definition); + return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL; + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $base_plugin_id = $base_plugin_definition['id']; + + if (!isset($this->derivatives[$base_plugin_id])) { + $plugin_derivatives = []; + + foreach ($this->displayPluginManager->getDefinitions() as $display_definition) { + $view_id = $display_definition['view_id']; + $view_display = $display_definition['view_display']; + // The derived block needs both the view / display identifiers to + // construct the pager. + $machine_name = "${view_id}__${view_display}"; + + /** @var \Drupal\views\ViewEntityInterface $view */ + $view = $this->storage->load($view_id); + $display = $view->getDisplay($view_display); + + $plugin_derivatives[$machine_name] = [ + 'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name, + 'label' => $this->label(), + 'admin_label' => $this->t(':view: :label for :display', [ + ':view' => $view->label(), + ':label' => $this->label(), + ':display' => $display['display_title'], + ]), + ] + $base_plugin_definition; + } + + $this->derivatives[$base_plugin_id] = $plugin_derivatives; + } + return $this->derivatives[$base_plugin_id]; + } + +} diff --git a/modules/islandora_advanced_search/src/Plugin/Block/SearchResultsPagerBlockDeriver.php b/modules/islandora_advanced_search/src/Plugin/Block/SearchResultsPagerBlockDeriver.php index e4e0bbad..c1378fae 100644 --- a/modules/islandora_advanced_search/src/Plugin/Block/SearchResultsPagerBlockDeriver.php +++ b/modules/islandora_advanced_search/src/Plugin/Block/SearchResultsPagerBlockDeriver.php @@ -2,90 +2,16 @@ namespace Drupal\islandora_advanced_search\Plugin\Block; -use Drupal\Component\Plugin\PluginBase; -use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use Symfony\Component\DependencyInjection\ContainerInterface; - /** * This deriver creates a block for every search_api.display. */ -class SearchResultsPagerBlockDeriver implements ContainerDeriverInterface { - - use StringTranslationTrait; - - /** - * List of derivative definitions. - * - * @var array - */ - protected $derivatives = []; - - /** - * The entity storage used for the view. - * - * @var \Drupal\Core\Entity\EntityStorageInterface - */ - protected $storage; - - /** - * The display manager for the search_api. - * - * @var \Drupal\search_api\Display\DisplayPluginManager - */ - protected $displayPluginManager; - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, $base_plugin_id) { - $deriver = new static($container, $base_plugin_id); - $deriver->storage = $container->get('entity_type.manager')->getStorage('view'); - $deriver->displayPluginManager = $container->get('plugin.manager.search_api.display'); - return $deriver; - } +class SearchResultsPagerBlockDeriver extends SearchApiDisplayBlockDeriver { /** * {@inheritdoc} */ - public function getDerivativeDefinition($derivative_id, $base_plugin_definition) { - $derivatives = $this->getDerivativeDefinitions($base_plugin_definition); - return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL; - } - - /** - * {@inheritdoc} - */ - public function getDerivativeDefinitions($base_plugin_definition) { - $base_plugin_id = $base_plugin_definition['id']; - - if (!isset($this->derivatives[$base_plugin_id])) { - $plugin_derivatives = []; - - foreach ($this->displayPluginManager->getDefinitions() as $display_definition) { - $view_id = $display_definition['view_id']; - $view_display = $display_definition['view_display']; - // The derived block needs both the view / display identifiers - // to construct the pager. - $machine_name = "${view_id}__${view_display}"; - - /** @var \Drupal\views\ViewEntityInterface $view */ - $view = $this->storage->load($view_id); - $display = $view->getDisplay($view_display); - - $plugin_derivatives[$machine_name] = [ - 'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name, - 'label' => $this->t('Search Results Pager'), - 'admin_label' => $this->t(':view: Pager for :display', [ - ':view' => $view->label(), - ':display' => $display['display_title'], - ]), - ] + $base_plugin_definition; - } - - $this->derivatives[$base_plugin_id] = $plugin_derivatives; - } - return $this->derivatives[$base_plugin_id]; + protected function label() { + return $this->t('Search Results Pager'); } } diff --git a/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowActiveExcludedFacets.php b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowActiveExcludedFacets.php index 20e978a6..cef77592 100644 --- a/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowActiveExcludedFacets.php +++ b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowActiveExcludedFacets.php @@ -2,11 +2,10 @@ namespace Drupal\islandora_advanced_search\Plugin\facets_summary\processor; -use Drupal\Core\Link; -use Drupal\Core\Url; use Drupal\facets_summary\FacetsSummaryInterface; use Drupal\facets_summary\Processor\BuildProcessorInterface; use Drupal\facets_summary\Processor\ProcessorPluginBase; +use Drupal\facets\FacetInterface; /** * Provides a processor that shows the search query. @@ -22,50 +21,27 @@ use Drupal\facets_summary\Processor\ProcessorPluginBase; */ class ShowActiveExcludedFacets extends ProcessorPluginBase implements BuildProcessorInterface { + use ShowFacetsTrait; + + /** + * {@inheritdoc} + */ + protected function condition(FacetInterface $facet) { + return $facet->getExclude(); + } + + /** + * {@inheritdoc} + */ + protected function classes() { + return ['facet-summary-item--facet', 'facet-summary-item--exclude']; + } + /** * {@inheritdoc} */ public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) { - $request = \Drupal::request(); - $query_params = $request->query->all(); - foreach ($facets as $facet) { - if ($facet->getExclude()) { - $url_alias = $facet->getUrlAlias(); - $filter_key = $facet->getFacetSourceConfig()->getFilterKey() ?: 'f'; - $active_items = $facet->getActiveItems(); - foreach ($active_items as $active_item) { - $url = Url::createFromRequest($request); - $modified_query_params = $query_params; - $modified_query_params[$filter_key] = array_filter($query_params[$filter_key], function ($query_param) use ($url_alias, $active_item) { - $pos = strpos($query_param, ':'); - $alias = substr($query_param, 0, $pos); - $value = substr($query_param, $pos + 1); - return !($alias == $url_alias && $value == $active_item); - }); - $url->setOption('query', $modified_query_params); - $item = [ - '#theme' => 'facets_result_item__summary', - '#value' => $active_item, - // We do not have counts for excluded facets... - '#show_count' => FALSE, - // Do not know the count. - '#count' => 0, - '#is_active' => TRUE, - '#facet' => $facet, - '#raw_value' => $active_item, - ]; - $item = (new Link($item, $url))->toRenderable(); - $item['#wrapper_attributes'] = [ - 'class' => [ - 'facet-summary-item--facet', - 'facet-summary-item--exclude', - ], - ]; - $build['#items'][] = $item; - } - } - } - return $build; + return $this->buildHelper($build, $facets); } } diff --git a/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowFacetsTrait.php b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowFacetsTrait.php new file mode 100644 index 00000000..a3ceacab --- /dev/null +++ b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowFacetsTrait.php @@ -0,0 +1,67 @@ +query->all(); + foreach ($facets as $facet) { + if ($this->condition($facet)) { + $url_alias = $facet->getUrlAlias(); + $filter_key = $facet->getFacetSourceConfig()->getFilterKey() ?: 'f'; + $active_items = $facet->getActiveItems(); + foreach ($active_items as $active_item) { + $url = Url::createFromRequest($request); + $modified_query_params = $query_params; + $modified_query_params[$filter_key] = array_filter($query_params[$filter_key], function ($query_param) use ($url_alias, $active_item) { + $pos = strpos($query_param, ':'); + $alias = substr($query_param, 0, $pos); + $value = substr($query_param, $pos + 1); + return !($alias == $url_alias && $value == $active_item); + }); + $url->setOption('query', $modified_query_params); + $item = [ + '#theme' => 'facets_result_item__summary', + '#value' => $active_item, + // We do not have counts for excluded/missing facets... + '#show_count' => FALSE, + // Do not know the count. + '#count' => 0, + '#is_active' => TRUE, + '#facet' => $facet, + '#raw_value' => $active_item, + ]; + $item = (new Link($item, $url))->toRenderable(); + $item['#wrapper_attributes'] = [ + 'class' => $this->classes(), + ]; + $build['#items'][] = $item; + } + } + } + return $build; + } + +} diff --git a/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowMissingFacets.php b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowMissingFacets.php index 75a4c5e1..4e658ebc 100644 --- a/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowMissingFacets.php +++ b/modules/islandora_advanced_search/src/Plugin/facets_summary/processor/ShowMissingFacets.php @@ -2,11 +2,10 @@ namespace Drupal\islandora_advanced_search\Plugin\facets_summary\processor; -use Drupal\Core\Link; -use Drupal\Core\Url; use Drupal\facets_summary\FacetsSummaryInterface; use Drupal\facets_summary\Processor\BuildProcessorInterface; use Drupal\facets_summary\Processor\ProcessorPluginBase; +use Drupal\facets\FacetInterface; /** * Provides a processor that shows the search query. @@ -22,49 +21,27 @@ use Drupal\facets_summary\Processor\ProcessorPluginBase; */ class ShowMissingFacets extends ProcessorPluginBase implements BuildProcessorInterface { + use ShowFacetsTrait; + + /** + * {@inheritdoc} + */ + protected function condition(FacetInterface $facet) { + return !$facet->getExclude() && empty($facet->getResults()); + } + + /** + * {@inheritdoc} + */ + protected function classes() { + return ['facet-summary-item--facet']; + } + /** * {@inheritdoc} */ public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) { - $request = \Drupal::request(); - $query_params = $request->query->all(); - foreach ($facets as $facet) { - if (!$facet->getExclude() && empty($facet->getResults())) { - $url_alias = $facet->getUrlAlias(); - $filter_key = $facet->getFacetSourceConfig()->getFilterKey() ?: 'f'; - $active_items = $facet->getActiveItems(); - foreach ($active_items as $active_item) { - $url = Url::createFromRequest($request); - $modified_query_params = $query_params; - $modified_query_params[$filter_key] = array_filter($query_params[$filter_key], function ($query_param) use ($url_alias, $active_item) { - $pos = strpos($query_param, ':'); - $alias = substr($query_param, 0, $pos); - $value = substr($query_param, $pos + 1); - return !($alias == $url_alias && $value == $active_item); - }); - $url->setOption('query', $modified_query_params); - $item = [ - '#theme' => 'facets_result_item__summary', - '#value' => $active_item, - // We do not have counts for missing facets... - '#show_count' => FALSE, - // Do not know the count. - '#count' => 0, - '#is_active' => TRUE, - '#facet' => $facet, - '#raw_value' => $active_item, - ]; - $item = (new Link($item, $url))->toRenderable(); - $item['#wrapper_attributes'] = [ - 'class' => [ - 'facet-summary-item--facet', - ], - ]; - $build['#items'][] = $item; - } - } - } - return $build; + return $this->buildHelper($build, $facets); } }