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 @daft.

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.

Hi,

I wonder if I do something wrong because it’s not working as expected for me. Maybe I understand the feature wrong.

I have a blueprint like the following for Features:

handle: Lists\Features
type: structure
name: Features
drafts: false
multisite: sync
pagefinder: false

structure:
    maxDepth: 1

fields:
    always_included:
        label: Set this feature to always be include in all models
        type: switch
        default: false
        translatable: false

In another blueprint, for the Models, I have a field using these Features. However, I don’t want to let the users select those features that are always included because, well, they are!

handle: Site\Models
type: stream
name: Models
drafts: false
multisite: sync
pagefinder: item

fields:
    features:
        label: Included features of the model
        type: entries
        source: Lists\Features
        conditions: always_included = false
        displayMode: taglist
        translatable: false

I was expecting the condition to kick in and that the Features marked as “always included” would not appear in the Model’s taglist, but they do.

What is it that I do not grasp?

Thanks for your help!