Browse Source

bento solr site now returns results to the block

d9
ppound 3 years ago
parent
commit
f94c4fbc1e
  1. 82
      modules/solr_site/js/solr_site_results.js
  2. 1
      modules/solr_site/roblib_search_solr_site.libraries.yml
  3. 4
      modules/solr_site/roblib_search_solr_site.module
  4. 73
      modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php
  5. 26
      modules/solr_site/src/Form/SolrsiteSettingsForm.php
  6. 12
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php
  7. 26
      modules/solr_site/templates/roblib-search-solr-site-results.html.twig
  8. 43
      modules/solr_site/templates/theme.inc

82
modules/solr_site/js/solr_site_results.js

@ -1,47 +1,43 @@
Drupal.behaviors.roblib_search_solr_site_results = { Drupal.behaviors.roblib_search_solr_site_results = {
attach: function (context, settings) { attach: function (context, settings) {
url = settings.roblib_search_solr_site_results.search_url; if (context == document) {
query = settings.roblib_search_solr_site_results.search_query; query = $current_path.replace('roblib_search/', '');
baseUrl = settings.roblib_search_solr_site_results.base_url; solrUrl = settings.solr_site_search_url + query;
default_site_type = settings.roblib_search_solr_site_results.default_site_type + '/'; baseUrl = settings.solr_site_base_url + query;
jQuery.getJSON(url, function (data) { jQuery.getJSON(baseUrl, function (data) {
var items = []; var items = [];
total_found = data.response.numFound; total_found = data.resultCount;
var numberOfDocs = 0;
try { if (total_found < 1) {
numberOfDocs = data.response.docs.length; jQuery('#' + 'roblib-search-content-solr-site-results').empty().append('No Results');
} catch (err) { jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-results').hide();
// do nothing leave docLength at 0 } else {
} jQuery('#' + 'roblib-search-content-solr-site-results').empty();
if (numberOfDocs < 1) { var counter = 0;
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append('No Results'); var divs = new Array();
jQuery('.' + 'pane-roblib-search-solr-site-roblib-search-solr-site-results').hide(); var content = new Array();
} else { jQuery.each(data.results, function (key, val) {
jQuery('#' + 'roblib-search-content-solr-site-results').empty(); id = 'roblib_search_solr_results_' + 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.response.docs, function (key, val) {
id = 'roblib_search_solr_results_' + counter;
content[counter] = val.teaser;
divs[counter++] = id;
items.push('<div class ="roblib-search-row" id="' + id + '">\n\
<span class="roblib-title results">\n\ <span class="roblib-title results">\n\
<a href="' + val.url + '">' + val.label + '</a></span>' + <a href="' + val.url + '">' + val.title + '</a></span>' +
'\n\<span class="roblib-solr-bundle">(' + val.bundle_name + ')</span> <span class="roblib-solr-teaser"> ' + val.teaser + '</span></div>'); '\n\<span class="roblib-solr-bundle">(' + val.type + ')</span> <span class="roblib-solr-teaser"> ' + val.body + '</span></div>');
}); });
jQuery('#' + 'roblib-search-content-solr-site-results').empty().append(items.join('')); jQuery('#' + 'roblib-search-content-solr-site-results').empty().append(items.join(''));
} }
if (numberOfDocs > 0) { if (total_found > 0) {
results_url = '<a href="' + baseUrl + '/search/' + default_site_type + query + '?f[0]=(-bundle:bestbet)"' + '>see all results (' results_url = '<a href="' + solrUrl + '">see all results ('
+ total_found + ') </a>'; + total_found + ') </a>';
move_me_div_url = '<a href="' + baseUrl + '/search/' + default_site_type + query + '?f[0]=(-bundle:bestbet)"' + '>Library Site (' move_me_div_url = '<a href="' + solrUrl + '">Library Site ('
+ total_found + ') </a>'; + total_found + ') </a>';
jQuery('#' + 'roblib-search-solr-site-results-more').empty().append(results_url); jQuery('#' + 'roblib-search-solr-site-results-more').empty().append(results_url);
jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url); jQuery('#' + 'roblib-solr-more-results-results').empty().append(results_url);
jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url); jQuery('#' + 'roblib-solr-search-toc-results').empty().append(move_me_div_url);
} }
}); });
} }
}
} }

1
modules/solr_site/roblib_search_solr_site.libraries.yml

