My organization uses Joomla! for its customer facing site. There is a database that I only have REST API access to. I'm trying to ping that API to include results from it alongside the normal Joomla! site search results.
I wrote the following code going along with the example here: https://docs.joomla.org/J3.x:Creating_a_search_plugin
// Prevent direct access
defined('_JEXEC') or die('Restricted access');
// Require API class
require_once APIClass.php';
class PlgSearchApiinjector extends JPlugin
{
/**
* Constructor Function
*/
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}
/**
* Includes this plugin in list of places to search
*/
public function onContentSearchAreas()
{
static $areas = array(
'plg_apiinjector' => 'API Search Result Injector - Unified Search',
);
return $areas;
}
/**
* Pings API and injects results
*/
public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
{
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
// If the array is not correct, return it:
if (is_array($areas)) {
if (!array_intersect($areas, array_keys($this->onContentSearchAreas()))) {
return array();
}
}
// Trim spaces from beginning and end of search terms.
$text = trim($text);
// Return Array when nothing was filled in.
if ($text == '') {
return array();
}
// Create new API object and complete search
$api = new API;
$results = $api->keywordSearch($text);
// Create a rows array to return
$rows = [];
// Loop through the results and add each one to the rows array.
foreach ($results as $result) {
// Extract portion of the result and prepare it for insertion into the $rows[] array.
$title = $result['Title'];
$section = 'Catalog';
$text = $result['Summary'];
$browsernav = 1;
$created = date("Y-m-d H:i:s");
$link = $result['WebLink'];
$tempObject = (object) array(
'href' => $link,
'title' => $title,
'section' => $section,
'created' => $created,
'text' => $text,
'browsernav' => $browsernav,
);
// Add the object to the rows array
array_push($rows, $tempObject);
// Return rows object
return $rows;
}
}
}
This isn't working the way I expected. Based on my tests, it doesn't appear that onContentSearch() is ever even being called.
Can someone explain to me what I'm doing wrong?
One more thing... As much as I would love to index the second database and index everything with Tika, that's not an option given the restrictions that are placed on me.
Thank you in advance!