Skip to content

Commit f65f872

Browse files
committed
Check all field instances to see if they're dirty/modified/have errors
Resolves craftcms/ckeditor#391
1 parent 5ad0b36 commit f65f872

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Craft CMS 5
22

3+
## Unreleased
4+
5+
- Fixed a bug where changes to multi-instance CKEditor fields weren’t persisting, if the first instance in the field layout hadn’t been modified. ([craftcms/ckeditor#391](https://github.com/craftcms/ckeditor/pull/391))
6+
37
## 5.6.15 - 2025-04-04
48

59
- Fixed an error that could occur when clearing control panel resources, if the `resourceBasePath` setting was set to a nonexistent folder path. ([#17021](https://github.com/craftcms/cms/pull/17021))

src/elements/NestedElementManager.php

+48-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use craft\helpers\Html;
2929
use craft\helpers\StringHelper;
3030
use craft\models\Site;
31+
use Generator;
3132
use Throwable;
3233
use yii\base\Component;
3334
use yii\base\InvalidConfigException;
@@ -683,7 +684,14 @@ private function isDirty(ElementInterface $owner): bool
683684
return $owner->isAttributeDirty($this->attribute);
684685
}
685686

686-
return $owner->isFieldDirty($this->field->handle);
687+
foreach ($this->fieldInstances($owner) as $instance) {
688+
/** @var FieldInterface $instance */
689+
if ($owner->isFieldDirty($instance->handle)) {
690+
return true;
691+
}
692+
}
693+
694+
return false;
687695
}
688696

689697
private function isModified(ElementInterface $owner, bool $anySite = false): bool
@@ -692,13 +700,49 @@ private function isModified(ElementInterface $owner, bool $anySite = false): boo
692700
return $owner->isAttributeModified($this->attribute);
693701
}
694702

695-
return $owner->isFieldModified($this->field->handle, $anySite);
703+
foreach ($this->fieldInstances($owner) as $instance) {
704+
/** @var FieldInterface $instance */
705+
if ($owner->isFieldModified($instance->handle, $anySite)) {
706+
return true;
707+
}
708+
}
709+
710+
return false;
696711
}
697712

698713
private function hasErrors(ElementInterface $owner): bool
699714
{
700-
$attribute = $this->attribute ?? $this->field->handle;
701-
return $owner->hasErrors("$attribute.*");
715+
if (isset($this->attribute)) {
716+
return $owner->hasErrors("$this->attribute.*");
717+
}
718+
719+
foreach ($this->fieldInstances($owner) as $instance) {
720+
/** @var FieldInterface $instance */
721+
if ($owner->hasErrors("$instance->handle.*")) {
722+
return true;
723+
}
724+
}
725+
726+
return false;
727+
}
728+
729+
private function fieldInstances(ElementInterface $owner): Generator
730+
{
731+
if (!isset($this->field)) {
732+
return;
733+
}
734+
735+
if (!$this->field::isMultiInstance()) {
736+
yield $this->field;
737+
return;
738+
}
739+
740+
$customFields = $owner->getFieldLayout()?->getCustomFields() ?? [];
741+
foreach ($customFields as $field) {
742+
if ($field->id === $this->field->id) {
743+
yield $field;
744+
}
745+
}
702746
}
703747

704748
private function saveNestedElements(ElementInterface $owner): void

0 commit comments

Comments
 (0)