Skip to content

Commit

Permalink
show custom element sources in CKE link to {type}
Browse files Browse the repository at this point in the history
  • Loading branch information
i-just committed Nov 16, 2023
1 parent 1b15638 commit a8c2120
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -668,6 +669,7 @@ private function _linkOptions(?ElementInterface $element = null): array
'elementType' => Category::class,
'refHandle' => Category::refHandle(),
'sources' => $categorySources,
'criteria' => ['uri' => ':notempty:'],
];
}

Expand Down Expand Up @@ -732,6 +734,8 @@ private function _sectionSources(?ElementInterface $element = null): array
}
}

$sources = array_values(array_unique($sources));

if ($showSingles) {
array_unshift($sources, 'singles');
}
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit a8c2120

Please sign in to comment.