Browse Source

added solr bestbet bento box

d9
ppound 3 years ago
parent
commit
751b2691a3
  1. 37
      modules/solr_site/js/solr_site_bestbet.js
  2. 6
      modules/solr_site/roblib_search_solr_site.libraries.yml
  3. 5
      modules/solr_site/roblib_search_solr_site.module
  4. 25
      modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php
  5. 14
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php
  6. 11
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php
  7. 8
      modules/solr_site/templates/roblib-search-solr-site-bestbet.html.twig
  8. 5
      src/Plugin/Block/RoblibSearchOtherSourcesBlock.php

37
modules/solr_site/js/solr_site_bestbet.js

@ -1,37 +1,34 @@
Drupal.behaviors.roblib_search_solr_site_bestbet = { Drupal.behaviors.roblib_search_solr_bestbet_results = {
attach: function(context, settings) { attach: function (context, settings) {
url = settings.roblib_search_solr_site_bestbet.search_url; if (context == document) {
default_site_type = settings.roblib_search_solr_site_results.default_site_type query = drupalSettings.path.currentPath.replace('roblib_search/', '');
jQuery.getJSON(url, function(data) { solrUrl = settings.solr_site_search_url + query;
baseUrl = settings.solr_bestbet_base_url + query;
jQuery.getJSON(baseUrl, function (data) {
var items = []; var items = [];
var numberOfDocs = 0; total_found = data.resultCount;
try {
numberOfDocs = data.response.docs.length; if (total_found < 1) {
} catch (err) {
// do nothing leave docLength at 0
}
if (numberOfDocs < 1) {
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append('No Results'); jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append('No Results');
jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-bestbet').hide(); jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-' . solrType).hide();
} else { } else {
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty(); jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty();
var counter = 0; var counter = 0;
var divs = new Array(); var divs = new Array();
var content = new Array(); var content = new Array();
jQuery.each(data.response.docs, function(key, val) { jQuery.each(data.results, function (key, val) {
id = 'roblib_search_solr_bestbets_' + counter; id = 'roblib_search_solr_results_bestbet-' + counter;
content[counter] = val.teaser; content[counter] = val.teaser;
divs[counter++] = id; divs[counter++] = id;
items.push('<div class ="roblib-search-row" id="' + id + '">\n\ items.push('<div class ="roblib-search-row" id="' + id + '">\n\
<div class="roblib-title solr_site">\n\ <span class="roblib-title results">\n\
<a href="' + val.url + '">' + val.content + '</a></div></div>'); <a href="' + val.field_link + '">' + val.title + '</a></span>' +
'\n\<span class="roblib-solr-bundle">(' + val.type + ')</span> <span class="roblib-solr-teaser"> ' + val.body + '</span></div>');
}); });
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append(items.join('')); jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append(items.join(''));
} }
}); });
} }
}
} }

6
modules/solr_site/roblib_search_solr_site.libraries.yml

@ -7,3 +7,9 @@ solr-site:
js/solr_site_results.js: {} js/solr_site_results.js: {}
dependencies: dependencies:
- core/drupal - core/drupal
solr-bestbet:
version: 1.x
js:
js/solr_site_bestbet.js: {}
dependencies:
- core/drupal

5
modules/solr_site/roblib_search_solr_site.module

@ -16,7 +16,10 @@ function roblib_search_solr_site_theme($existing, $type, $theme, $path) {
$spinner_path = '/' . $modulePath . "/img/spinner.gif"; $spinner_path = '/' . $modulePath . "/img/spinner.gif";
return [ return [
'roblib_search_solr_site_results' => [ 'roblib_search_solr_site_results' => [
'variables' => ['solr_type' => NULL, 'spinner_path' => $spinner_path], 'variables' => ['solr_type' => 'results', 'spinner_path' => $spinner_path],
],
'roblib_search_solr_site_bestbet' => [
'variables' => ['solr_type' => 'bestbet', 'spinner_path' => $spinner_path],
], ],
]; ];
} }

25
modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php

