From 3a34393634d39681105c738f0c270ab1857ab80a Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Mon, 2 Dec 2024 15:28:49 -0800 Subject: [PATCH] Fallback to all field layouts for event-defined sources --- CHANGELOG.md | 4 ++++ src/elements/Asset.php | 14 +++++++------- src/elements/Category.php | 16 ++++++++-------- src/elements/Entry.php | 12 ++++++------ 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b67ffaefffe..8abb03e3d13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft CMS 4 +## Unreleased + +- Fixed a bug where asset, category, and entry sources defined by the `EVENT_REGISTER_SOURCES` event didn’t have any custom fields available to them, unless the `EVENT_REGISTER_FIELD_LAYOUTS` event was also used to define the available field layouts for the event-defined source. ([#16256](https://github.com/craftcms/cms/discussions/16256)) + ## 4.13.4 - 2024-12-02 - Reduced the likelihood of a deadlock error occurring when updating search indexes. ([#15221](https://github.com/craftcms/cms/issues/15221)) diff --git a/src/elements/Asset.php b/src/elements/Asset.php index 82c3da92eed..529880cdd83 100644 --- a/src/elements/Asset.php +++ b/src/elements/Asset.php @@ -442,14 +442,14 @@ public static function sourcePath(string $sourceKey, string $stepKey, ?string $c */ protected static function defineFieldLayouts(string $source): array { - $fieldLayouts = []; - if ( - preg_match('/^volume:(.+)$/', $source, $matches) && - ($volume = Craft::$app->getVolumes()->getVolumeByUid($matches[1])) - ) { - $fieldLayouts[] = $volume->getFieldLayout(); + if (preg_match('/^volume:(.+)$/', $source, $matches)) { + $volume = Craft::$app->getVolumes()->getVolumeByUid($matches[1]); + return array_filter([ + $volume?->getFieldLayout(), + ]); } - return $fieldLayouts; + + return parent::defineFieldLayouts($source); } /** diff --git a/src/elements/Category.php b/src/elements/Category.php index 089cee9830b..38863ff785b 100644 --- a/src/elements/Category.php +++ b/src/elements/Category.php @@ -212,14 +212,14 @@ protected static function defineSources(string $context): array */ protected static function defineFieldLayouts(string $source): array { - $fieldLayouts = []; - if ( - preg_match('/^group:(.+)$/', $source, $matches) && - ($group = Craft::$app->getCategories()->getGroupByUid($matches[1])) - ) { - $fieldLayouts[] = $group->getFieldLayout(); - } - return $fieldLayouts; + if (preg_match('/^group:(.+)$/', $source, $matches)) { + $group = Craft::$app->getCategories()->getGroupByUid($matches[1]); + return array_filter([ + $group?->getFieldLayout(), + ]); + } + + return parent::defineFieldLayouts($source); } /** diff --git a/src/elements/Entry.php b/src/elements/Entry.php index 84c645f0f8c..938016ab4e6 100644 --- a/src/elements/Entry.php +++ b/src/elements/Entry.php @@ -350,16 +350,16 @@ public static function modifyCustomSource(array $config): array protected static function defineFieldLayouts(string $source): array { // Get all the sections covered by this source - $sections = []; if ($source === '*') { $sections = Craft::$app->getSections()->getAllSections(); } elseif ($source === 'singles') { $sections = Craft::$app->getSections()->getSectionsByType(Section::TYPE_SINGLE); - } elseif ( - preg_match('/^section:(.+)$/', $source, $matches) && - $section = Craft::$app->getSections()->getSectionByUid($matches[1]) - ) { - $sections = [$section]; + } elseif (preg_match('/^section:(.+)$/', $source, $matches)) { + $sections = array_filter([ + Craft::$app->getSections()->getSectionByUid($matches[1]), + ]); + } else { + return parent::defineFieldLayouts($source); } $fieldLayouts = [];