This tutorial describes how to apply a default filter to the list of entities displayed in a modal window when updating a link field.
Background Information:
In our application, we have an entity "CollectionsTracker", which we use to track payments received from rental units, and that entity is linked in a many-to-one relationship to a "Tenancy" entity which represents a lease contract for a given Contact entity and a Property entity.
Tenancies can have two possible status values: "active" when the lease is current or "inactive" when the lease has expired.
When a new CollectionsTracker record is created, the Bookkeeper needs to specify to which Tenancy is the tracker linked, thus a list of Tenancies is displayed in a modal window, and we want to make sure that only Tenancies with a status "active" are displayed as default.
Implementation:
1) Define the "Active" filter class for theTenancy entity
custom/Espo/Modules/PropertyManagement/Classes/Select/Tenancy/PrimaryFilters/Active.php
2) Map the filter class to the Tenancy entity
custom/Espo/Modules/PropertyManagement/Recources/metadata/selectDefs/Tenancy.json
3) Specify the default filter in the Tenancy entity clientDefs
custom/Espo/Modules/PropertyManagement/Resources/metadata/clientDefs/Tenancy.json
4) Create a custom field view class for the tenancy link
client/custom/src/views/fields/primary-filtered-link.js
5) Map the custom link field view to the tenancy link in the Collections Tracker entity
custom/Espo/Custom/Resources/metadata/entityDefs/CollectionsTracker.json
6) Clear cache and rebuild
Background Information:
In our application, we have an entity "CollectionsTracker", which we use to track payments received from rental units, and that entity is linked in a many-to-one relationship to a "Tenancy" entity which represents a lease contract for a given Contact entity and a Property entity.
Tenancies can have two possible status values: "active" when the lease is current or "inactive" when the lease has expired.
When a new CollectionsTracker record is created, the Bookkeeper needs to specify to which Tenancy is the tracker linked, thus a list of Tenancies is displayed in a modal window, and we want to make sure that only Tenancies with a status "active" are displayed as default.
Implementation:
1) Define the "Active" filter class for theTenancy entity
custom/Espo/Modules/PropertyManagement/Classes/Select/Tenancy/PrimaryFilters/Active.php
PHP Code:
namespace Espo\Modules\PropertyManagement\Classes\Select\Tenancy\PrimaryFilters;
use Espo\ORM\Query\SelectBuilder;
use Espo\ORM\Query\Part\Condition as Cond;
use Espo\Core\Select\Primary\Filter;
class Active implements Filter
{
public function apply(SelectBuilder $queryBuilder): void
{
$queryBuilder->where(
Cond::equal(
Cond::column('status'),
'Active'
)
);
}
}
2) Map the filter class to the Tenancy entity
custom/Espo/Modules/PropertyManagement/Recources/metadata/selectDefs/Tenancy.json
Code:
{ "primaryFilterClassNameMap": { "active": "Espo\\Modules\\PropertyManagement\\Classes\\Select\\Tenancy\\PrimaryFilters\\Active" } }
custom/Espo/Modules/PropertyManagement/Resources/metadata/clientDefs/Tenancy.json
Code:
{ "defaultFilterData": { "primary": "active" }, }
client/custom/src/views/fields/primary-filtered-link.js
Code:
define('custom:views/fields/primary-filtered-link', ['views/fields/link'], function (Dep) { return Dep.extend({ setup: function() { Dep.prototype.setup.call(this); this.selectPrimaryFilterName = this.getMetadata().get(['clientDefs', this.foreignScope, 'defaultFilterData', 'primary']) || this.selectPrimaryFilterName; } }); });
custom/Espo/Custom/Resources/metadata/entityDefs/CollectionsTracker.json
Code:
{ "fields": { "tenancy": { "type": "link", "tooltip": true, "view": "custom:views/fields/primary-filtered-link" } } }
Comment