mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Enhancement add support for TreeMultiselectField in react
This commit is contained in:
parent
884f53e0f2
commit
78d4d0d5dd
@ -387,7 +387,6 @@ class TreeDropdownField extends FormField
|
||||
$isSubTree = false;
|
||||
|
||||
$this->search = $request->requestVar('search');
|
||||
$flatlist = $request->requestVar('flatList');
|
||||
$id = (is_numeric($request->latestParam('ID')))
|
||||
? (int)$request->latestParam('ID')
|
||||
: (int)$request->requestVar('ID');
|
||||
@ -450,10 +449,11 @@ class TreeDropdownField extends FormField
|
||||
|
||||
// Set title formatter
|
||||
$customised = function (DataObject $child) use ($isSubTree) {
|
||||
$title = ($child->hasMethod('getMenuTitle')) ? $child->geMenuTitle() : $child->getTitle();
|
||||
return [
|
||||
'name' => $this->getName(),
|
||||
'id' => $child->obj($this->keyField),
|
||||
'title' => $child->getTitle(),
|
||||
'title' => $title,
|
||||
'treetitle' => $child->obj($this->labelField),
|
||||
'disabled' => $this->nodeIsDisabled($child),
|
||||
'isSubTree' => $isSubTree
|
||||
@ -466,7 +466,7 @@ class TreeDropdownField extends FormField
|
||||
$json = $markingSet
|
||||
->getChildrenAsArray($customised);
|
||||
|
||||
if ($flatlist) {
|
||||
if ($request->requestVar('flatList')) {
|
||||
// format and filter $json here
|
||||
$json['children'] = $this->flattenChildrenArray($json['children']);
|
||||
}
|
||||
@ -590,7 +590,7 @@ class TreeDropdownField extends FormField
|
||||
$contextString = implode('/', $parentTitles);
|
||||
|
||||
$child['contextString'] = ($contextString !== '') ? $contextString .'/' : '';
|
||||
$child['children'] = [];
|
||||
unset($child['children']);
|
||||
|
||||
if (!$this->search || in_array($child['id'], $this->realSearchIds)) {
|
||||
$output[] = $child;
|
||||
@ -719,16 +719,15 @@ class TreeDropdownField extends FormField
|
||||
public function getSchemaStateDefaults()
|
||||
{
|
||||
$data = parent::getSchemaStateDefaults();
|
||||
// Check label for field
|
||||
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
||||
$selectedlabel = null;
|
||||
|
||||
// Ensure cache is keyed by last modified date of the underlying list
|
||||
$data['data']['cacheKey'] = DataList::create($this->sourceObject)->max('LastEdited');
|
||||
if ($record) {
|
||||
$title = ($record->hasMethod('getMenuTitle')) ? $record->geMenuTitle() : $record->getTitle();
|
||||
$data['data']['valueObject'] = [
|
||||
'id' => $record->getField($this->keyField),
|
||||
'title' => $record->getTitle(),
|
||||
'title' => $title,
|
||||
'treetitle' => $record->obj($this->labelField)->getSchemaValue(),
|
||||
];
|
||||
}
|
||||
@ -744,6 +743,7 @@ class TreeDropdownField extends FormField
|
||||
'showSearch' => $this->showSearch,
|
||||
'emptyString' => $this->getEmptyString(),
|
||||
'hasEmptyDefault' => $this->getHasEmptyDefault(),
|
||||
'multiple' => false,
|
||||
]);
|
||||
|
||||
return $data;
|
||||
|
@ -5,9 +5,11 @@ namespace SilverStripe\Forms;
|
||||
use SilverStripe\Core\Convert;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\ORM\DataList;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\DataObjectInterface;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\Security\Group;
|
||||
use SilverStripe\View\ViewableData;
|
||||
use stdClass;
|
||||
|
||||
@ -54,34 +56,82 @@ use stdClass;
|
||||
*/
|
||||
class TreeMultiselectField extends TreeDropdownField
|
||||
{
|
||||
public function __construct($name, $title = null, $sourceObject = "SilverStripe\\Security\\Group", $keyField = "ID", $labelField = "Title")
|
||||
{
|
||||
public function __construct(
|
||||
$name,
|
||||
$title = null,
|
||||
$sourceObject = Group::class,
|
||||
$keyField = "ID",
|
||||
$labelField = "Title"
|
||||
) {
|
||||
parent::__construct($name, $title, $sourceObject, $keyField, $labelField);
|
||||
$this->removeExtraClass('single');
|
||||
$this->addExtraClass('multiple');
|
||||
$this->value = 'unchanged';
|
||||
}
|
||||
|
||||
public function getSchemaDataDefaults()
|
||||
{
|
||||
$data = parent::getSchemaDataDefaults();
|
||||
|
||||
$data['data'] = array_merge($data['data'], [
|
||||
'hasEmptyDefault' => false,
|
||||
'multiple' => true,
|
||||
]);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getSchemaStateDefaults()
|
||||
{
|
||||
$data = parent::getSchemaStateDefaults();
|
||||
unset($data['data']['valueObject']);
|
||||
|
||||
$items = $this->getItems();
|
||||
$values = [];
|
||||
foreach ($items as $item) {
|
||||
if ($item instanceof DataObject) {
|
||||
$values[] = [
|
||||
'id' => $item->getField($this->keyField),
|
||||
'title' => $item->getTitle(),
|
||||
'parentid' => $item->ParentID,
|
||||
'treetitle' => $item->obj($this->labelField)->getSchemaValue(),
|
||||
];
|
||||
} else {
|
||||
$values[] = $item;
|
||||
}
|
||||
}
|
||||
$data['data']['valueObjects'] = $values;
|
||||
|
||||
// cannot rely on $this->value as this could be a many-many relationship
|
||||
$value = array_column($values, 'id');
|
||||
$data['value'] = ($value) ? $value : 'unchanged';
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this field's linked items
|
||||
* @return ArrayList|DataList $items
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
$items = new ArrayList();
|
||||
|
||||
// If the value has been set, use that
|
||||
if ($this->value != 'unchanged' && is_array($this->sourceObject)) {
|
||||
$items = array();
|
||||
if ($this->value != 'unchanged') {
|
||||
if (is_array($this->sourceObject)) {
|
||||
$values = is_array($this->value) ? $this->value : preg_split('/ *, */', trim($this->value));
|
||||
|
||||
foreach ($values as $value) {
|
||||
$item = new stdClass;
|
||||
$item->ID = $value;
|
||||
$item->Title = $this->sourceObject[$value];
|
||||
$items[] = $item;
|
||||
$items->push($item);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
// Otherwise, look data up from the linked relation
|
||||
} if ($this->value != 'unchanged' && is_string($this->value)) {
|
||||
$items = new ArrayList();
|
||||
if (is_string($this->value)) {
|
||||
$ids = explode(',', $this->value);
|
||||
foreach ($ids as $id) {
|
||||
if (!is_numeric($id)) {
|
||||
@ -93,13 +143,18 @@ class TreeMultiselectField extends TreeDropdownField
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
} elseif ($this->form) {
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->form) {
|
||||
$fieldName = $this->name;
|
||||
$record = $this->form->getRecord();
|
||||
if (is_object($record) && $record->hasMethod($fieldName)) {
|
||||
return $record->$fieldName();
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,7 +216,7 @@ class TreeMultiselectField extends TreeDropdownField
|
||||
{
|
||||
// Detect whether this field has actually been updated
|
||||
if ($this->value !== 'unchanged') {
|
||||
$items = array();
|
||||
$items = [];
|
||||
|
||||
$fieldName = $this->name;
|
||||
$saveDest = $record->$fieldName();
|
||||
@ -174,7 +229,9 @@ class TreeMultiselectField extends TreeDropdownField
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->value) {
|
||||
if (is_array($this->value)) {
|
||||
$items = $this->value;
|
||||
} elseif ($this->value) {
|
||||
$items = preg_split("/ *, */", trim($this->value));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user