diff --git a/src/Forms/GridField/GridFieldLazyLoader.php b/src/Forms/GridField/GridFieldLazyLoader.php index 8ca6ca1b5..98fe89c53 100755 --- a/src/Forms/GridField/GridFieldLazyLoader.php +++ b/src/Forms/GridField/GridFieldLazyLoader.php @@ -2,7 +2,10 @@ namespace SilverStripe\Forms\GridField; +use SilverStripe\Forms\FormField; +use SilverStripe\Forms\TabSet; use SilverStripe\ORM\ArrayList; +use SilverStripe\ORM\Limitable; use SilverStripe\ORM\SS_List; /** @@ -19,9 +22,17 @@ class GridFieldLazyLoader implements GridField_DataManipulator, GridField_HTMLPr */ public function getManipulatedData(GridField $gridField, SS_List $dataList) { - return $this->isLazy($gridField) ? - ArrayList::create([]) : - $dataList; + // If we are lazy loading, empty the list + if ($this->isLazy($gridField)) { + 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 * @return bool */ 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; + } + } }