Esempio n. 1
0
 /**
  * Generates a dynamic form field.
  *
  * @param Model $model the data model.
  * @param string $attribute the attribute name or expression.
  * @param array $options the additional configurations for the field object.
  * @return DynamicActiveField the created DynamicActiveField object
  */
 public function field($model, $attribute, $options = [])
 {
     $config = ArrayHelper::getValue($this->config, 'fieldConfig', []);
     if ($config instanceof \Closure) {
         $config = call_user_func($config, $model, $attribute);
     }
     if (!isset($config['class'])) {
         $config['class'] = DynamicActiveField::className();
     }
     return \Yii::createObject(ArrayHelper::merge($config, $options, ['model' => $model, 'attribute' => $attribute, 'formConfig' => $this->config]));
 }
Esempio n. 2
0
 /**
  * @param ActiveForm|array $form
  * @param array $options
  * @return ActiveField
  * @throws \yii\base\InvalidConfigException
  */
 public function getField($form, $options = [])
 {
     $attribute = "[{$this->getName()}]value";
     $config = [];
     if ($form instanceof ActiveForm) {
         $config = $form->fieldConfig;
     } elseif (is_array($form) && isset($form['fieldConfig'])) {
         $config = $form['fieldConfig'];
     }
     if ($config instanceof \Closure) {
         $config = call_user_func($config, $this, $attribute);
     }
     if (!isset($config['class'])) {
         $config['class'] = is_array($form) ? DynamicActiveField::className() : $form->fieldClass;
     }
     $config['template'] = "{label}\n<div class=\"input-group\">\n{input}<span class=\"input-group-btn\"><button type=\"button\" class=\"btn btn-danger\"><span class=\"glyphicon glyphicon-remove\" aria-hidden=\"true\"></span></button>\n</span>\n</div>\n{hint}\n{error}";
     $fieldConfig = $this->getEAttribute()->getFieldConfig();
     $fieldOptions = ArrayHelper::getValue($fieldConfig, 'fieldOptions', []);
     $labelOptions = ArrayHelper::getValue($fieldConfig, 'labelOptions', []);
     if (is_array($form)) {
         $options['formConfig'] = $form;
     } else {
         $options['form'] = $form;
     }
     $field = Yii::createObject(ArrayHelper::merge($config, $fieldOptions, $options, ['model' => $this, 'attribute' => $attribute]));
     if ($labelOptions) {
         $field->label(null, $labelOptions);
     } else {
         $field->label($this->getPresentation());
     }
     $fieldType = ArrayHelper::getValue($fieldConfig, 'fieldType', 'textInput');
     $inputOptions = ArrayHelper::getValue($fieldConfig, 'inputOptions', []);
     if ($this->getEAttribute()->predefinedValues) {
         $fieldType = 'dropDownList';
         $inputOptions['items'] = ArrayHelper::map(Value::find()->where(['id' => $this->getEAttribute()->id])->asArray()->all(), 'id', 'value');
         $inputOptions['options']['prompt'] = '';
     }
     if (method_exists($field, $fieldType)) {
         $parameters = [];
         $class = new \ReflectionClass(get_class($field));
         $reflectionParameters = $class->getMethod($fieldType)->getParameters();
         foreach ($reflectionParameters as $parameter) {
             $name = $parameter->getName();
             $parameters[$name] = ArrayHelper::remove($inputOptions, $name, $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null);
         }
         //            if (array_key_exists('items', $parameters) && empty($parameters['items']) && AttributeTypes::isActiveRecordType($this->getType())) {
         //                /** @var ActiveRecord $class */
         //                $class = Yii::$app->core->getEntityClass($this->getType());
         //                $reflector = new \ReflectionClass($class);
         //                $typeName = Inflector::titleize($reflector->getShortName());
         //                $hasToString = $reflector->hasMethod('__toString');
         //                $items = ArrayHelper::map($class::find()->all(), function($row) {
         //                    /** @var ActiveRecord $row */
         //                    $pk = $row->getPrimaryKey(false);
         //                    return is_array($pk) ? json_encode($pk) : $pk;
         //                }, function ($row) use ($hasToString, $typeName) {
         //                    /** @var ActiveRecord $row */
         //                    if ($hasToString) {
         //                        return $row->__toString();
         //                    } else {
         //                        $pk = $row->getPrimaryKey(false);
         //                        $pk = is_array($pk) ? json_encode($pk) : $pk;
         //                        return $typeName . ' ' . $pk;
         //                    }
         //                });
         //                $parameters['items'] = $items ? $items : [];
         //            }
         //
         //            //TODO multiple, promp option for dropdown
         call_user_func_array(array($field, $fieldType), $parameters);
     }
     return $field;
 }