@ -5,6 +5,5 @@ solr-site:
css/roblib_search_solr_site.css: {} css/roblib_search_solr_site.css: {}
js: js:
js/solr_site_results.js: {} js/solr_site_results.js: {}
js/solr_site_bestbet.js: {}
dependencies: dependencies:
- core/drupal - core/drupal

4
modules/solr_site/roblib_search_solr_site.module

@ -15,8 +15,8 @@ function roblib_search_solr_site_theme($existing, $type, $theme, $path) {
->getPath(); ->getPath();
$spinner_path = '/' . $modulePath . "/img/spinner.gif"; $spinner_path = '/' . $modulePath . "/img/spinner.gif";
return [ return [
'roblib_search_solr_site' => [ 'roblib_search_solr_site_results' => [
'variables' => ['query' => NULL, 'spinner_path' => $spinner_path], 'variables' => ['solr_type' => NULL, 'spinner_path' => $spinner_path],
], ],
]; ];
} }

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

@ -29,42 +29,77 @@ class RoblibSearchSolrsiteController extends ControllerBase {
/** /**
* Query Solr for site results. * Query Solr for site results.
* @param string $query * @param string $queryString
* The search term(s) to send to Solr. * The search term(s) to send to Solr.
* @param string type * @param string type
* The type of search, either BestBet or General. * The type of search, either BestBet or General.
* @return string * @return string
* A JSON string * A JSON string
*/ */
function getResults($query = NULL, $type = 'General') { function getResults($queryString = NULL, $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.
if (!isset($query)) { if (!isset($queryString)) {
if (isset($_GET['roblib_query'])) { if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query']; $queryString = $_GET['roblib_query'];
} }
else { else {
return ''; return '';
} }
} }
$query = urlencode($query); $queryString = urlencode($queryString);
$index = \Drupal\search_api\Entity\Index::load('default_solr_index');
$query = $index->query();
/* $num_results = variable_get('roblib_search_solr_site_num_results', '5'); // Change the parse mode for the search.
$query = apachesolr_drupal_query("apachesolr"); $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
$encoded_query = htmlentities($query_string, ENT_QUOTES); ->createInstance('direct');
$query->addParam('q', $encoded_query); // keyword to be searched $parse_mode->setConjunction('OR');
$query->addParam('rows', $num_results); $query->setParseMode($parse_mode);
$query->addParam('fq', $type);
$query->addParam('mm', '1');
apachesolr_search_add_boost_params($query);
$response = $query->search();
$json_results = drupal_json_encode($response);
return $json_results;*/
// Restrict the search to specific languages.
$query->setLanguages(['en']);
$search_url = $base_url . '/roblib_search/solr_site/' . $type . '/' . $query; // Set fulltext search keywords and fields.
return json_encode($query); $query->keys($queryString);
// $query->setFulltextFields(['rendered_item', 'title', 'body']);
$query->range(0, 5);
$query->sort('search_api_relevance', 'DESC');
$results = $query->execute();
$output = [];
$output['resultCount'] = $results->getResultCount();
$output['results'] = [];
foreach ($results as $result) {
$id = $result->getId();
$titles = $result->getField('title')->getValues();
$output['results'][$id]['title'] = $titles[0]->getText();
$body = $result->getField('body')->getValues();
$bodyOutput = strip_tags($body[0]->getText());
if (strlen($bodyOutput) > 500) {
$bodyOutput = substr($bodyOutput, 0, 500) . '...';
}
$output['results'][$id]['body'] = $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);
$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;
}
echo json_encode($output);
exit();
} }
} }

26
modules/solr_site/src/Form/SolrsiteSettingsForm.php

