Forcing a Default Filter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lukhaton
    Junior Member
    • Aug 2025
    • 4

    #1

    Forcing a Default Filter

    I'm wondering if anyone knows how to force a default filter on Contact. I've tried several ways without success, my code is never called. Here's an example of my latest attempt:

    <?php
    // File: Contact.php
    //
    // Contact.json
    // {
    // "repositoryClassName": "Espo\\Custom\\Repositories\\Contact"
    // }

    namespace Espo\Custom\Repositories; // Contact.php here

    use Espo\Core\Templates\Repositories\Person;
    use Espo\ORM\Query\SelectBuilder;

    class Contact extends Person
    {
    protected function beforeFind(SelectBuilder $selectBuilder): void
    {
    parent::beforeFind($selectBuilder);

    $selectBuilder->addWhere([
    'cCountryCountryId' => "68a20dfb1f9cdf2ae"
    ]);
    }
    }


    A little help would be appreciated. Thank you in advance.
  • rabii
    Active Community Member
    • Jun 2016
    • 1321

    #2
    You need to use a selectDefs metadata - create a primaryFilters then use the default filter in your contact. below steps:

    1 - Create Country.php under this path (create folders if not already created) custom/Espo/Custom/Classes/Select/Contact/PrimaryFilters/Country.php

    this is the content of the Country.php

    PHP Code:
    <?php
    namespace Espo\Custom\Classes\Select\Contact\PrimaryFilters;
    use 
    Espo\Core\Select\Primary\Filter;
    use 
    Espo\ORM\Query\SelectBuilder;
    class 
    Country implements Filter
    {
        private const 
    COUNTRY_ID '68a20dfb1f9cdf2ae';

        public function 
    apply(SelectBuilder $queryBuilder): void
        
    {
            
    $queryBuilder->where([
                
    'cCountryCountryId' => self::COUNTRY_ID
            
    ]);
        }
    }

    2 - Create contact.json file under custom/Espo/Custom/Resources/metadata/selectDefs/Contact.json

    below content for contact.json​

    PHP Code:
    {
        
    "primaryFilterClassNameMap": {
            
    "country""Espo\\Custom\\Classes\\Select\\Contact\\PrimaryFilters\\Country"
        
    }
    }
    ​ 

    3 - Create/edit if exist contact.json under /custom/Espo/Custom/Resources/metadata/clientDefs/Contact.json

    below content of the file​

    PHP Code:
    {
        
    "defaultFilterData": {
            
    "primary""country"
        
    }


    Once you are done - clear cache and rebuild the system - now when you visit the contact this filter will be applied.

    Hope this helps
    Rabii
    say hey

    Comment

    • lukhaton
      Junior Member
      • Aug 2025
      • 4

      #3
      Thank you very much, I really appreciate your help.

      Comment


      • rabii
        rabii commented
        Editing a comment
        you are welcome
    • lukhaton
      Junior Member
      • Aug 2025
      • 4

      #4
      My "country" filter works fine thanks to your solution. Thanks again.

      However, my filter disappears from the list of available filters if I do the following: 1) Go to Contacts. 2) Select the "All" filter or another filter. 3) Navigate elsewhere: Leads, Home, etc. 4) Return to Contacts. The "country" filter has disappeared from the list.

      As long as I leave the "country" filter selected in Contacts, I can navigate elsewhere, and when I return to Contacts, my filter is still there.

      To get my filter back, I need to clear my browser cache.

      If you have any suggestions for solving this problem, it would be greatly appreciated.

      Thank you in advance.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1321

        #5
        yes because you have not added the filter to the filter list

        Edit contact.json under /custom/Espo/Custom/Resources/metadata/clientDefs/Contact.json and add code below so it looks like below

        PHP Code:
        {
            
        "filterList": [
                
        "__APPEND__",
                
        "country"
            
        ],
            
        "defaultFilterData": {
                
        "primary""country"
            
        }
        }
        ​ 


        also add this to create a translated label for the filter Create/edit if exist contact.json file under custom/Espo/Custom/Resources/i18n/en_US/Contact.json

        PHP Code:
        {
            
        "presetFilters": {
                
        "country""Country"
            
        }
        }
        ​ 


        Note Country​ will be the label of the filter so you can put any name that you think will be suitable the key should always stay country as it reference the class . you can do something as below

        PHP Code:
        {
            
        "presetFilters": {
                
        "country""Country by ID​"
            
        }
        }
        ​ 




        Clear cache and rebuild - refresh contact page and you should always have country as default but also have it on the filter list added.

        Cheers
        Rabii
        say hey

        Comment

        • lukhaton
          Junior Member
          • Aug 2025
          • 4

          #6
          Thank you. Problem solved.

          Comment


          • rabii
            rabii commented
            Editing a comment
            you are welcome
        Working...