Enh #4191: Added SortOrder Form Input Field

This commit is contained in:
Lucas Bartholemy 2020-07-10 12:55:22 +02:00
parent 5ebaa01686
commit 8a12acd122
7 changed files with 78 additions and 12 deletions

View File

@ -13,6 +13,7 @@ use humhub\modules\content\widgets\richtext\RichTextField;
use humhub\modules\file\components\FileManager;
use humhub\modules\ui\form\widgets\DatePicker;
use humhub\modules\ui\form\widgets\MultiSelect;
use humhub\modules\ui\form\widgets\SortOrderField;
use Yii;
use yii\helpers\Html;
use yii\widgets\ActiveField;
@ -261,7 +262,7 @@ class HForm extends \yii\base\Component
}
break;
case 'multiselectdropdown':
$field = $this->form->field($model, $name)->widget(MultiSelect::class, [
$field = $this->form->field($model, $name)->widget(MultiSelect::class, [
'items' => $definition['items'],
'options' => $definition['options']
]);
@ -293,7 +294,7 @@ class HForm extends \yii\base\Component
$field = $this->form->field($model, $name)->checkboxList($definition['items'], $options);
break;
case 'textarea':
if(isset($definition['class'])) {
if (isset($definition['class'])) {
$options['class'] = $definition['class'];
}
@ -338,11 +339,14 @@ class HForm extends \yii\base\Component
$field = $this->form->field($model, $name)->widget(RichTextField::class, $options);
break;
case 'sortOrder':
$field = $this->form->field($model, $name)->widget(SortOrderField::class, $options);
break;
default:
return "Field Type " . $definition['type'] . " not supported by Compat HForm";
}
if(!empty($definition['hint']) && $field instanceof ActiveField) {
if (!empty($definition['hint']) && $field instanceof ActiveField) {
$field->hint(Html::encode($definition['hint'], false));
}

View File

@ -13,3 +13,4 @@ HumHub Change Log
- Chg #4158: Cleanup post table removed unused column
- Fix #4182: Native edge password reveal icons interferes with custom one
- Fix #4173: Notification overview HTML compliant issue
- Enh #4191: Added SortOrder Form Input Field

View File

@ -1,5 +1,6 @@
<?php
use humhub\modules\ui\form\widgets\SortOrderField;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;
@ -44,7 +45,7 @@ use humhub\modules\space\widgets\SpacePickerField;
<?= $form->field($group, 'show_at_registration')->checkbox(); ?>
<?php endif; ?>
<?= $form->field($group, 'show_at_directory')->checkbox(); ?>
<?= $form->field($group, 'sort_order'); ?>
<?= $form->field($group, 'sort_order')->widget(SortOrderField::class) ?>
<?= CHtml::submitButton(Yii::t('AdminModule.user', 'Save'), ['class' => 'btn btn-primary', 'data-ui-loader' => ""]); ?>
@ -55,4 +56,4 @@ use humhub\modules\space\widgets\SpacePickerField;
?>
<?php ActiveForm::end(); ?>
</div>
<?php $this->endContent(); ?>
<?php $this->endContent(); ?>

View File

@ -1,6 +1,7 @@
<?php
use humhub\modules\ui\form\widgets\ActiveForm;
use humhub\modules\ui\form\widgets\SortOrderField;
use humhub\modules\user\models\ProfileFieldCategory;
use humhub\widgets\Button;
use yii\helpers\Url;
@ -23,11 +24,8 @@ use humhub\libs\Html;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($category, 'title') ?>
<?= $form->field($category, 'description')->textarea(['rows' => 5]) ?>
<?= $form->field($category, 'sort_order') ?>
<?= $form->field($category, 'sort_order')->widget(SortOrderField::class) ?>
<?= $form->field($category, 'translation_category') ?>
<hr>
@ -36,7 +34,7 @@ use humhub\libs\Html;
<?php if (!$category->isNewRecord && !$category->is_system): ?>
<?= Button::danger(Yii::t('AdminModule.user', 'Delete'))
->link(Url::to(['delete-category', 'id' => $category->id]))->confirm()->right()?>
->link(Url::to(['delete-category', 'id' => $category->id]))->confirm()->right() ?>
<?php endif; ?>
<?php ActiveForm::end(); ?>

View File

@ -6,6 +6,7 @@
*
*/
use humhub\modules\ui\form\widgets\SortOrderField;
use humhub\widgets\ModalButton;
use humhub\widgets\ModalDialog;
use yii\bootstrap\ActiveForm;
@ -18,7 +19,7 @@ use yii\bootstrap\ActiveForm;
<?php $form = ActiveForm::begin() ?>
<div class="modal-body">
<?= $form->field($model, 'name')?>
<?= $form->field($model, 'sort_order')->textInput( ['type' => 'number'])?>
<?= $form->field($model, 'sort_order')->widget(SortOrderField::class) ?>
</div>
<div class="modal-footer">
<?= ModalButton::submitModal()?>

View File

@ -0,0 +1,61 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\ui\form\widgets;
use Yii;
use yii\bootstrap\InputWidget;
/**
* SortOrderField is a uniform form field for setting a numeric sort order for model classes.
*
* The label and hint text is set automatically and it is not necessary to implement a attributeLabel or attributeHint
* in the model class.
*
* Future implementations of this class could also output a slider (or similar) instead of a text input field.
*
* Example usage:
*
* ```php
* <?= $form->field($model, $attribute)->widget(SortOrderField::class, [
* // configure additional widget properties here
* ]) ?>
* ```
*
* @since 1.6
* @package humhub\modules\ui\form\widgets
*/
class SortOrderField extends InputWidget
{
/**
* @var int the default value
*/
public $defaultValue = 100;
/**
* @inheritDoc
*/
public function run()
{
$this->field->label(Yii::t('UiModule.form', 'Sort Order'));
$this->field->hint(Yii::t('UiModule.form', 'Value between 0 and 50000, usually in steps of 100.'));
$model = $this->model;
$attribute = $this->attribute;
if ($this->defaultValue !== null && !is_numeric($model->$attribute)) {
$model->$attribute = $this->defaultValue;
}
$this->options['type'] = 'number';
return $this->renderInputHtml('text');
}
}

View File

@ -188,7 +188,7 @@ class ProfileField extends ActiveRecord
'class' => 'form-control',
],
'sort_order' => [
'type' => 'text',
'type' => 'sortOrder',
'maxlength' => 32,
'class' => 'form-control',
],