Back to FormBehavior class

Method chosen

public static void
chosen
(mixed $selector = '.advancedSelect', mixed $debug = null, mixed $options = array())
Method to load the Chosen JavaScript framework and supporting CSS into the document head
Parameters
  • string $selector Class for Chosen elements.
  • mixed $debug Is debugging mode on? [optional]
  • array $options the possible Chosen options as name => value [optional]
Returns
  • void
Since
  • 3.0
Class: FormBehavior
Project: Joomla

Method chosen - Source code

/**
 * Method to load the Chosen JavaScript framework and supporting CSS into the document head
 *
 * If debugging mode is on an uncompressed version of Chosen is included for easier debugging.
 *
 * @param   string  $selector  Class for Chosen elements.
 * @param   mixed   $debug     Is debugging mode on? [optional]
 * @param   array   $options   the possible Chosen options as name => value [optional]
 *
 * @return  void
 *
 * @since   3.0
 */
public static function chosen($selector = '.advancedSelect', $debug = null, $options = array())
{
    if (isset(static::$loaded[__METHOD__][$selector])) {
        return;
    }
    // If no debugging value is set, use the configuration setting
    if ($debug === null) {
        $debug = JDEBUG;
    }
    // Default settings
    if (!isset($options['disable_search_threshold'])) {
        $options['disable_search_threshold'] = 10;
    }
    // Allow searching contains space in query
    if (!isset($options['search_contains'])) {
        $options['search_contains'] = true;
    }
    if (!isset($options['allow_single_deselect'])) {
        $options['allow_single_deselect'] = true;
    }
    if (!isset($options['placeholder_text_multiple'])) {
        $options['placeholder_text_multiple'] = Text::_('JGLOBAL_TYPE_OR_SELECT_SOME_OPTIONS');
    }
    if (!isset($options['placeholder_text_single'])) {
        $options['placeholder_text_single'] = Text::_('JGLOBAL_SELECT_AN_OPTION');
    }
    if (!isset($options['no_results_text'])) {
        $options['no_results_text'] = Text::_('JGLOBAL_SELECT_NO_RESULTS_MATCH');
    }
    // Options array to json options string
    $options_str = \json_encode($options, $debug && \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
    // Add chosen.js assets
    /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
    $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
    $wa->usePreset('chosen')->registerAndUseScript('joomla-chosen', 'legacy/joomla-chosen.min.js', [], [], ['chosen'])->addInlineScript("\n\t\tjQuery(document).ready(function (){\n\t\t\tjQuery('" . $selector . "').jchosen(" . $options_str . ");\n\t\t});\n\t");
    static::$loaded[__METHOD__][$selector] = true;
}