From a8c2120901004eb494efc2a286b51aa58b28f3b2 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Thu, 16 Nov 2023 15:24:15 +0000 Subject: [PATCH] show custom element sources in CKE link to {type} --- src/Field.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Field.php b/src/Field.php index 4677ec9..130d579 100644 --- a/src/Field.php +++ b/src/Field.php @@ -29,6 +29,7 @@ use craft\models\ImageTransform; use craft\models\Section; use craft\models\Volume; +use craft\services\ElementSources; use craft\web\View; use HTMLPurifier_Config; use HTMLPurifier_HTMLDefinition; @@ -668,6 +669,7 @@ private function _linkOptions(?ElementInterface $element = null): array 'elementType' => Category::class, 'refHandle' => Category::refHandle(), 'sources' => $categorySources, + 'criteria' => ['uri' => ':notempty:'], ]; } @@ -732,6 +734,8 @@ private function _sectionSources(?ElementInterface $element = null): array } } + $sources = array_values(array_unique($sources)); + if ($showSingles) { array_unshift($sources, 'singles'); } @@ -740,6 +744,12 @@ private function _sectionSources(?ElementInterface $element = null): array array_unshift($sources, '*'); } + // include custom sources + $customSources = $this->_getCustomSources(Entry::class); + if (!empty($customSources)) { + $sources = array_merge($sources, $customSources); + } + return $sources; } @@ -755,11 +765,19 @@ private function _categorySources(?ElementInterface $element = null): array return []; } - return Collection::make(Craft::$app->getCategories()->getAllGroups()) + $sources = Collection::make(Craft::$app->getCategories()->getAllGroups()) ->filter(fn(CategoryGroup $group) => $group->getSiteSettings()[$element->siteId]?->hasUrls ?? false) ->map(fn(CategoryGroup $group) => "group:$group->uid") ->values() ->all(); + + // include custom sources + $customSources = $this->_getCustomSources(Category::class); + if (!empty($customSources)) { + $sources = array_merge($sources, $customSources); + } + + return $sources; } /** @@ -784,10 +802,37 @@ private function _volumeSources(): array $volumes = $volumes->filter(fn(Volume $volume) => $userService->checkPermission("viewAssets:$volume->uid")); } - return $volumes + $sources = $volumes ->map(fn(Volume $volume) => "volume:$volume->uid") ->values() ->all(); + + // include custom sources + $customSources = $this->_getCustomSources(Asset::class); + if (!empty($customSources)) { + $sources = array_merge($sources, $customSources); + } + + return $sources; + } + + /** + * Returns custom element sources keys for given element type. + * + * @param string $elementType + * @return array + */ + private function _getCustomSources(string $elementType): array + { + $customSources = []; + $elementSources = Craft::$app->getElementSources()->getSources($elementType, 'modal'); + foreach ($elementSources as $elementSource) { + if ($elementSource['type'] === ElementSources::TYPE_CUSTOM && isset($elementSource['key'])) { + $customSources[] = $elementSource['key']; + } + } + + return $customSources; } /**