Merge pull request #2871 from priitperna/master

Added scope support to the Relation formwidget
This commit is contained in:
Samuel Georges 2017-05-27 22:19:20 +10:00 committed by GitHub
commit 6891d21bdf
2 changed files with 15 additions and 2 deletions

View File

@ -43,6 +43,11 @@ class Relation extends FormWidgetBase
*/
public $emptyOption;
/**
* @var string Use a custom scope method for the list query.
*/
public $scope;
//
// Object properties
//
@ -66,6 +71,7 @@ class Relation extends FormWidgetBase
'nameFrom',
'descriptionFrom',
'emptyOption',
'scope',
]);
if (isset($this->config->select)) {
@ -118,6 +124,10 @@ class Relation extends FormWidgetBase
$query->where($relationModel->getKeyName(), '<>', $model->getKey());
}
if ($scopeMethod = $this->scope) {
$query->$scopeMethod($model);
}
// Even though "no constraints" is applied, belongsToMany constrains the query
// by joining its pivot table. Remove all joins from the query.
$query->getQuery()->getQuery()->joins = [];
@ -139,6 +149,7 @@ class Relation extends FormWidgetBase
$result = $query->getQuery()->get();
}
// Some simpler relations can specify a custom local or foreign "other" key,
// which can be detected and implemented here automagically.
$primaryKeyName = in_array($relationType, ['hasMany', 'belongsTo', 'hasOne'])

View File

@ -3,6 +3,7 @@
$checkedValues = (array) $field->value;
$isScrollable = count($fieldOptions) > 10;
$readOnly = $this->previewMode || $field->readOnly || $field->disabled;
$quickselectEnabled = $field->getConfig('quickselect');
?>
<!-- Checkbox List -->
<?php if ($readOnly && $field->value): ?>
@ -37,14 +38,15 @@
<?php elseif (!$readOnly && count($fieldOptions)): ?>
<div class="field-checkboxlist <?= $isScrollable ? 'is-scrollable' : '' ?>">
<?php if ($isScrollable): ?>
<?php if ($quickselectEnabled || $isScrollable): ?>
<!-- Quick selection -->
<small>
<?= e(trans('backend::lang.form.select')) ?>:
<a href="javascript:;" data-field-checkboxlist-all><?= e(trans('backend::lang.form.select_all')) ?></a>,
<a href="javascript:;" data-field-checkboxlist-none><?= e(trans('backend::lang.form.select_none')) ?></a>
</small>
<?php endif ?>
<?php if ($isScrollable): ?>
<!-- Scrollable Checkbox list -->
<div class="field-checkboxlist-scrollable">
<div class="control-scrollbar" data-control="scrollbar">