5

I am working on some custom search plugins and I have a 2-part question regarding the file administrator/components/com_search/helpers/search.php.

I want to adjust the sanitization settings without hacking core files.

I need $lower_limit = 1; The core script supplies a value of 3 and reads:

$lower_limit = $lang->getLowerLimitSearchWord();

I have found the reference https://api.joomla.org/cms-3/classes/Joomla.CMS.Language.Language.html#method_getLowerLimitSearchWord. It tells me:

integer The lower limit integer for length of search words (3 if no value was set for a specific language)

but this doesn't tell me how/where to override that default value. I assume I should be doing something like COM_SEARCH_LOWER_LIMIT_SEARCH_WORD=1 or something.

Question 1. Which language override file do I need to write to and what is the appropriate syntax?

The next setting to understand is $search_ignore = $lang->getIgnoredSearchWords();. Right now, it is showing me a whitelist of and, or, and in.

Question 2. How do I adjust the getIgnoredSearchWords() strings?

I found where the default values are stored, but I don't want to hack this file: https://github.com/joomla-framework/language/blob/master/Tests/data/language/en-GB/en-GB.localise.php

This JSE question is virtually a duplicate but it fails to explain how to override these defaults.

I'm not meant to touch /language/en-GB/en-GB.localise.php, right?

1 Answer 1

6

Look for methods called set<PROPERTY>Callback on the same API documentation you linked. E.g. to override the lower limit use Joomla\CMS\Language\Language::setLowerLimitSearchWordCallback().

I'm not meant to touch /language/en-GB/en-GB.localise.php, right?

Correct. This should be done in a system plugin:

defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;

class PlgSystemExample extends CMSPlugin
{
    protected $app;

    public function onAfterInitialise()
    {
        $this->app->getLanguage()->setLowerLimitSearchWordCallback(array(__CLASS__, 'setLowerLimitSearchWord'));    
    }

    public static function setLowerLimitSearchWord()
    {
        return 2;
    }
}

You can pass an anonymous function too:

$this->app->getLanguage()->setLowerLimitSearchWordCallback(
    function()
    {
        return 2;
    }
);

For setting ignored search words use setIgnoredSearchWordsCallback():

public function onAfterRoute()
{
    $this->app->getLanguage()->setIgnoredSearchWordsCallback(array(__CLASS__, 'setIgnoredSearchWords'));
}

public static function setIgnoredSearchWords()
{
    return ['in', 'and', 'or', 'if'];
}

Regarding plugin events, you can use either onAfterInitialise() or onAfterRoute(). Anything later will not work. Use onAfterRoute() if you need the use of input variables, e.g. to limit the plugin to specific pages.

7
  • I checked that again and the property is indeed protected and the setter method use is appropriate, so your answer is good and useful.
    – Zollie
    Feb 13, 2019 at 9:41
  • @Sharky We were able to quickly and simply implement your advice in a new System Plugin. Thank you. Out of curiosity, can this be performed inside of a Search Plugin? or does it need to be done in a System Plugin? I have a few small custom Search Plugins that I wish to apply these changes to. Feb 14, 2019 at 4:42
  • 2
    @mickmackusa Apparently, yes, this can be done using onContentSearchAreas event.
    – Sharky
    Feb 14, 2019 at 6:35
  • My boss and I are wondering what country you are operating out of. I don't know you beyond your username. Feb 15, 2019 at 8:51
  • @Sharky Can you provide a full example for such a onContentSearchAreas event? I have such a function in a JPlugin, so what should I add there to eliminate all stop words, for example? This would help newcomers like me.
    – Sven
    Sep 30, 2020 at 10:21

Your Answer

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

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