Cms: Allow Entries fieldtype to configure individual EntryTypes, not full Sections

Created on 22 May 2019  Â·  5Comments  Â·  Source: craftcms/cms

The Entries fieldtype configuration lets you select Sources which are Sections. I've had several cases where I wanted to allow users to only be able to select one or two EntryTypes within a Section.

Example: An 'Events' section that has 3 EntryTypes: Webinar, In-Person, Holidays. Currently, you'd have to create a field and define an Instruction that says to only pick Holiday events even though the UI allows them to select anything.

new-field

content modeling enhancement

Most helpful comment

I just added a new event to craft\fields\BaseRelationField for the next release (3.4.16), which makes it possible to customize relational fields’ element criteria right at the field level.

Going with the OP example:

use craft\events\ElementCriteriaEvent;
use craft\fields\Entries;
use yii\base\Event;

// Only allow Holiday entries to be selected
Event::on(
    Entries::class,
    Entries::EVENT_DEFINE_SELECTION_CRITERIA,
    function(ElementCriteriaEvent $e) {
        /** @var Entries $field */
        $field = $e->sender;

        if ($field->handle === 'relatedHolidays') {
            $e->criteria['type'] = 'holiday';
        }
    }
);

(Still planning on making this a core feature down the road, so not going to close the issue.)

All 5 comments

I also think this is a very useful feature. I mostly have a usecase for it in structures, where there is often a real difference between a parent and it's children. But in general I miss this feature regularly

I would love to add to this a way to only select entries with a certain status (Only published entries for example)

In the mean time before this gets implemented in core, it's actually really easy and low-risk to do it yourself in a custom module! For example, I have a structure "Pages" with some pages being product pages. The following does two things:

  1. Adds a new section for "Product Pages" in the "Entries" element list of the CMS
  2. Adds "Product Pages" as an option on Entry field settings.
if (Craft::$app->getRequest()->getIsCpRequest()) {
            // Allow selecting only product pages
            Event::on(Entry::class, Element::EVENT_REGISTER_SOURCES, function(RegisterElementSourcesEvent $event) {
                $newSource = [
                    'key' => 'view:products',
                    'label' => 'Product Pages',
                    'criteria' => [
                        'sectionId' => [8], // Pages section ID
                        'typeId' => [26], // Product Page entry type
                        'editable' => true // Make sure the current user has permission to edit
                    ]
                ];
                $event->sources[] = $newSource;
            });
        }

This is a little known feature that adds a ton of value to the CMS - this is also really helpful for building "audit" sections in the CP. For example, for another client, while entering products, I wanted to be able to allow them to quickly view all products without thumbnails. The code above can be easily tweaked to filter by fields values, since it's just parsed to an element query.

I just added a new event to craft\fields\BaseRelationField for the next release (3.4.16), which makes it possible to customize relational fields’ element criteria right at the field level.

Going with the OP example:

use craft\events\ElementCriteriaEvent;
use craft\fields\Entries;
use yii\base\Event;

// Only allow Holiday entries to be selected
Event::on(
    Entries::class,
    Entries::EVENT_DEFINE_SELECTION_CRITERIA,
    function(ElementCriteriaEvent $e) {
        /** @var Entries $field */
        $field = $e->sender;

        if ($field->handle === 'relatedHolidays') {
            $e->criteria['type'] = 'holiday';
        }
    }
);

(Still planning on making this a core feature down the road, so not going to close the issue.)

Craft 3.4.16 is out now with that new event.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukebailey picture lukebailey  Â·  3Comments

bitboxfw picture bitboxfw  Â·  3Comments

angrybrad picture angrybrad  Â·  3Comments

mccombs picture mccombs  Â·  3Comments

timkelty picture timkelty  Â·  3Comments