@ -36,7 +36,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
* @return string * @return string
* A JSON string * A JSON string
*/ */
function getResults($queryString = NULL, $type = 'General') { function getResults($queryString = NULL, $solr_type = 'General') {
global $base_url; global $base_url;
$config = \Drupal::config('roblib_search_solr_site.settings'); $config = \Drupal::config('roblib_search_solr_site.settings');
// Url parameters. // Url parameters.
@ -49,7 +49,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
} }
} }
$queryString = urlencode($queryString); $queryString = urlencode($queryString);
// TODO make the index configurable.
$index = \Drupal\search_api\Entity\Index::load('default_solr_index'); $index = \Drupal\search_api\Entity\Index::load('default_solr_index');
$query = $index->query(); $query = $index->query();
@ -60,10 +60,14 @@ class RoblibSearchSolrsiteController extends ControllerBase {
$query->setParseMode($parse_mode); $query->setParseMode($parse_mode);
// Restrict the search to specific languages. // Restrict the search to specific languages.
$query->setLanguages(['en']); //$query->setLanguages(['en']);
// Set fulltext search keywords and fields.
$query->keys($queryString); $query->keys($queryString);
if ($solr_type == 'BestBet') {
$query->addCondition('type', 'bestbet');
}
// $query->setFulltextFields(['rendered_item', 'title', 'body']); // $query->setFulltextFields(['rendered_item', 'title', 'body']);
$query->range(0, 5); $query->range(0, 5);
$query->sort('search_api_relevance', 'DESC'); $query->sort('search_api_relevance', 'DESC');
@ -78,24 +82,23 @@ class RoblibSearchSolrsiteController extends ControllerBase {
$titles = $result->getField('title')->getValues(); $titles = $result->getField('title')->getValues();
$output['results'][$id]['title'] = $titles[0]->getText(); $output['results'][$id]['title'] = $titles[0]->getText();
$body = $result->getField('body')->getValues(); $body = $result->getField('body')->getValues();
if (!empty($body)) {
$bodyOutput = strip_tags($body[0]->getText()); $bodyOutput = strip_tags($body[0]->getText());
if (strlen($bodyOutput) > 500) { if (strlen($bodyOutput) > 500) {
$bodyOutput = substr($bodyOutput, 0, 500) . '...'; $bodyOutput = substr($bodyOutput, 0, 500) . '...';
} }
$output['results'][$id]['body'] = $bodyOutput; }
$output['results'][$id]['body'] = empty($bodyOutput) ? '' : $bodyOutput;
//$rendered_items = $result->getField('rendered_item')->getValues(); //$rendered_items = $result->getField('rendered_item')->getValues();
// $output['rendered_item'] = $rendered_items[0]->getText(); // $output['rendered_item'] = $rendered_items[0]->getText();
$summarys = $result->getField('summary')->getValues(); $summarys = $result->getField('summary')->getValues();
$output['results'][$id]['summary'] = reset($summarys); $output['results'][$id]['summary'] = empty($summmarys) ? '' : reset($summarys);
$type = $result->getField('type')->getValues(); $type = $result->getField('type')->getValues();
$output['results'][$id]['type'] = reset($type); $output['results'][$id]['type'] = reset($type);
$urls = $result->getField('url')->getValues(); $urls = $result->getField('url')->getValues();
$output['results'][$id]['url'] = reset($urls); $output['results'][$id]['url'] = reset($urls);
//list(, $path, $langcode) = explode(':', $result->getId()); $links = $result->getField('field_link')->getValues();
//list($entity_type, $id) = explode('/', $path); $output['results'][$id]['field_link'] = empty($links) ? '' : $links;
//$entity = \Drupal::service('entity_type.manager')->getStorage($entity_type)->load($id);
//teaserOutput = \Drupal::service('entity_type.manager')->getViewBuilder($entity_type)->view($entity, 'teaser');
//$output['teaser'] = $teaserOutput;
} }
echo json_encode($output); echo json_encode($output);

14
modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php

@ -19,21 +19,25 @@ class RoblibSearchSolrSiteBestbet extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function build() { public function build() {
//$host = \Drupal::request()->getSchemeAndHttpHost(); $search_url = \Drupal::config('roblib_search_solr.settings')
$search_url = \Drupal::config('roblib_search_solr_site.settings')
->get('solr_site_url'); ->get('solr_site_url');
$settings = [ $settings = [
'solr_site_search_url' => $search_url, 'solr_site_search_url' => $search_url,
'solr_bestbet_base_url' => '/roblib_search/solr_site/bestbet/',
]; ];
return [ return [
'#theme' => 'roblib_search_solr_site', '#theme' => 'roblib_search_solr_site_bestbet',
'query' => $query, //'#solr_type' => 'bestbet',
'#attached' => [ '#attached' => [
'library' => [ 'library' => [
'roblib_search_solr_site/solr_site' 'roblib_search_solr_site/solr-bestbet'
], ],
'drupalSettings' => $settings, 'drupalSettings' => $settings,
], ],
]; ];
} }
/*public function getCacheMaxAge() {
return 1;
}*/
} }

11
modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php

@ -19,17 +19,16 @@ class RoblibSearchSolrsiteGeneral extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function build() { public function build() {
$host = \Drupal::request()->getSchemeAndHttpHost();
$search_url = \Drupal::config('roblib_search_solr.settings') $search_url = \Drupal::config('roblib_search_solr.settings')
->get('solr_site_url'); ->get('solr_site_url');
$settings = [ $settings = [
'solr_site_search_url' => $search_url, 'solr_site_search_url' => $search_url,
'solr_site_base_url' => $host . '/roblib_search/solr_site/general/', 'solr_site_base_url' => '/roblib_search/solr_site/general/',
'solr_type' => 'results',
]; ];
return [ return [
'#theme' => 'roblib_search_solr_site_results', '#theme' => 'roblib_search_solr_site_results',
//'query' => $query, //'#solr_type' => 'results',
'#solr_type' => 'results',
'#attached' => [ '#attached' => [
'library' => [ 'library' => [
'roblib_search_solr_site/solr-site' 'roblib_search_solr_site/solr-site'
@ -39,4 +38,8 @@ class RoblibSearchSolrsiteGeneral extends BlockBase {
]; ];
} }
/*public function getCacheMaxAge() {
return 1;
}*/
} }

8
modules/solr_site/templates/roblib-search-solr-site-bestbet.html.twig

@ -0,0 +1,8 @@
<div class ="roblib-search-more" id="roblib-search-solr-site-{{ solr_type }}-more"></div>
<div class ="roblib-search-content solr-site-{{ solr_type }}" id="roblib-search-content-solr-site-{{ solr_type }}">
<img src="{{ spinner_path }}"/>
</div>
<div id="roblib-solr-more-results-{{ solr_type }}" class="roblib-solr-more-button "></div>
<div id="roblib-solr-search-toc-{{ solr_type }}" class="roblib-bento-toc moveme-please"></div>

5
src/Plugin/Block/RoblibSearchOtherSourcesBlock.php

@ -31,4 +31,9 @@ class RoblibSearchOtherSourcesBlock extends BlockBase {
]; ];
} }
public function getCacheMaxAge() {
// don't cache this block
return 0;
}
} }

Loading…
Cancel
Save