@@ -96,6 +96,11 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
9696 '' => 'edit ' ,
9797 ];
9898
99+ /**
100+ * Used to cache the results of getGridFieldItemAdjacencies();
101+ */
102+ private array $ cachedGridFieldItemAdjacencies = [];
103+
99104 /**
100105 *
101106 * @param GridField $gridField
@@ -188,6 +193,7 @@ public function edit($request)
188193 */
189194 public function ItemEditForm ()
190195 {
196+ $ this ->resetAdjacenciesCache ();
191197 $ list = $ this ->gridField ->getList ();
192198
193199 if (empty ($ this ->record )) {
@@ -627,18 +633,30 @@ public function getEditLink($id)
627633 */
628634 private function getGridFieldItemAdjacencies (): array
629635 {
630- $ list = $ this ->getGridField ()->getManipulatedList ();
631- $ paginator = $ this ->getGridFieldPaginatorState ();
632- if (!$ paginator ) {
636+ $ paginatorState = $ this ->getGridFieldPaginatorState ();
637+ if (!$ paginatorState ) {
633638 return [];
634639 }
635- $ currentPage = $ paginator ->getData ('currentPage ' );
636- $ itemsPerPage = $ paginator ->getData ('itemsPerPage ' );
637-
640+ $ keyStr = '' ;
641+ $ data = $ this ->getGridField ()->getState ()->toArray ();
642+ array_walk_recursive ($ data , function ($ v , $ k ) use (&$ keyStr ) {
643+ if (is_scalar ($ k ) && is_scalar ($ v )) {
644+ $ keyStr .= $ k . $ v ;
645+ }
646+ });
647+ $ key = md5 ($ keyStr );
648+ if (array_key_exists ($ key , $ this ->cachedGridFieldItemAdjacencies )) {
649+ return $ this ->cachedGridFieldItemAdjacencies [$ key ];
650+ }
651+ $ currentPage = $ paginatorState ->getData ('currentPage ' );
652+ $ itemsPerPage = $ paginatorState ->getData ('itemsPerPage ' );
638653 $ limit = $ itemsPerPage + 2 ;
639- $ limitOffset = max (0 , $ itemsPerPage * ($ currentPage -1 ) -1 );
640-
641- return $ list ->limit ($ limit , $ limitOffset )->column ('ID ' );
654+ $ limitOffset = max (0 , $ itemsPerPage * ($ currentPage - 1 ) - 1 );
655+ $ this ->cachedGridFieldItemAdjacencies [$ key ] = $ this ->getGridField ()
656+ ->getManipulatedList ()
657+ ->limit ($ limit , $ limitOffset )
658+ ->column ('ID ' );
659+ return $ this ->cachedGridFieldItemAdjacencies [$ key ];
642660 }
643661
644662 /**
@@ -736,6 +754,7 @@ private function getNumPages(GridField $gridField): int
736754 */
737755 public function getPreviousRecordID ()
738756 {
757+ $ this ->resetAdjacenciesCache ();
739758 return $ this ->getAdjacentRecordID (-1 );
740759 }
741760
@@ -746,6 +765,7 @@ public function getPreviousRecordID()
746765 */
747766 public function getNextRecordID ()
748767 {
768+ $ this ->resetAdjacenciesCache ();
749769 return $ this ->getAdjacentRecordID (1 );
750770 }
751771
@@ -1003,4 +1023,12 @@ private function getResponseNegotiator(DBHTMLText $renderedForm): PjaxResponseNe
10031023 }
10041024 ], $ this ->getToplevelController ()->getResponse ());
10051025 }
1026+
1027+ /**
1028+ * Reset the cache used for getGridFieldItemAdjacencies()
1029+ */
1030+ private function resetAdjacenciesCache ()
1031+ {
1032+ $ this ->cachedGridFieldItemAdjacencies = [];
1033+ }
10061034}
0 commit comments