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 = {
attach: function(context, settings) {
url = settings.roblib_search_solr_site_bestbet.search_url;
default_site_type = settings.roblib_search_solr_site_results.default_site_type
jQuery.getJSON(url, function(data) {
Drupal.behaviors.roblib_search_solr_bestbet_results = {
attach: function (context, settings) {
if (context == document) {
query = drupalSettings.path.currentPath.replace('roblib_search/', '');
solrUrl = settings.solr_site_search_url + query;
baseUrl = settings.solr_bestbet_base_url + query;
jQuery.getJSON(baseUrl, function (data) {
var items = [];
var numberOfDocs = 0;
try {
numberOfDocs = data.response.docs.length;
} catch (err) {
// do nothing leave docLength at 0
}
if (numberOfDocs < 1) {
total_found = data.resultCount;
if (total_found < 1) {
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 {
jQuery('#' + 'roblib-search-content-solr-site-bestbet').empty();
var counter = 0;
var divs = new Array();
var content = new Array();
jQuery.each(data.response.docs, function(key, val) {
id = 'roblib_search_solr_bestbets_' + counter;
jQuery.each(data.results, function (key, val) {
id = 'roblib_search_solr_results_bestbet-' + counter;
content[counter] = val.teaser;
divs[counter++] = id;
items.push('<div class ="roblib-search-row" id="' + id + '">\n\
<div class="roblib-title solr_site">\n\
<a href="' + val.url + '">' + val.content + '</a></div></div>');
<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: {}
dependencies:
- 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";
return [
'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
* A JSON string
*/
function getResults($queryString = NULL, $type = 'General') {
function getResults($queryString = NULL, $solr_type = 'General') {
global $base_url;
$config = \Drupal::config('roblib_search_solr_site.settings');
// Url parameters.
@ -49,7 +49,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
}
}
$queryString = urlencode($queryString);
// TODO make the index configurable.
$index = \Drupal\search_api\Entity\Index::load('default_solr_index');
$query = $index->query();
@ -60,10 +60,14 @@ class RoblibSearchSolrsiteController extends ControllerBase {
$query->setParseMode($parse_mode);
// Restrict the search to specific languages.
$query->setLanguages(['en']);
//$query->setLanguages(['en']);
// Set fulltext search keywords and fields.
$query->keys($queryString);
if ($solr_type == 'BestBet') {
$query->addCondition('type', 'bestbet');
}
// $query->setFulltextFields(['rendered_item', 'title', 'body']);
$query->range(0, 5);
$query->sort('search_api_relevance', 'DESC');
@ -78,24 +82,23 @@ class RoblibSearchSolrsiteController extends ControllerBase {
$titles = $result->getField('title')->getValues();
$output['results'][$id]['title'] = $titles[0]->getText();
$body = $result->getField('body')->getValues();
if (!empty($body)) {
$bodyOutput = strip_tags($body[0]->getText());
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();
// $output['rendered_item'] = $rendered_items[0]->getText();
$summarys = $result->getField('summary')->getValues();
$output['results'][$id]['summary'] = reset($summarys);
$output['results'][$id]['summary'] = empty($summmarys) ? '' : reset($summarys);
$type = $result->getField('type')->getValues();
$output['results'][$id]['type'] = reset($type);
$urls = $result->getField('url')->getValues();
$output['results'][$id]['url'] = reset($urls);
//list(, $path, $langcode) = explode(':', $result->getId());
//list($entity_type, $id) = explode('/', $path);
//$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;
$links = $result->getField('field_link')->getValues();
$output['results'][$id]['field_link'] = empty($links) ? '' : $links;
}
echo json_encode($output);

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

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