Skip to content

Commit

Permalink
Add template for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
almasaeed2010 committed Mar 27, 2018
1 parent a05921f commit 0b94d91
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
39 changes: 39 additions & 0 deletions theme/templates/elasticsearch_results.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php foreach ($rows as $row): ?>
<p>
<?php // Deal with urls to outside sites (we can't use l() here) ?>
<?php if ($base_url): ?>
<a href="<?php print url($row->url, ['base_url' => $base_url]) ?>">
<strong><?php print $row->title ?></strong>
</a>
<br/>
<?php else: ?>
<strong><?php print l($row->title, $row->url) ?></strong><br/>
<?php endif ?>

<span>Content type: <em><?php print $row->type ?></em></span><br/>

<?php if (!empty($row->content)): ?>
<span><?php print $row->content ?></span><br/>
<?php endif; ?>

<?php if ($base_url): ?>
<small>
<a href="<?php print $row->url ?>" class="text-muted">
<?php print substr($row->url, 0, 40) . (strlen($row->url) > 40 ? '...' : '') ?>
</a>
</small>
<?php else: ?>
<small>
<?php
$url = $GLOBALS['base_url'] . url(ltrim($row->url, '/'));
$url = substr($url, 0, 40) . (strlen($url) > 40 ? '...' : '')
?>
<?php print l($url, $row->url, [
'attributes' => [
'class' => ['text-muted'],
],
]) ?>
</small>
<?php endif; ?>
</p>
<?php endforeach;
54 changes: 40 additions & 14 deletions tripal_elasticsearch.api.inc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ function get_website_search_results_category_list($keyword) {
* @param string $base_url Specify the site that owns the results.
*
* @return string
* @throws Exception
*/
function get_website_search_result_table($search_results, $pager = TRUE, $base_url = NULL) {
// Get all labels
Expand All @@ -263,41 +264,66 @@ function get_website_search_result_table($search_results, $pager = TRUE, $base_u
$rows = [];
foreach ($search_results as $item) {
$item = (object) $item;
$row = new stdClass();
$row->title = $item->title;
if (isset($item->nid)) {
$row = '<h3>' . l($item->title, 'node/' . $item->nid) . '</h3>';
// $row = '<h3>' . l($item->title, 'node/' . $item->nid) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/node/' . $item->nid . '">' . $item->title . '</a></h3>';
// }

if ($base_url) {
$row = '<h3><a href="' . $base_url . '/node/' . $item->nid . '">' . $item->title . '</a></h3>';
$row->url = "{$base_url}/node/{$item->nid}";
}
else {
$row->url = "/node/{$item->nid}";
}
}
else {
$row = '<h3>' . l($item->title, 'bio_data/' . $item->entity_id) . '</h3>';
// $row = '<h3>' . l($item->title, 'bio_data/' . $item->entity_id) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/bio_data/' . $item->entity_id . '">' . $item->title . '</a></h3>';
// }

if ($base_url) {
$row = '<h3><a href="' . $base_url . '/bio_data/' . $item->entity_id . '">' . $item->title . '</a></h3>';
$row->url = "{$base_url}/bio_data/{$item->entity_id}";
}
else {
$row->url = "/bio_data/{$item->entity_id}";
}
}

$row->type = '';
if (property_exists($item, 'bundle_label')) {
$type = isset($types[$item->bundle_label]) ? $types[$item->bundle_label] : $item->bundle_label;
$row .= '<div>Type: <i>' . $type . '</i></div>';
// $row .= '<div>Type: <i>' . $type . '</i></div>';
$row->type = $type;
}
elseif (property_exists($item, 'type')) {
$type = isset($types[$item->type]) ? $types[$item->type] : $item->type;
$row .= '<div>Type: <i>' . $type . '</i></div>';
// $row .= '<div>Type: <i>' . $type . '</i></div>';
$row->type = $type;
}

$row->content = '';
if (property_exists($item, 'highlight')) {
$row .= '<p>' . $item->highlight . '</p>';
// $row .= '<p>' . $item->highlight . '</p>';
$row->content = $item->highlight;
}

$rows[] = [$row];
$rows[] = $row;
}

$output = theme('table', [
'header' => [],
// $output = theme('table', [
// 'header' => [],
// 'rows' => $rows,
// 'attributes' => [
// 'id' => 'es-search-results-table',
// ],
// ]);
$output = theme('elasticsearch_results', [
'rows' => $rows,
'attributes' => [
'id' => 'es-search-results-table',
],
'base_url' => $base_url
]);

if ($pager) {
Expand Down Expand Up @@ -427,4 +453,4 @@ function get_table_search_result_table($search_results, $index_name, $total) {
$output .= theme('pager', ['quantity', $total]);

return $output;
}
}
12 changes: 12 additions & 0 deletions tripal_elasticsearch.module
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ function tripal_elasticsearch_theme($existing, $type, $theme, $path) {
'render element' => 'content',
'path' => "$path/theme/templates",
],
'elasticsearch_results' => [
'template' => 'elasticsearch_results',
// 'render element' => 'elements',
'path' => "$path/theme/templates",
'variables' => [
// The results array
'rows' => [],
// The base url of the site
'base_url' => null
]
],
];

return $themes;
Expand Down Expand Up @@ -575,6 +586,7 @@ function tripal_elasticsearch_block_view($delta = '') {
* tripal_elasticsearch_search_results_category_page_callback
*
* @param string $node_type Category name
* @throws Exception
*/
function tripal_elasticsearch_web_search_results_page_callback($node_type = '') {
$keyword = isset($_GET['search_box']) ? $_GET['search_box'] : '';
Expand Down

0 comments on commit 0b94d91

Please sign in to comment.