API Update serialisation of JSON tree data

Update TreeDropdownField schema
This commit is contained in:
Damian Mooyman 2017-04-11 18:06:23 +12:00
parent 05ac2c6fc9
commit 0791b387b8
3 changed files with 64 additions and 17 deletions

View File

@ -55,6 +55,10 @@ use InvalidArgumentException;
*/
class TreeDropdownField extends FormField
{
protected $schemaDataType = self::SCHEMA_DATA_TYPE_SINGLESELECT;
protected $schemaComponent = 'TreeDropdownField';
private static $url_handlers = array(
'$Action!/$ID' => '$Action'
);
@ -317,20 +321,13 @@ class TreeDropdownField extends FormField
*/
public function Field($properties = array())
{
$item = DataObject::singleton($this->sourceObject);
$emptyTitle = _t(
'DropdownField.CHOOSE_MODEL',
'(Choose {name})',
['name' => $item->i18n_singular_name()]
);
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
if ($record instanceof ViewableData) {
$title = $record->obj($this->labelField)->forTemplate();
} elseif ($record) {
$title = Convert::raw2xml($record->{$this->labelField});
} else {
$title = $emptyTitle;
$title = $this->getEmptyTitle();
}
// TODO Implement for TreeMultiSelectField
@ -343,7 +340,7 @@ class TreeDropdownField extends FormField
$properties,
array(
'Title' => $title,
'EmptyTitle' => $emptyTitle,
'EmptyTitle' => $this->getEmptyTitle(),
'Metadata' => ($metadata) ? Convert::raw2json($metadata) : null,
)
);
@ -430,7 +427,8 @@ class TreeDropdownField extends FormField
return [
'name' => $this->getName(),
'id' => $child->obj($this->keyField),
'title' => $child->obj($this->labelField),
'title' => $child->getTitle(),
'treetitle' => $child->obj($this->labelField),
'disabled' => $this->nodeIsDisabled($child),
'isSubTree' => $isSubTree
];
@ -627,4 +625,44 @@ class TreeDropdownField extends FormField
$copy->setSourceObject($this->sourceObject);
return $copy;
}
public function getSchemaStateDefaults()
{
// Check label for field
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
$selectedlabel = null;
$data = parent::getSchemaStateDefaults();
$data['data']['emptyTitle'] = $this->getEmptyTitle();
if ($record) {
$data['data']['valueObject'] = [
'id' => $record->getField($this->keyField),
'title' => $record->getTitle(),
'treetitle' => $record->obj($this->labelField)->getSchemaValue(),
];
}
return $data;
}
public function getSchemaDataDefaults()
{
$data = parent::getSchemaDataDefaults();
$data['data']['urlTree'] = $this->Link('tree');
return $data;
}
/**
* @return string
*/
protected function getEmptyTitle()
{
$item = DataObject::singleton($this->sourceObject);
$emptyTitle = _t(
'DropdownField.CHOOSE_MODEL',
'(Choose {name})',
['name' => $item->i18n_singular_name()]
);
return $emptyTitle;
}
}

View File

@ -361,17 +361,26 @@ class MarkedSet
$output = $data;
// Serialise node
$output['node'] = $serialiseEval($data['node']);
$serialised = $serialiseEval($data['node']);
// Force serialisation of DBField instances
if (is_array($output['node'])) {
foreach ($output['node'] as $key => $value) {
if (is_array($serialised)) {
foreach ($serialised as $key => $value) {
if ($value instanceof DBField) {
$output['node'][$key] = $value->getSchemaValue();
$serialised[$key] = $value->getSchemaValue();
}
}
} elseif ($output['node'] instanceof DBField) {
$output['node'] = $output['node']->getSchemaValue();
// Merge with top level array
unset($output['node']);
$output = array_merge($output, $serialised);
} else {
if ($serialised instanceof DBField) {
$serialised = $serialised->getSchemaValue();
}
// Replace node with serialised value
$output['node'] = $serialised;
}
// Replace children with serialised elements

View File

@ -13,7 +13,7 @@
<li id="selector-{$name}-{$id}" data-id="{$id}"
class="class-{$node.ClassName} {$markingClasses} <% if $disabled %>disabled<% end_if %>"
>
<a rel="$node.ID">{$title}</a>
<a rel="$node.ID">{$treetitle}</a>
$SubTree
</li>
<% end_loop %>