Skip to content

Commit

Permalink
Merge branch '4.5' into feature/ckeditor-44
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jan 23, 2025
2 parents 98e91bf + a62e82d commit 8101a03
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

## Unreleased

- CKEditor fields no longer have extra bottom padding, and the CKEditor logo is now displayed over the bottom border. ([#252](https://github.com/craftcms/ckeditor/pull/252))
- Added the ability to show the “Source” button for specific user groups. ([#318](https://github.com/craftcms/ckeditor/pull/318))
- Added `craft\ckeditor\Field::$sourceEditingGroups`.
- Deprecated `craft\ckeditor\Field::$enableSourceEditingForNonAdmins`.
- Fixed a bug where “Edit entry” buttons were only partially translated. ([#337](https://github.com/craftcms/ckeditor/issues/337))
- Fixed a bug where copying and pasting nested entries wasn’t working on non-primary sites. ([#315](https://github.com/craftcms/ckeditor/issues/315))
- Fixed an error that could occur when editing nested entries on newly-created provisional drafts. ([#339](https://github.com/craftcms/ckeditor/pull/339))

## 4.4.0 - 2024-11-12

Expand Down
87 changes: 80 additions & 7 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ private static function adjustFieldValues(
public ?string $defaultTransform = null;

/**
* @var bool Whether to enable source editing for non-admin users.
* @since 3.3.0
* @var string|string[]|null User groups whose members should be able to see the “Source” button
* @since 4.5.0
*/
public bool $enableSourceEditingForNonAdmins = false;
public string|array|null $sourceEditingGroups = ['__ADMINS__'];

/**
* @var bool Whether to show volumes the user doesn’t have permission to view.
Expand Down Expand Up @@ -452,6 +452,13 @@ public function __construct($config = [])
$config['removeNbsp'],
);

if (isset($config['enableSourceEditingForNonAdmins'])) {
$config['sourceEditingGroups'] = $config['enableSourceEditingForNonAdmins'] ? '*' : ['__ADMINS__'];
unset($config['enableSourceEditingForNonAdmins']);
} elseif (array_key_exists('sourceEditingGroups', $config) && empty($config['sourceEditingGroups'])) {
$config['sourceEditingGroups'] = null;
}

if (isset($config['entryTypes']) && $config['entryTypes'] === '') {
$config['entryTypes'] = [];
}
Expand Down Expand Up @@ -603,6 +610,22 @@ public function getSettingsHtml(): ?string
{
$view = Craft::$app->getView();

$userGroupOptions = [
[
'label' => Craft::t('app', 'Admins'),
'value' => '__ADMINS__',
],
];

foreach (Craft::$app->getUserGroups()->getAllGroups() as $group) {
if ($group->can('accessCp')) {
$userGroupOptions[] = [
'label' => $group->name,
'value' => $group->uid,
];
}
}

$volumeOptions = [];
foreach (Craft::$app->getVolumes()->getAllVolumes() as $volume) {
if ($volume->getFs()->hasUrls) {
Expand All @@ -623,6 +646,7 @@ public function getSettingsHtml(): ?string

return $view->renderTemplate('ckeditor/_field-settings.twig', [
'field' => $this,
'userGroupOptions' => $userGroupOptions,
'purifierConfigOptions' => $this->configOptions('htmlpurifier'),
'volumeOptions' => $volumeOptions,
'transformOptions' => $transformOptions,
Expand Down Expand Up @@ -855,7 +879,7 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
ArrayHelper::removeValue($toolbar, 'createEntry');
}

if (!$this->enableSourceEditingForNonAdmins && !Craft::$app->getUser()->getIsAdmin()) {
if (!$this->isSourceEditingAllowed(Craft::$app->getUser()->getIdentity())) {
ArrayHelper::removeValue($toolbar, 'sourceEditing');
}

Expand Down Expand Up @@ -920,7 +944,7 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
'ui' => [
'viewportOffset' => ['top' => 44],
'poweredBy' => [
'position' => 'inside',
'position' => 'outside',
'label' => '',
],
],
Expand Down Expand Up @@ -1101,7 +1125,7 @@ protected function prepValueForInput($value, ?ElementInterface $element, bool $s
}

$value = $chunks
->map(function(BaseChunk $chunk) use ($static, $entries) {
->map(function(BaseChunk $chunk) use ($static, $entries, $element) {
if ($chunk instanceof Markup) {
return $chunk->rawHtml;
}
Expand All @@ -1110,6 +1134,13 @@ protected function prepValueForInput($value, ?ElementInterface $element, bool $s
$entry = $entries[$chunk->entryId];

try {
// set up-to-date owner on the entry
// e.g. when provisional draft was created for the owner and the page was reloaded
$entry->setOwner($element);
if ($entry->id === $entry->getPrimaryOwnerId()) {
$entry->setPrimaryOwner($element);
}

$cardHtml = $this->getCardHtml($entry);
} catch (InvalidConfigException) {
// this can happen e.g. when the entry type has been deleted
Expand Down Expand Up @@ -1155,7 +1186,33 @@ protected function prepValueForInput($value, ?ElementInterface $element, bool $s
}

/**
* @innheritdoc
* Returns if user belongs to a group whose members are allowed to edit source even if they're not admins
* @param User $user
* @return bool
*/
private function isSourceEditingAllowed(User $user): bool
{
if ($this->sourceEditingGroups === '*') {
return true;
}

$sourceEditingGroups = array_flip($this->sourceEditingGroups);

if ($user->admin && isset($sourceEditingGroups['__ADMINS__'])) {
return true;
}

foreach ($user->getGroups() as $group) {
if (isset($sourceEditingGroups[$group->uid])) {
return true;
}
}

return false;
}

/**
* @inheritdoc
*/
protected function searchKeywords(mixed $value, ElementInterface $element): string
{
Expand Down Expand Up @@ -1666,4 +1723,20 @@ private function _describedBy(View $view): string

return '';
}

/**
* @deprecated in 4.5.0
*/
public function getEnableSourceEditingForNonAdmins(): bool
{
return $this->sourceEditingGroups === '*';
}

/**
* @deprecated in 4.5.0
*/
public function setEnableSourceEditingForNonAdmins(bool $value): void
{
$this->sourceEditingGroups = $value ? '*' : ['__ADMINS__'];
}
}
19 changes: 13 additions & 6 deletions src/templates/_field-settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@
on: field.showWordCount
}) }}

{{ forms.lightswitchField({
id: 'enable-source-editing-for-non-admins',
name: 'enableSourceEditingForNonAdmins',
label: 'Show the “Source” button for non-admin users?'|t('ckeditor'),
on: field.enableSourceEditingForNonAdmins,
}) }}
{% if userGroupOptions|length %}
<div id="source-editing-groups">
{{ forms.checkboxSelectField({
id: 'sourceEditingGroups',
name: 'sourceEditingGroups',
label: 'Who should see the “Source” button?'|t('ckeditor'),
options: userGroupOptions,
values: field.sourceEditingGroups,
allLabel: 'All users'|t('app'),
showAllOption: true,
}) }}
</div>
{% endif %}

<hr>

Expand Down
2 changes: 1 addition & 1 deletion src/translations/en/ckeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
'No transform' => 'No transform',
'Purify HTML' => 'Purify HTML',
'Removes any potentially-malicious code on save, by running the submitted data through {link}.' => 'Removes any potentially-malicious code on save, by running the submitted data through {link}.',
'Show the “Source” button for non-admin users?' => 'Show the “Source” button for non-admin users?',
'Show word count' => 'Show word count',
'Site: {name}' => 'Site: {name}',
'The default transform that should be applied when inserting an image.' => 'The default transform that should be applied when inserting an image.',
Expand All @@ -45,6 +44,7 @@
'This field doesn’t allow nested entries.' => 'This field doesn’t allow nested entries.',
'Toolbar' => 'Toolbar',
'View available settings' => 'View available settings',
'Who should see the “Source” button?' => 'Who should see the “Source” button?',
'Word Limit' => 'Word Limit',
'You can save custom {name} configs as {ext} files in {path}.' => 'You can save custom {name} configs as {ext} files in {path}.',
'Your JavaScript config contains functions. If you switch to JSON, they will be lost. Would you like to continue?',
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.

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8101a03

Please sign in to comment.