3

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?

4

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($this, '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($this, '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.

  • 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 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. – mickmackusa Feb 14 at 4:42
  • 1
    @mickmackusa Apparently, yes, this can be done using onContentSearchAreas event. – Sharky Feb 14 at 6:35
  • My boss and I are wondering what country you are operating out of. I don't know you beyond your username. – mickmackusa Feb 15 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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