We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5811
    • 1,717 Posts

    How to implement a category with AjaxSearch 1.9.0 ...

    for Ajax mode see as example : http://www.modx2.wangba.fr/index.php?id=204

    [!AjaxSearch? &category=`articleCategory` &config=`custom2` &tvPhx=`articleImage` 
    
    &ajaxMax=`4` &showMoreResults=`1` &moreResultsPage=`[[GetFirstChild]]`!]


    1/ &category=`articleCategory`
    first you should decide which tv name will be used to tag as category your Modx documents
    e.g: on the demo site I have defined a tv named "articleCategory"

    So each document have a tv with a value among : arts,geography,music,litterature, ...


    2/ for each group of results (category) you could decide to:
    - change the way how the results are displayed
    - the title of each category

    For this use &config=`custom2` And create a file named "custom2.config.php" in the /configs directory with a categConfig function

    How to set up this categConfig function ?
    if(!function_exists('categConfig')) {
        function categConfig($site='defsite',$category){
            $config = array();
            ... // something should be added here
            return $config;
        }
    }

    1/ you should avoid to have a function define twice, so you should use:
    if(!function_exists('categConfig')) {
    }


    2/ the categConfig function has two parameters:
    - the site name, by default ’defsite’
    - the category name

    3/ then you should return an array $config

    4/ then you could add for each category some config parameters
    e.g:

        /*
         *  categConfig :   To define the display of categories (output)
         *  Add a category as a switch item. 'uncategorized' item describe the results outside of any category
         *  Add a switch for a new site. The default site is named 'defsite'.
         *  Allowed config parameters : grpLabel, tplResult, tplAjaxResult, display, extract, rank ...
         */
        if(!function_exists('categConfig')) {
            function categConfig($site='defsite',$category){
                $config = array();
                $site = strtolower($site);
                $category = strtolower($category);
                switch($site) {
                    case 'defsite':
                    switch($category){
                        case 'arts':
                            $config['grpLabel'] = 'Arts and Painting';
                            $config['tplAjaxResult'] = 'imgResult';      // allow the display of an image
                            break;
                        case 'music':
                            $config['grpLabel'] = 'Music';
                            $config['tplAjaxResult'] = 'imgAjaxResult';      // allow the display of an image
                            break;
                        case 'geography':
                            $config['grpLabel'] = 'Geography';
                            $config['tplAjaxResult'] = 'imgAjaxResult';
                            break;
                        case '':
                            $config['grpLabel'] = 'Site wide';
                            break;
                    }
                }
                return $config;
            }
        }
    e.g:
        $config['grpLabel'] = 'Arts and Painting';
        $config['tplAjaxResult'] = 'imgResult';      // allow the display of an image


    by using ’grpLabel’, you could redefined the group of results. For instance rather to have ’arts’ as group name
    we prefer "Arts and painting’

    with $config[’tplAjaxResult’] = ’imgResult’; we decide to change the ajaxResult template by using the "imgAjaxResult"
    template. Why. because for the "arts" category we would like display an image with the result. Only for this category.
    In the snippet call we use &tvPhx=`articleImage` to get the image as placeholder for each result.

    But You can also, decide to locally redefined, the ajaxMax parameter, the extract parameter or the rank parameter.
    It’s as you want and valid only for the categories you choose.


    for non ajax mode : http://www.modx2.wangba.fr/index.php?id=126