Browse Source

added solr bestbet bento box

d9
ppound 3 years ago
parent
commit
751b2691a3
  1. 63
      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. 31
      modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php
  5. 18
      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

63
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;
var items = []; baseUrl = settings.solr_bestbet_base_url + query;
var numberOfDocs = 0; jQuery.getJSON(baseUrl, function (data) {
try { var items = [];
numberOfDocs = data.response.docs.length; total_found = data.resultCount;
} catch (err) {
// do nothing leave docLength at 0
}
if (numberOfDocs < 1) {
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append('No Results');
jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-bestbet').hide();
} else {
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty(); if (total_found < 1) {
var counter = 0; jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append('No Results');
var divs = new Array(); jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-' . solrType).hide();
var content = new Array(); } else {
jQuery.each(data.response.docs, function(key, val) { jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty();
id = 'roblib_search_solr_bestbets_' + counter; var counter = 0;
content[counter] = val.teaser; var divs = new Array();
divs[counter++] = id; var content = new Array();
items.push('<div class ="roblib-search-row" id="' + id + '">\n\ jQuery.each(data.results, function (key, val) {
<div class="roblib-title solr_site">\n\ id = 'roblib_search_solr_results_bestbet-' + counter;
<a href="' + val.url + '">' + val.content + '</a></div></div>'); content[counter] = val.teaser;
divs[counter++] = id;
}); items.push('<div class ="roblib-search-row" id="' + id + '">\n\
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty().append(items.join('')); <span class="roblib-title results">\n\
} <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(''));
}
});
}
} }
} }

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],
], ],
]; ];
} }

31
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();
$bodyOutput = strip_tags($body[0]->getText()); if (!empty($body)) {
if (strlen($bodyOutput) > 500) { $bodyOutput = strip_tags($body[0]->getText());
$bodyOutput = substr($bodyOutput, 0, 500) . '...'; if (strlen($bodyOutput) > 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);

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

@ -18,22 +18,26 @@ 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'
@ -38,5 +37,9 @@ 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

@ -30,5 +30,10 @@ class RoblibSearchOtherSourcesBlock extends BlockBase {
'#query' => $query, '#query' => $query,
]; ];
} }
public function getCacheMaxAge() {
// don't cache this block
return 0;
}
} }

Loading…
Cancel
Save