@ -16,38 +16,20 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormId() { public function getFormId() {
return 'roblib_search_eds_settings_form'; return 'roblib_search_solr_settings_form';
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search_eds.settings'); $config = \Drupal::config('roblib_search_solr.settings');
/*
*
* $form['roblib_search_solr_site_url'] = array(
'#type' => 'textfield',
'#title' => t('Solr url'),
'#default_value' => variable_get('roblib_search_solr_site_url', 'http://localhost:8983/solr'),
'#description' => t('The base Solr URL, for example http://localhost:8983/solr'),
'#required' => TRUE,
);
$form['roblib_search_solr_site_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#default_value' => variable_get('roblib_search_solr_site_num_results', '5'),
'#description' => t('The number of results to display in the Bento box'),
'#required' => TRUE,
);
return system_settings_form($form);
*/
$form['solr_site_url'] = array( $form['solr_site_url'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Solr URL'), '#title' => t('Solr URL'),
'#default_value' => $config->get('solr_site_url'), '#default_value' => $config->get('solr_site_url'),
'#description' => t('The base Solr URL, for example http://localhost:8983/solr'), '#description' => t('The base Solr URL, for example /search/node?keys='),
'#required' => TRUE, '#required' => TRUE,
); );
@ -77,7 +59,7 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search_eds.settings'); $config = \Drupal::configFactory()->getEditable('roblib_search_solr.settings');
$config->set('solr_site_url', $form_state->getValue('solr_site_url'))->save(); $config->set('solr_site_url', $form_state->getValue('solr_site_url'))->save();
$config->set('solr_site_num_results', $form_state->getValue('solr_site_num_results'))->save(); $config->set('solr_site_num_results', $form_state->getValue('solr_site_num_results'))->save();
} }

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

@ -19,18 +19,20 @@ class RoblibSearchSolrsiteGeneral extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function build() { public function build() {
// $host = \Drupal::request()->getSchemeAndHttpHost(); $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'); ->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/',
]; ];
return [ return [
'#theme' => 'roblib_search_solr_site', '#theme' => 'roblib_search_solr_site_results',
'query' => $query, //'query' => $query,
'#solr_type' => 'results',
'#attached' => [ '#attached' => [
'library' => [ 'library' => [
'roblib_search_solr_site/solr_site' 'roblib_search_solr_site/solr-site'
], ],
'drupalSettings' => $settings, 'drupalSettings' => $settings,
], ],

26
modules/solr_site/templates/roblib-search-solr-site-results.html.twig

@ -1,24 +1,8 @@
<?php
/** <div class ="roblib-search-more" id="roblib-search-solr-site-{{ solr_type }}-more"></div>
* @file roblib-search-solr-site-results.tpl.php <div class ="roblib-search-content solr-site-{{ solr_type }}" id="roblib-search-content-solr-site-{{ solr_type }}">
* <img src="{{ spinner_path }}"/>
*
* Variables available:
* - $variables: all array elements of $variables can be used as a variable. e.g. $base_url equals $variables['base_url']
*
*/
?>
<?
// Get rid of warnings in admin interface
if (!isset($type)){
$type = 'results';
}
?>
<div class ="roblib-search-more" id="roblib-search-solr-site-<?php echo $type;?>-more"></div>
<div class ="roblib-search-content solr-site-<?php echo $type;?>" id="roblib-search-content-solr-site-<?php echo $type;?>">
<img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div> </div>
<div id="roblib-solr-more-results-<?php print($type)?>" class="roblib-solr-more-button "></div> <div id="roblib-solr-more-results-{{ solr_type }}" class="roblib-solr-more-button "></div>
<div id="roblib-solr-search-toc-<?php print($type)?>" class="roblib-bento-toc moveme-please"></div> <div id="roblib-solr-search-toc-{{ solr_type }}" class="roblib-bento-toc moveme-please"></div>

43
modules/solr_site/templates/theme.inc

@ -1,43 +0,0 @@
<?php
/*
*
*/
/**
* @param array $variables
* @param string $type
*
* @return string
*/
function roblib_search_solr_site_preprocess_roblib_search_solr_site(&$variables){
//function roblib_search_solr_site_preprocessor(&$variables, $type) {
global $base_url;
$type = $variables['type'];
if (!isset($query)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
return '';
}
}
$spinner_path = $base_url . '/' . drupal_get_path('module', 'roblib_search') . '/img/' . 'spinner.gif';
$variables['spinner_path'] = $spinner_path;
$variables['type'] = $type;
$query = urlencode($query);
$search_url = $base_url . '/roblib_search/solr_site/' . $type . '/' . $query;
drupal_add_js(drupal_get_path('module', 'roblib_search_solr_site') . '/js/solr_site_' . $type . '.js');
$key = 'roblib_search_solr_site_' . $type;
$default_module = variable_get('search_default_module', 'site') == 'apachesolr_search'
? 'site' : variable_get('search_default_module', 'site');
drupal_add_js(array(
$key => array(
'search_url' => $search_url,
'search_query' => $query,
'base_url' => $base_url,
'default_site_type' => $default_module
)
), array('type' => 'setting'));
}
Loading…
Cancel
Save