Announcement

Collapse
No announcement yet.

How To: Own Primary Filter EspoCRM 7.0.x

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How To: Own Primary Filter EspoCRM 7.0.x

    Hello! I have been busy solving my own primary filters a few days ago. The existing posts were only partly of help, as the basic structure (Select Manager to Select Builder) has been changed in version 7.

    Attached is a little help. I hope it helps someone.

    My initial situation: The entity contact has got a new enum field "ownState". Possible entries are "activce, feedback, inactive". The default value is "active". I wanted a filter like for the leads "actual" and "inactive".

    Folders or files may need to be created.

    /custom/Espo/Custom/Select/Contact/PrimaryFilters/Actual.php
    PHP Code:
    <?php
    namespace Espo\Custom\Select\Contact\PrimaryFilters;
    use 
    Espo\Core\Select\Primary\Filter;
    use 
    Espo\ORM\Query\SelectBuilder;
    use 
    Espo\ORM\Query\Part\Condition as Cond;
    class 
    Actual implements Filter
    {
    public function 
    apply(SelectBuilder $queryBuilder): void
    {
    $queryBuilder->where(
    Cond::in(
    Cond::column('ownState'),
    [
    'active','feedback']
    )
    );
    }
    }

    /custom/Espo/Custom/Select/Contact/PrimaryFilters/Inactive.php
    PHP Code:
    <?php
    namespace Espo\Custom\Select\Contact\PrimaryFilters;
    use 
    Espo\Core\Select\Primary\Filter;
    use 
    Espo\ORM\Query\SelectBuilder;
    use 
    Espo\ORM\Query\Part\Condition as Cond;
    class 
    Inactive implements Filter
    {
    public function 
    apply(SelectBuilder $queryBuilder): void
    {
    $queryBuilder->where(
    Cond::in(
    Cond::column('ownState'),
    [
    'inactive']
    )
    );
    }
    }

    /custom/Espo/Custom/Ressources/metadata/selectDefs/Contact.json
    Code:
    {
    "primaryFilterClassNameMap": {
    "actual": "Espo\\Custom\\Select\\Contact\\PrimaryFilters\\Actual",
    "inactive": "Espo\\Custom\\Select\\Contact\\PrimaryFilters\\Inactive"
    }
    }

    /custom/Espo/Custom/Ressources/metadata/clientDefs/Contact.json
    Add the following value

    Code:
    "filterList":[
    {
    "name":"actual"
    },
    {
    "name":"inactive"
    }
    ]

    /custom/Espo/Custom/Ressources/i18n/de_DE/Contact.json
    Please select the appropriate language. In my case German. Add the following value

    Code:
    "presetFilters": {
    "actual": "Aktuell",
    "inactive": "Archiv"
    }

    Finally
    • Administation: Clear Cache
    • Reload Site in Browser
    The description of the API for condition and more can be found under
    /application/Espo/ORM/Query/Part/

  • #2
    The list of all metadata parameters related to Select framework is available here: https://docs.espocrm.com/development...a/select-defs/

    Comment


    • #3
      I'm having issues with custom filters following this guide, perhaps someone could take a look at what I might have done incorrectly.
      The filter is present in the list, when I click it, I get error 500

      This is what the logs gave me:
      Code:
      ERROR: Uncaught Exception JsonException: "JSON syntax error in 'custom/Espo/Custom/Resources/metadata/clientDefs/FilterTest.json'." at /var/www/html/application/Espo/Core/Utils/File/Unifier.php line 240 {"exception":"[object] (JsonException(code: 0): JSON syntax error in 'custom/Espo/Custom/Resources/metadata/clientDefs/FilterTest.json'. at /var/www/html/application/Espo/Core/Utils/File/Unifier.php:240)"} []
      And this is the contents of said clientDefs/FilterTest.json:

      Code:
      {
      "controller": "controllers/record",
      "boolFilterList": [
      "onlyMy"
      ],
      "filterList": [{
      "name": "3"
      }
      ]
      }

      This was done on an English version, of a clean install, v. 7.1.10, made specifically for testing this filter guide.



      yuri Are you by any chance planning to implement an easier way of doing this in future releases?

      Comment


      • #4
        > Are you by any chance planning to implement an easier way of doing this in future releases?

        Please don't ask me such things, it frustrates me. It's already very easy. Implement 1 interface, add something in metadata. Come on. It's programming, it requires a little bit of effort. We can't have just ONE BUTTON named "Do whatever I want for me".

        Comment


        • DashingUno
          DashingUno commented
          Editing a comment
          I understand your frustration boss, but also please look at it from user's side as well.
          Since there is no official guide on implementing custom filters, what we are left with is courtesy of other users.

          The first thing you encounter when searching for that, is the Dev CRM guide, not everybody is a mid-high level programmer, so you spend time understanding the structure and what not, you implement it - it doesn't work.
          Next thing you do is search for the error that you got, and find out that the function is depreciated since 7.*. and you just wasted half an hour on outdated material.

          Then you get to this guide, but this isn't a guide rather than 1 mans experience for a specific use-case, with spelling mistakes in official file paths.
          Which leads to a logical conclusion of asking the almighty main dev, whether this will be implemented in future releases since there are multiple threads on the forum about it.

          I feel bad that my question frustrated you, and I apologize for it, but I think unless there is an official guide for it, or a UI implementation to create custom filters, the same question will popup on your feed every 6 months or so.

        • esforim
          esforim commented
          Editing a comment
          A+ for the effort @DashingUno

          As we are programming-illiterate, we are at the mercy of the generosity of what is shared.

      • #5
        Read the message in the log. It tells you all needed information what you did wrong. Your JSON is not valid.

        Comment


        • DashingUno
          DashingUno commented
          Editing a comment
          Thank you for pointing me in the right direction

      • #6
        My issue was: It works as described in the first post it just doesn't work with "3", considers it an "unexpected integer".
        After I changed "3" to "Randomtext" - worked on the first try.

        I for one didn't know that you can't use Integers in filter naming.

        Is that common php knowledge, or Espo specific ?

        Comment


        • #7
          Needed to set up another filter and encountered an issue with this example, it wont filter if the filed is a link type.

          As far as I understand this is the part that is responsible for it

          PHP Code:
          $queryBuilder->where(
          Cond::in(
          Cond::column('typeincome'),
          [
          'Rent']​ 
          Im trying to filter "Transaction" entity by a field "typeincome", which is a link to a "typeincome" entity, which has a "Rent" object, is it possible to use it as a filter?

          Comment

          Working...
          X