Extend BasePickerField from InputWidget for better ActiveField integration

This commit is contained in:
Lucas Bartholemy 2017-01-11 18:24:12 +01:00
parent 80891f4d81
commit 27275678e0
3 changed files with 127 additions and 2 deletions

View File

@ -0,0 +1,37 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\widgets;
/**
* A HumHub enhanced version of [[\yii\bootstrap\ActiveField]].
*
* @since 1.2
* @author Luke
*/
class ActiveField extends \yii\bootstrap\ActiveField
{
/**
* @inheritdoc
*/
public function widget($class, $config = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
if (isset($config['options']) && isset(class_parents($class)['humhub\widgets\InputWidget'])) {
$this->adjustLabelFor($config['options']);
}
return parent::widget($class, $config);
}
}

View File

@ -34,7 +34,7 @@ use \yii\helpers\Url;
* @since 1.2
* @author buddha
*/
abstract class BasePickerField extends JsWidget
abstract class BasePickerField extends InputWidget
{
/**
@ -286,7 +286,6 @@ abstract class BasePickerField extends JsWidget
* Loads all items of the given $selection array.
* The $selection array contains all selected itemKeys.
*
*
* @param array $selection array of itemKeys
* @return type array of items of type $itemClass or empty array for an empty selection
*/

View File

@ -0,0 +1,89 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace humhub\widgets;
use humhub\widgets\JsWidget;
use yii\base\Model;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
/**
* InputWidget is the base class for widgets that collect user inputs.
*
* An input widget can be associated with a data model and an attribute,
* or a name and a value. If the former, the name and the value will
* be generated automatically.
*
* Classes extending from this widget can be used in an [[\yii\widgets\ActiveForm|ActiveForm]]
* using the [[\yii\widgets\ActiveField::widget()|widget()]] method, for example like this:
*
* ```php
* <?= $form->field($model, 'from_date')->widget('WidgetClassName', [
* // configure additional widget properties here
* ]) ?>
* ```
*
* For more details and usage information on InputWidget, see the [guide article on forms](guide:input-forms).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class InputWidget extends JsWidget
{
/**
* @var Model the data model that this widget is associated with.
*/
public $model;
/**
* @var string the model attribute that this widget is associated with.
*/
public $attribute;
/**
* @var string the input name. This must be set if [[model]] and [[attribute]] are not set.
*/
public $name;
/**
* @var string the input value.
*/
public $value;
/**
* @var array the HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
if ($this->name === null && !$this->hasModel()) {
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
}
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
parent::init();
}
/**
* @return bool whether this widget is associated with a data model.
*/
protected function hasModel()
{
return $this->model instanceof Model && $this->attribute !== null;
}
}