[5.x] Matrix field conditional display for entry types #16386
Unanswered
turnstylerj
asked this question in
Ideas
Replies: 2 comments 1 reply
-
I've also just found a use-case for this. Interested to hear if this is feasible! Thanks. |
Beta Was this translation helpful? Give feedback.
0 replies
-
For now, this can be done via a custom module, add something like this to the use craft\fields\Matrix;
use craft\events\DefineEntryTypesForFieldEvent;
...
Event::on(
Matrix::class,
Matrix::EVENT_DEFINE_ENTRY_TYPES,
function(DefineEntryTypesForFieldEvent $event) {
$element = $event->element;
if (
$element instanceof Entry && // Check if the element is an entry
$element->section && // Check if the entry is not a nested entry
$event->sender->handle === 'bodyContent' && // Check if the field is the bodyContent field
$element->section->handle === 'blog' // Check if the entry is in the blog section
) {
foreach ($event->entryTypes as $i => $entryType) {
if (in_array(
$entryType->handle,
[
'hero',
'twoColumnsText',
'features'
])) {
unset($event->entryTypes[$i]);
}
}
}
}); PS: This is just a quick-and-dirty thing for a hobby project, so in real life it may need some kind of configuration instead of hard-coding the rules. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
One of the features I'm missing from Neo with the new Matrix is the ability to conditionally display Entry Types. It's common to have a handful of "core" Entry Types and a few that are only used on one or two entries; we typically give the author a default set of Entry Types for simplicity on day-to-day entry creation, then an advanced option that enables the one-off/all Entry Types.
Beta Was this translation helpful? Give feedback.
All reactions