Allow Entries fieldtype to configure individual EntryTypes, not full Sections #8686
-
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. |
Beta Was this translation helpful? Give feedback.
Replies: 6 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 |
Beta Was this translation helpful? Give feedback.
-
I would love to add to this a way to only select entries with a certain status (Only published entries for example) |
Beta Was this translation helpful? Give feedback.
-
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:
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. |
Beta Was this translation helpful? Give feedback.
-
I just added a new event to 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.) |
Beta Was this translation helpful? Give feedback.
-
Craft 3.4.16 is out now with that new event. |
Beta Was this translation helpful? Give feedback.
-
We’ve added this for Craft 4! See #10393 for details. |
Beta Was this translation helpful? Give feedback.
We’ve added this for Craft 4! See #10393 for details.