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

link options - only allow when fs has urls #183

Merged
merged 6 commits into from
Mar 6, 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 @@ -3,6 +3,7 @@
## Unreleased

- CKEditor config edit pages now warn when switching the Config Options setting from JavaScript to JSON if the JavaScript code contains any functions. ([#152](https://github.com/craftcms/ckeditor/issues/152), [#180](https://github.com/craftcms/ckeditor/pull/180))
- Fixed a bug where the “Link to an asset” option was showing up when there weren’t any available volumes with URLs. ([#179](https://github.com/craftcms/ckeditor/issues/179))

## 3.8.0 - 2024-02-21

Expand Down
32 changes: 25 additions & 7 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
'imageTextAlternative',
],
],
'assetSources' => $this->_assetSources(),
'assetSelectionCriteria' => $this->_assetSelectionCriteria(),
'linkOptions' => $this->_linkOptions($element),
'table' => [
'contentToolbar' => [
Expand Down Expand Up @@ -650,7 +652,7 @@ private function _linkOptions(?ElementInterface $element = null): array

$sectionSources = $this->_entrySources($element);
$categorySources = $this->_categorySources($element);
$volumeSources = $this->_assetSources();
$volumeSources = $this->_assetSources(true);

if (!empty($sectionSources)) {
$linkOptions[] = [
Expand All @@ -673,16 +675,12 @@ private function _linkOptions(?ElementInterface $element = null): array
}

if (!empty($volumeSources)) {
$criteria = [];
if ($this->showUnpermittedFiles) {
$criteria['uploaderId'] = null;
}
$linkOptions[] = [
'label' => Craft::t('ckeditor', 'Link to an asset'),
'elementType' => Asset::class,
'refHandle' => Asset::refHandle(),
'sources' => $volumeSources,
'criteria' => $criteria,
'criteria' => $this->_assetSelectionCriteria(),
];
}

Expand Down Expand Up @@ -782,9 +780,10 @@ private function _categorySources(?ElementInterface $element = null): array
/**
* Returns the available asset sources.
*
* @param bool $withUrlsOnly Whether to only return volumes that have filesystems that have public URLs
* @return string[]
*/
private function _assetSources(): array
private function _assetSources(bool $withUrlsOnly = false): array
{
if (!$this->availableVolumes) {
return [];
Expand All @@ -801,6 +800,11 @@ private function _assetSources(): array
$volumes = $volumes->filter(fn(Volume $volume) => $userService->checkPermission("viewAssets:$volume->uid"));
}

if ($withUrlsOnly) {
// only allow volumes that belong to FS that have public URLs
$volumes = $volumes->filter(fn(Volume $volume) => $volume->getFs()->hasUrls);
}

$sources = $volumes
->map(fn(Volume $volume) => "volume:$volume->uid")
->values()
Expand All @@ -815,6 +819,20 @@ private function _assetSources(): array
return $sources;
}

/**
* Returns the asset selection criteria.
*
* @return array
*/
private function _assetSelectionCriteria(): array
{
$criteria = [];
if ($this->showUnpermittedFiles) {
$criteria['uploaderId'] = null;
}
return $criteria;
}

/**
* Returns custom element sources keys for given element type.
*
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js.map

Large diffs are not rendered by default.

26 changes: 12 additions & 14 deletions src/web/assets/ckeditor/src/image/imageinsert/imageinsertui.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class CraftImageInsertUI extends ImageInsertUI {

init() {
// Make sure there are linked volumes
if (!this._linkOption) {
if (!this._assetSources) {
console.warn(
'Omitting the "image" CKEditor toolbar button, because there aren’t any permitted volumes.',
);
Expand All @@ -31,11 +31,8 @@ export default class CraftImageInsertUI extends ImageInsertUI {
componentFactory.add('imageInsert', componentCreator);
}

get _linkOption() {
const linkOptions = this.editor.config.get('linkOptions');
return linkOptions.find(
(option) => option.elementType === 'craft\\elements\\Asset',
);
get _assetSources() {
return this.editor.config.get('assetSources');
}

_createToolbarImageButton(locale) {
Expand All @@ -53,16 +50,17 @@ export default class CraftImageInsertUI extends ImageInsertUI {
}

_showImageSelectModal() {
const sources = this._assetSources;
const editor = this.editor;
const config = editor.config;
const linkOption = this._linkOption;

Craft.createElementSelectorModal(linkOption.elementType, {
storageKey: `ckeditor:${this.pluginName}:${linkOption.elementType}`,
sources: linkOption.sources,
criteria: Object.assign({}, linkOption.criteria, {
kind: 'image',
}),
const criteria = Object.assign({}, config.get('assetSelectionCriteria'), {
kind: 'image',
});

Craft.createElementSelectorModal('craft\\elements\\Asset', {
storageKey: `ckeditor:${this.pluginName}:'craft\\elements\\Asset'`,
sources,
criteria,
defaultSiteId: config.get('elementSiteId'),
transforms: config.get('transforms'),
multiSelect: true,
Expand Down
Loading