From 49982b51edc501f4ad66a2ea62e855cb5df955db Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Fri, 24 Oct 2014 17:45:11 +1100 Subject: [PATCH] Clean up code, add listExtendColumns override --- modules/backend/behaviors/ListController.php | 84 ++++++++++++-------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/modules/backend/behaviors/ListController.php b/modules/backend/behaviors/ListController.php index 67ac8d2e5..1cba33666 100644 --- a/modules/backend/behaviors/ListController.php +++ b/modules/backend/behaviors/ListController.php @@ -117,42 +117,38 @@ class ListController extends ControllerBehavior $columnConfig = $this->makeConfig($listConfig->list); $columnConfig->model = $model; $columnConfig->alias = $definition; - if (isset($listConfig->recordUrl)) { - $columnConfig->recordUrl = $listConfig->recordUrl; - } - if (isset($listConfig->recordOnClick)) { - $columnConfig->recordOnClick = $listConfig->recordOnClick; - } - if (isset($listConfig->recordsPerPage)) { - $columnConfig->recordsPerPage = $listConfig->recordsPerPage; - } - if (isset($listConfig->noRecordsMessage)) { - $columnConfig->noRecordsMessage = $listConfig->noRecordsMessage; - } - if (isset($listConfig->defaultSort)) { - $columnConfig->defaultSort = $listConfig->defaultSort; - } - if (isset($listConfig->showSorting)) { - $columnConfig->showSorting = $listConfig->showSorting; - } - if (isset($listConfig->showSetup)) { - $columnConfig->showSetup = $listConfig->showSetup; - } - if (isset($listConfig->showCheckboxes)) { - $columnConfig->showCheckboxes = $listConfig->showCheckboxes; - } - if (isset($listConfig->showTree)) { - $columnConfig->showTree = $listConfig->showTree; - } - if (isset($listConfig->treeExpanded)) { - $columnConfig->treeExpanded = $listConfig->treeExpanded; - } - $widget = $this->makeWidget('Backend\Widgets\Lists', $columnConfig); - $widget->bindToController(); /* - * Extensibility helpers + * Prepare the columns configuration */ + $configFieldsToTransfer = [ + 'recordUrl', + 'recordOnClick', + 'recordsPerPage', + 'noRecordsMessage', + 'defaultSort', + 'showSorting', + 'showSetup', + 'showCheckboxes', + 'showTree', + 'treeExpanded', + ]; + + foreach ($configFieldsToTransfer as $field) { + if (isset($listConfig->{$field})) { + $columnConfig->{$field} = $listConfig->{$field}; + } + } + + /* + * List Widget with extensibility + */ + $widget = $this->makeWidget('Backend\Widgets\Lists', $columnConfig); + + $widget->bindEvent('list.extendColumns', function () use ($widget) { + $this->controller->listExtendColumns($widget); + }); + $widget->bindEvent('list.extendQueryBefore', function ($query) use ($definition) { $this->controller->listExtendQueryBefore($query, $definition); }); @@ -173,6 +169,8 @@ class ListController extends ControllerBehavior return $this->controller->listOverrideHeaderValue($column->columnName, $definition); }); + $widget->bindToController(); + /* * Prepare the toolbar widget (optional) */ @@ -306,6 +304,24 @@ class ListController extends ControllerBehavior // Overrides // + /** + * Called before the list columns are defined. + * @param Backend\Widgets\List $host The hosting list widget + * @return void + */ + // public function listExtendColumnsBefore($host) + // { + // } + + /** + * Called after the list columns are defined. + * @param Backend\Widgets\List $host The hosting list widget + * @return void + */ + public function listExtendColumns($host) + { + } + /** * Controller override: Extend supplied model * @param Model $model @@ -366,7 +382,7 @@ class ListController extends ControllerBehavior } /** - * Static helper for extending form fields. + * Static helper for extending list columns. * @param callable $callback * @return void */