mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Default TreeDropdown to "Title" search if $labelField isn't in DB
This is a workaround in order to ensure the field stays operational for SiteTree and File records with the new $showSearch=true default. Previously it was necessary to use setSearchCallback(), otherwise the SQL query would fail. One limitation to keep this change generic is that "MenuTitle" won't be used to search, since its SiteTree specific, while the "Title" and "Name" fields are generally regarded as model conventions (e.g. they're used in DataObject->getTitle() as well). See https://github.com/silverstripe/silverstripe-framework/pull/2364
This commit is contained in:
parent
4ff7b43c44
commit
79cab42a91
@ -93,7 +93,7 @@ class TreeDropdownField extends FormField {
|
|||||||
$this->keyField = $keyField;
|
$this->keyField = $keyField;
|
||||||
$this->labelField = $labelField;
|
$this->labelField = $labelField;
|
||||||
$this->showSearch = $showSearch;
|
$this->showSearch = $showSearch;
|
||||||
|
|
||||||
parent::__construct($name, $title);
|
parent::__construct($name, $title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -389,10 +389,32 @@ class TreeDropdownField extends FormField {
|
|||||||
*/
|
*/
|
||||||
protected function populateIDs() {
|
protected function populateIDs() {
|
||||||
// get all the leaves to be displayed
|
// get all the leaves to be displayed
|
||||||
if ( $this->searchCallback )
|
if ($this->searchCallback) {
|
||||||
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
|
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
|
||||||
else
|
} else {
|
||||||
$res = DataObject::get($this->sourceObject, "\"$this->labelField\" LIKE '%$this->search%'");
|
$sourceObject = $this->sourceObject;
|
||||||
|
$wheres = array();
|
||||||
|
if(singleton($sourceObject)->hasDatabaseField($this->labelField)) {
|
||||||
|
$wheres[] = "\"$searchField\" LIKE '%$this->search%'";
|
||||||
|
} else {
|
||||||
|
if(singleton($sourceObject)->hasDatabaseField('Title')) {
|
||||||
|
$wheres[] = "\"Title\" LIKE '%$this->search%'";
|
||||||
|
}
|
||||||
|
if(singleton($sourceObject)->hasDatabaseField('Name')) {
|
||||||
|
$wheres[] = "\"Name\" LIKE '%$this->search%'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$wheres) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'Cannot query by %s.%s, not a valid database column',
|
||||||
|
$sourceObject,
|
||||||
|
$this->labelField
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = DataObject::get($this->sourceObject, implode(' OR ', $wheres));
|
||||||
|
}
|
||||||
|
|
||||||
if( $res ) {
|
if( $res ) {
|
||||||
// iteratively fetch the parents in bulk, until all the leaves can be accessed using the tree control
|
// iteratively fetch the parents in bulk, until all the leaves can be accessed using the tree control
|
||||||
|
Loading…
x
Reference in New Issue
Block a user