diff --git a/src/Form/Fields/Formatters/EditorEmbedsFormatter.php b/src/Form/Fields/Formatters/EditorEmbedsFormatter.php
index 7e5e74b46..f1fe0ed57 100644
--- a/src/Form/Fields/Formatters/EditorEmbedsFormatter.php
+++ b/src/Form/Fields/Formatters/EditorEmbedsFormatter.php
@@ -7,6 +7,7 @@
use Code16\Sharp\Form\Fields\SharpFormUploadField;
use Code16\Sharp\Utils\Fields\Formatters\FormatsEditorEmbeds;
use Code16\Sharp\Utils\Fields\Formatters\FormatsHtmlContent;
+use DOMElement;
use Illuminate\Support\Str;
class EditorEmbedsFormatter extends SharpFieldFormatter implements FormatsAfterUpdate
@@ -66,10 +67,7 @@ function (string $content) use ($field, $value) {
if ($fieldKey === 'slot') {
$this->setInnerHtml($element, $fieldValue);
} else {
- $element->setAttribute(
- Str::kebab($fieldKey),
- is_array($fieldValue) ? json_encode($fieldValue) : $fieldValue
- );
+ $this->setAttribute($element, $fieldKey, $fieldValue);
}
}
}
@@ -113,10 +111,7 @@ function (string $content) use ($field) {
$this->tryJsonDecode($element->getAttribute(Str::kebab($fieldKey)))
);
- $element->setAttribute(
- Str::kebab($fieldKey),
- is_array($formatted) ? json_encode($formatted) : $formatted
- );
+ $this->setAttribute($element, $fieldKey, $formatted);
}
}
}
@@ -126,4 +121,15 @@ function (string $content) use ($field) {
}
);
}
+
+ protected function setAttribute(DOMElement $element, string $fieldKey, mixed $value): void
+ {
+ $attribute = Str::kebab($fieldKey);
+
+ if ($value === false || $value === null) {
+ $element->removeAttribute($attribute);
+ } else {
+ $element->setAttribute($attribute, is_array($value) ? json_encode($value) : $value);
+ }
+ }
}
diff --git a/tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php b/tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php
index 4707333fc..fc7718bce 100644
--- a/tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php
+++ b/tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php
@@ -350,6 +350,8 @@
'width' => 600,
'height' => 600,
],
+ 'check' => null,
+ 'nullable' => null,
'_html' => sprintf('
My contentful content',
$thumbnail,
),
@@ -373,6 +375,7 @@
(new EditorFormatterTestEmbed())->key() => [
'0' => [
'slot' => 'My contentful content',
+ 'check' => true,
'visual' => [
'name' => 'image.jpg',
'path' => 'data/Posts/1/image.jpg',
@@ -386,7 +389,7 @@
],
],
],
- ]))->toEqual(sprintf('My contentful content',
+ ]))->toEqual(sprintf('My contentful content',
e(json_encode([
'file_name' => 'data/Posts/1/image.jpg',
'size' => 120,
@@ -396,6 +399,26 @@
));
});
+it('allows to format embeds with false/null values from front', function () {
+ $formatter = (new EditorFormatter())->setInstanceId(1);
+ $field = SharpFormEditorField::make('md')
+ ->allowEmbeds([EditorFormatterTestEmbed::class]);
+
+ expect($formatter->fromFront($field, 'attribute', [
+ 'text' => <<<'HTML'
+
+ HTML,
+ 'embeds' => [
+ (new EditorFormatterTestEmbed())->key() => [
+ '0' => [
+ 'check' => false,
+ 'nullable' => null,
+ ],
+ ],
+ ],
+ ]))->toEqual('');
+});
+
it('allows to format a unicode text value from front', function () {
// This test was created to demonstrate preg_replace failure
// without the unicode modifier
diff --git a/tests/Unit/Form/Fields/Formatters/Fixtures/EditorFormatterTestEmbed.php b/tests/Unit/Form/Fields/Formatters/Fixtures/EditorFormatterTestEmbed.php
index 2ad1caa56..f3c30e679 100644
--- a/tests/Unit/Form/Fields/Formatters/Fixtures/EditorFormatterTestEmbed.php
+++ b/tests/Unit/Form/Fields/Formatters/Fixtures/EditorFormatterTestEmbed.php
@@ -4,6 +4,7 @@
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
use Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed;
+use Code16\Sharp\Form\Fields\SharpFormCheckField;
use Code16\Sharp\Form\Fields\SharpFormTextField;
use Code16\Sharp\Form\Fields\SharpFormUploadField;
use Code16\Sharp\Utils\Fields\FieldsContainer;
@@ -23,6 +24,8 @@ public function buildFormFields(FieldsContainer $formFields): void
{
$formFields
->addField(SharpFormTextField::make('slot'))
+ ->addField(SharpFormCheckField::make('check', 'Check'))
+ ->addField(SharpFormTextField::make('nullable'))
->addField(SharpFormUploadField::make('visual')->setImageOnly());
}
diff --git a/tests/Unit/Show/Fields/Formatters/TextFormatterTest.php b/tests/Unit/Show/Fields/Formatters/TextFormatterTest.php
index 8bc27aba5..5038ea413 100644
--- a/tests/Unit/Show/Fields/Formatters/TextFormatterTest.php
+++ b/tests/Unit/Show/Fields/Formatters/TextFormatterTest.php
@@ -140,6 +140,8 @@
'0' => [
'slot' => 'My contentful content',
'visual' => null,
+ 'check' => null,
+ 'nullable' => null,
'_html' => '
My contentful content',
],
],