Skip to content

Commit

Permalink
FIX Ensure schema data includes attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 15, 2024
1 parent eb8907f commit 23cbfcb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,14 @@ public function getSchemaDataDefaults()
if ($titleTip instanceof Tip) {
$data['titleTip'] = $titleTip->getTipSchema();
}
$attributes = $this->getAttributes();
// Remove value from attributes because otherwise it breaks react fields
unset($attributes['value']);
// Remove above attributes from attributes list to avoid double-ups
foreach (array_keys($data) as $key) {
unset($attributes[$key]);
}
$data['attributes'] = $attributes;
return $data;
}

Expand Down
1 change: 0 additions & 1 deletion src/Forms/SearchableDropdownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ public function getAttributes(): array
parent::getAttributes(),
[
'name' => $name,
'data-schema' => json_encode($this->getSchemaData()),
]
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Forms/TreeDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,6 @@ public function getAttributes()
$attributes = [
'class' => $this->extraClass(),
'id' => $this->ID(),
'data-schema' => json_encode($this->getSchemaData()),
'data-state' => json_encode($this->getSchemaState()),
];

$attributes = array_merge($attributes, $this->attributes);
Expand Down
2 changes: 1 addition & 1 deletion templates/SilverStripe/Forms/TreeDropdownField.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div
class="TreeDropdownField<% if $extraClass %> $extraClass<% end_if %><% if $ShowSearch %> searchable<% end_if %>"
$AttributesHTML('class')
$AttributesHTML('class') data-schema="$SchemaData.JSON" data-state="$SchemaState.JSON"
<% if $Metadata %>data-metadata="$Metadata.ATT"<% end_if %>
>
<input id="$ID" type="hidden" name="$Name.ATT" value="$Value.ATT" />
Expand Down
10 changes: 10 additions & 0 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ public function testGetSchemaDataDefaultsTitleTip()
$this->assertSame('Test tip', $schema['titleTip']['content']);
}

public function testGetSchemaDataDefaultsAttributes()
{
$field = new FormField('MyField');
$field->setAttribute('foo', 'bar');
$this->assertEquals('bar', $field->getAttribute('foo'));
$schema = $field->getSchemaDataDefaults();
$this->assertArrayHasKey('foo', $schema['attributes']);
$this->assertEquals('bar', $schema['attributes']['foo']);
}

public function testGetSchemaData()
{
$field = new FormField('MyField');
Expand Down

0 comments on commit 23cbfcb

Please sign in to comment.