0

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!

1 Answer 1

0

Okay... So I figured it out on my own. First, I didn't have consistency in the plugin names. Second, I needed to remove the plg_ prefix from the plugin name.

1
  • If this issue is resolved, you may award the green tick to your answer so that it is deemed "resolved" within the system (and will stop getting "bumped" by the Community program). Always try to be as educational and clear as possible when posting questions or answers on this site. This helps the site to be a more effective resource. Commented Jul 2, 2018 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.