Add condition to "entries" field type

Hello,

I would like to link some categories to blog posts, but not all.

If I use the “entries” field (to link categories to blogs), I don’t feel like there is a way to add a condition to display only certain categories.

One solution would be to use a dynamic dropdown/checkbox field.
But then the problem is that the value is not considered as a relationship.

The only solution is to separate each group of categories into differents “entry”?

Thanks!

PS: The same problem arises if you want to use a “relational” field in a form field. Since the “entries” types do not seem to work (only dropdown/checkbox fields work)

1 Like

Hi and welcome @MallauryGerard

I wonder if you can specify a scope with the entries field.

basic_entries:
    label: Basic Entry
    type: entries
    source: Basic\Entry
    scope: 'MyPhpClass::myMethod'

Then use the scope to filter the query…

Hello again, @MallauryGerard. We have investigated this requirement and added support for it October CMS v3.1.25. Also, the following has been added to the documentation:


Applying Conditions

You can restrict the related query using SQL or PHP using the approaches below. In the examples, the related record has a field called is_featured that renders as a checkbox. We can limit the related records to only those that have this checkbox marked.

SQL Query Condition

You may limit the related model using a raw SQL query using the conditions property.

categories:
    label: Categories
    type: entries
    source: Blog\Category
    conditions: is_featured = true

PHP Query Scope

You may limit the related query using a PHP method with the scope property.

basic_entries:
    label: Basic Entry
    type: entries
    source: Basic\Entry
    scope: App\Classes\ScopeHelper::applyScope

This would refer to the App\Classes\ScopeHelper class that may look a file located in app/classes/ScopeHelper.php, for example.

<?php namespace App\Classes;

class ScopeHelper
{
    public static function applyScope($query)
    {
        return $query->where('is_featured', true);
    }
}
1 Like

Hello,

Nice. Great job!

Thank you for your responsiveness.

1 Like

Thanks for the updates @daftspunky.

I can see the documentation is updated with the new feature but the current release is still v3.1.24.
When do you think the v3.1.25 will be released?

Thanks

Hi @abbasadel

It has been released today. Thanks.