Forcing a Default Filter

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

    #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
    • 1311

    #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
    Here to help :)

    Comment

    Working...