Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show custom element sources in CKE link to {type} #149

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Link element selector modals now include custom sources. ([#139](https://github.com/craftcms/ckeditor/issues/139))
- Fixed a bug where the CKEditor config-creation slideout could keep reappearing if canceled. ([#138](https://github.com/craftcms/ckeditor/pull/138))
- Fixed a conflict with `nystudio107/craft-code-editor` 1.0.14 and 1.0.15. ([#150](https://github.com/craftcms/ckeditor/issues/150))

Expand Down
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