Add support for filters in the RelationController configuration (#4241)
Credit to @alxy. Docs: https://github.com/octobercms/docs/pull/368
This commit is contained in:
parent
5055c830e9
commit
4e92686c1a
|
|
@ -60,11 +60,21 @@ class RelationController extends ControllerBehavior
|
|||
*/
|
||||
protected $viewWidget;
|
||||
|
||||
/**
|
||||
* @var \Backend\Widgets\Filter Reference to the view filter widget.
|
||||
*/
|
||||
protected $viewFilterWidget;
|
||||
|
||||
/**
|
||||
* @var Backend\Classes\WidgetBase Reference to the widget used for relation management.
|
||||
*/
|
||||
protected $manageWidget;
|
||||
|
||||
/**
|
||||
* @var \Backend\Widgets\Filter Reference to the manage filter widget.
|
||||
*/
|
||||
protected $manageFilterWidget;
|
||||
|
||||
/**
|
||||
* @var Backend\Classes\WidgetBase Reference to widget for relations with pivot data.
|
||||
*/
|
||||
|
|
@ -254,6 +264,8 @@ class RelationController extends ControllerBehavior
|
|||
$this->vars['relationField'] = $this->field;
|
||||
$this->vars['relationType'] = $this->relationType;
|
||||
$this->vars['relationSearchWidget'] = $this->searchWidget;
|
||||
$this->vars['relationManageFilterWidget'] = $this->manageFilterWidget;
|
||||
$this->vars['relationViewFilterWidget'] = $this->viewFilterWidget;
|
||||
$this->vars['relationToolbarWidget'] = $this->toolbarWidget;
|
||||
$this->vars['relationManageMode'] = $this->manageMode;
|
||||
$this->vars['relationManageWidget'] = $this->manageWidget;
|
||||
|
|
@ -364,6 +376,19 @@ class RelationController extends ControllerBehavior
|
|||
$this->searchWidget->bindToController();
|
||||
}
|
||||
|
||||
/*
|
||||
* Filter widgets (optional)
|
||||
*/
|
||||
if ($this->manageFilterWidget = $this->makeFilterWidget('manage')) {
|
||||
$this->controller->relationExtendManageFilterWidget($this->manageFilterWidget, $this->field, $this->model);
|
||||
$this->manageFilterWidget->bindToController();
|
||||
}
|
||||
|
||||
if ($this->viewFilterWidget = $this->makeFilterWidget('view')) {
|
||||
$this->controller->relationExtendViewFilterWidget($this->viewFilterWidget, $this->field, $this->model);
|
||||
$this->viewFilterWidget->bindToController();
|
||||
}
|
||||
|
||||
/*
|
||||
* View widget
|
||||
*/
|
||||
|
|
@ -538,6 +563,26 @@ class RelationController extends ControllerBehavior
|
|||
// Widgets
|
||||
//
|
||||
|
||||
/**
|
||||
* Initialize a filter widget
|
||||
*
|
||||
* @param $type string Either 'manage' or 'view'
|
||||
* @return \Backend\Classes\WidgetBase|null
|
||||
*/
|
||||
protected function makeFilterWidget($type)
|
||||
{
|
||||
if (!$this->getConfig($type . '[filter]')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$filterConfig = $this->makeConfig($this->getConfig($type . '[filter]'));
|
||||
$filterConfig->alias = $this->alias . ucfirst($type) . 'Filter';
|
||||
$filterWidget = $this->makeWidget('Backend\Widgets\Filter', $filterConfig);
|
||||
|
||||
return $filterWidget;
|
||||
}
|
||||
|
||||
|
||||
protected function makeToolbarWidget()
|
||||
{
|
||||
$defaultConfig = [];
|
||||
|
|
@ -715,6 +760,18 @@ class RelationController extends ControllerBehavior
|
|||
$searchWidget->setActiveTerm(null);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Link the Filter Widget to the List Widget
|
||||
*/
|
||||
if ($this->viewFilterWidget) {
|
||||
$this->viewFilterWidget->bindEvent('filter.update', function () use ($widget) {
|
||||
return $widget->onFilter();
|
||||
});
|
||||
|
||||
// Apply predefined filter values
|
||||
$widget->addFilter([$this->viewFilterWidget, 'applyAllScopesToQuery']);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Single (belongs to, has one)
|
||||
|
|
@ -813,6 +870,18 @@ class RelationController extends ControllerBehavior
|
|||
$widget->setSearchTerm($this->searchWidget->getActiveTerm());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Link the Filter Widget to the List Widget
|
||||
*/
|
||||
if ($this->manageFilterWidget) {
|
||||
$this->manageFilterWidget->bindEvent('filter.update', function () use ($widget) {
|
||||
return $widget->onFilter();
|
||||
});
|
||||
|
||||
// Apply predefined filter values
|
||||
$widget->addFilter([$this->manageFilterWidget, 'applyAllScopesToQuery']);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Form
|
||||
|
|
@ -1093,7 +1162,7 @@ class RelationController extends ControllerBehavior
|
|||
*/
|
||||
if ($this->viewMode == 'multi') {
|
||||
if (($checkedIds = post('checked')) && is_array($checkedIds)) {
|
||||
foreach ($checkedIds as $relationId) {
|
||||
foreach ($checkedIds as $relationId) {
|
||||
if (!$obj = $this->relationModel->find($relationId)) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -1299,7 +1368,7 @@ class RelationController extends ControllerBehavior
|
|||
* Provides an opportunity to manipulate the field configuration.
|
||||
* @param object $config
|
||||
* @param string $field
|
||||
* @param October\Rain\Database\Model $model
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendConfig($config, $field, $model)
|
||||
{
|
||||
|
|
@ -1309,7 +1378,7 @@ class RelationController extends ControllerBehavior
|
|||
* Provides an opportunity to manipulate the view widget.
|
||||
* @param Backend\Classes\WidgetBase $widget
|
||||
* @param string $field
|
||||
* @param October\Rain\Database\Model $model
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendViewWidget($widget, $field, $model)
|
||||
{
|
||||
|
|
@ -1319,7 +1388,7 @@ class RelationController extends ControllerBehavior
|
|||
* Provides an opportunity to manipulate the manage widget.
|
||||
* @param Backend\Classes\WidgetBase $widget
|
||||
* @param string $field
|
||||
* @param October\Rain\Database\Model $model
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendManageWidget($widget, $field, $model)
|
||||
{
|
||||
|
|
@ -1329,12 +1398,32 @@ class RelationController extends ControllerBehavior
|
|||
* Provides an opportunity to manipulate the pivot widget.
|
||||
* @param Backend\Classes\WidgetBase $widget
|
||||
* @param string $field
|
||||
* @param October\Rain\Database\Model $model
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendPivotWidget($widget, $field, $model)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides an opportunity to manipulate the manage filter widget.
|
||||
* @param \Backend\Widgets\Filter $widget
|
||||
* @param string $field
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendManageFilterWidget($widget, $field, $model)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides an opportunity to manipulate the view filter widget.
|
||||
* @param \Backend\Widgets\Filter $widget
|
||||
* @param string $field
|
||||
* @param \October\Rain\Database\Model $model
|
||||
*/
|
||||
public function relationExtendViewFilterWidget($widget, $field, $model)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The view widget is often refreshed when the manage widget makes a change,
|
||||
* you can use this method to inject additional containers when this process
|
||||
|
|
@ -1626,4 +1715,4 @@ class RelationController extends ControllerBehavior
|
|||
public function relationGetViewWidget() {
|
||||
return $this->viewWidget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,9 @@
|
|||
<?php if ($relationSearchWidget): ?>
|
||||
<?= $relationSearchWidget->render() ?>
|
||||
<?php endif ?>
|
||||
<?php if ($relationManageFilterWidget): ?>
|
||||
<?= $relationManageFilterWidget->render() ?>
|
||||
<?php endif ?>
|
||||
<?= $relationManageWidget->render() ?>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,5 @@
|
|||
<?php if ($relationViewFilterWidget): ?>
|
||||
<?= $relationViewFilterWidget->render() ?>
|
||||
<?php endif ?>
|
||||
|
||||
<?= $relationViewWidget->render() ?>
|
||||
|
|
@ -639,6 +639,7 @@ class Filter extends WidgetBase
|
|||
|
||||
$scope = new FilterScope($name, $label);
|
||||
$scope->displayAs($scopeType, $config);
|
||||
$scope->idPrefix = $this->alias;
|
||||
|
||||
/*
|
||||
* Set scope value
|
||||
|
|
|
|||
Loading…
Reference in New Issue