Don't lazy load tab that are not in a TabSet.

This commit is contained in:
Maxime Rainville 2018-10-05 16:57:37 +13:00
parent 5276b6cbb1
commit d2a51471e9

View File

@ -2,7 +2,10 @@
namespace SilverStripe\Forms\GridField; namespace SilverStripe\Forms\GridField;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Limitable;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
/** /**
@ -19,9 +22,17 @@ class GridFieldLazyLoader implements GridField_DataManipulator, GridField_HTMLPr
*/ */
public function getManipulatedData(GridField $gridField, SS_List $dataList) public function getManipulatedData(GridField $gridField, SS_List $dataList)
{ {
return $this->isLazy($gridField) ? // If we are lazy loading, empty the list
ArrayList::create([]) : if ($this->isLazy($gridField)) {
$dataList; if ($dataList instanceof Limitable) {
// If our original list can be limited, set the limit to 0.
$dataList = $dataList->limit(0);
} else {
// If not, create an empty list instead.
$dataList = ArrayList::create([]);
}
}
return $dataList;
} }
/** /**
@ -36,12 +47,32 @@ class GridFieldLazyLoader implements GridField_DataManipulator, GridField_HTMLPr
} }
/** /**
* Detect if the current request should include results * Detect if the current request should include results.
* @param GridField $gridField * @param GridField $gridField
* @return bool * @return bool
*/ */
private function isLazy(GridField $gridField) private function isLazy(GridField $gridField)
{ {
return $gridField->getRequest()->getHeader('X-Pjax') !== 'CurrentField'; return
$gridField->getRequest()->getHeader('X-Pjax') !== 'CurrentField' &&
$this->isInTabSet($gridField);
}
/**
* Recursively check if $field is inside a TabSet.
* @param FormField $field
* @return bool
*/
private function isInTabSet(FormField $field)
{
$list = $field->getContainerFieldList();
if ($list && $containerField = $list->getContainerField()) {
// Classes that extends TabSet might not have the expected JS to lazy load.
return get_class($containerField) === TabSet::class
?: $this->isInTabSet($containerField);
} else {
return false;
}
} }
} }