diff --git a/src/Forms/HTMLEditor/HTMLEditorField.php b/src/Forms/HTMLEditor/HTMLEditorField.php
index 2675875d6..3edd623f7 100644
--- a/src/Forms/HTMLEditor/HTMLEditorField.php
+++ b/src/Forms/HTMLEditor/HTMLEditorField.php
@@ -114,20 +114,10 @@ class HTMLEditorField extends TextareaField
public function getAttributes()
{
- $config = $this->getEditorConfig();
- // Fix CSS height based on rows
- $rowHeight = $this->config()->get('fixed_row_height');
- $attributes = [];
- if ($rowHeight && ($config instanceof TinyMCEConfig)) {
- $height = $this->getRows() * $rowHeight;
- $attributes['style'] = sprintf('height: %dpx;', $height);
- $config = clone $config;
- $config->setOption('height', 'auto');
- }
+ $config = $this->setEditorHeight($this->getEditorConfig());
// Merge attributes
return array_merge(
- $attributes,
parent::getAttributes(),
$config->getAttributes()
);
@@ -190,7 +180,7 @@ class HTMLEditorField extends TextareaField
public function getSchemaStateDefaults()
{
$stateDefaults = parent::getSchemaStateDefaults();
- $config = $this->getEditorConfig();
+ $config = $this->setEditorHeight($this->getEditorConfig());
$stateDefaults['data'] = $config->getConfigSchemaData();
return $stateDefaults;
}
@@ -204,4 +194,23 @@ class HTMLEditorField extends TextareaField
{
return htmlentities($this->Value() ?? '', ENT_COMPAT, 'UTF-8', false);
}
+
+ /**
+ * Set height of editor based on number of rows
+ */
+ private function setEditorHeight(HTMLEditorConfig $config): HTMLEditorConfig
+ {
+ $rowHeight = $this->config()->get('fixed_row_height');
+ if ($rowHeight && ($config instanceof TinyMCEConfig)) {
+ $rows = (int) $this->getRows();
+ $height = $rows * $rowHeight;
+ $config = clone $config;
+ if ($height) {
+ $config->setOption('height', 'auto');
+ $config->setOption('row_height', sprintf('%dpx', $height));
+ }
+ }
+
+ return $config;
+ }
}
diff --git a/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php b/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
index e09d5d7d4..bffd7c661 100644
--- a/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
+++ b/tests/php/Forms/HTMLEditor/HTMLEditorFieldTest.php
@@ -267,4 +267,43 @@ EOS
$editor->saveInto($obj);
$this->assertEquals($htmlValue, $obj->Content, 'Table is removed');
}
+
+ public function testGetAttributes()
+ {
+ // Create an editor and set fixed_row_height to 0
+ $editor = HTMLEditorField::create('Content');
+ $editor->config()->set('fixed_row_height', 0);
+ // Get the attributes and config from the editor
+ $attributes = $editor->getAttributes();
+ $data_config = json_decode($attributes['data-config'], true);
+ // If fixed_row_height is 0 then row_height and height config are not set
+ $this->assertArrayNotHasKey('height', $data_config, 'Config height should not be set');
+ $this->assertArrayNotHasKey('row_height', $data_config, 'Config row_height should not be set');
+ // Set the fixed_row_height back to 20px
+ $editor->config()->set('fixed_row_height', 20);
+ // Set the rows to 0
+ $editor->setRows(0);
+ // Get the attributes and config from the editor
+ $attributes = $editor->getAttributes();
+ $data_config = json_decode($attributes['data-config'], true);
+ // If rows is 0 then row_height and height config are not set
+ $this->assertArrayNotHasKey('height', $data_config, 'Config height should not be set');
+ $this->assertArrayNotHasKey('row_height', $data_config, 'Config row_height should not be set');
+ // Set the rows to 5
+ $editor->setRows(5);
+ // Get the attributes and config from the editor
+ $attributes = $editor->getAttributes();
+ $data_config = json_decode($attributes['data-config']);
+ // Check the height is set to auto and the row height is set to 100px (5 rows * 20px)
+ $this->assertEquals("auto", $data_config->height, 'Config height is not set');
+ $this->assertEquals("100px", $data_config->row_height, 'Config row_height is not set');
+ // Change the row height to 60px and set the rows to 3
+ $editor->setRows(3);
+ // Get the attributes and config from the editor
+ $attributes = $editor->getSchemaStateDefaults();
+ $data_config = json_decode($attributes['data']['attributes']['data-config']);
+ // Check the height is set to auto and the row height is set to 60px (3 rows * 20px)
+ $this->assertEquals("auto", $data_config->height, 'Config height is not set');
+ $this->assertEquals("60px", $data_config->row_height, 'Config row_height is not set');
+ }
}