@@ -10,7 +10,51 @@ trait Buttons
1010 // BUTTONS
1111 // ------------
1212
13- // TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']);
13+ /**
14+ * Order the CRUD buttons. If certain button names are missing from the given order array
15+ * they will be pushed to the end of the button collection.
16+ *
17+ *
18+ * @param string $stack Stack where the buttons belongs. Options: top, line, bottom.
19+ * @param array $order Ordered name of the buttons. ['update', 'delete', 'show']
20+ */
21+ public function orderButtons (string $ stack , array $ order )
22+ {
23+ $ newButtons = collect ([]);
24+ $ otherButtons = collect ([]);
25+
26+ // we get the buttons that belong to the specified stack
27+ $ stackButtons = $ this ->buttons ()->reject (function ($ item ) use ($ stack , $ otherButtons ) {
28+ if ($ item ->stack != $ stack ) {
29+ // if the button does not belong to this stack we just add it for merging later
30+ $ otherButtons ->push ($ item );
31+
32+ return true ;
33+ }
34+
35+ return false ;
36+ });
37+
38+ // we parse the ordered buttons
39+ collect ($ order )->each (function ($ btnKey ) use ($ newButtons , $ stackButtons ) {
40+ if (! $ button = $ stackButtons ->where ('name ' , $ btnKey )->first ()) {
41+ abort (500 , 'Button name [« ' .$ btnKey .'»] not found. ' );
42+ }
43+ $ newButtons ->push ($ button );
44+ });
45+
46+ // if the ordered buttons are less than the total number of buttons in the stack
47+ // we add the remaining buttons to the end of the ordered ones
48+ if (count ($ newButtons ) < count ($ stackButtons )) {
49+ foreach ($ stackButtons as $ button ) {
50+ if (! $ newButtons ->where ('name ' , $ button ->name )->first ()) {
51+ $ newButtons ->push ($ button );
52+ }
53+ }
54+ }
55+
56+ $ this ->setOperationSetting ('buttons ' , $ newButtons ->merge ($ otherButtons ));
57+ }
1458
1559 /**
1660 * Add a button to the CRUD table view.
0 commit comments