yii\helpers\BaseHtml::getAttributeValue PHP Méthode

getAttributeValue() public static méthode

For an attribute expression like [0]dates[0], this method will return the value of $model->dates[0]. See BaseHtml::getAttributeName for more details about attribute expression. If an attribute value is an instance of [[ActiveRecordInterface]] or an array of such instances, the primary value(s) of the AR instance(s) will be returned instead.
public static getAttributeValue ( Model $model, string $attribute ) : string | array
$model yii\base\Model the model object
$attribute string the attribute name or expression
Résultat string | array the corresponding attribute value
    public static function getAttributeValue($model, $attribute)
    {
        if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {
            throw new InvalidParamException('Attribute name must contain word characters only.');
        }
        $attribute = $matches[2];
        $value = $model->{$attribute};
        if ($matches[3] !== '') {
            foreach (explode('][', trim($matches[3], '[]')) as $id) {
                if ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$id])) {
                    $value = $value[$id];
                } else {
                    return null;
                }
            }
        }
        // https://github.com/yiisoft/yii2/issues/1457
        if (is_array($value)) {
            foreach ($value as $i => $v) {
                if ($v instanceof ActiveRecordInterface) {
                    $v = $v->getPrimaryKey(false);
                    $value[$i] = is_array($v) ? json_encode($v) : $v;
                }
            }
        } elseif ($value instanceof ActiveRecordInterface) {
            $value = $value->getPrimaryKey(false);
            return is_array($value) ? json_encode($value) : $value;
        }
        return $value;
    }

Usage Example

 public function run()
 {
     $options = $this->options;
     if (isset($options['id'])) {
         $id = $options['id'];
     } else {
         $id = 'editor';
     }
     if (isset($options['name'])) {
         $name = $options['name'];
     } elseif ($this->hasModel()) {
         $name = BaseHtml::getInputName($this->model, $this->attribute);
     } else {
         $name = $this->name;
     }
     if (isset($options['value'])) {
         $value = $options['value'];
     } elseif ($this->hasModel()) {
         $value = BaseHtml::getAttributeValue($this->model, $this->attribute);
     } else {
         $value = $this->value;
     }
     echo Html::beginTag('script', ['id' => $id, 'type' => 'text/plain', 'name' => $name, 'style' => "height:{$this->height}"]);
     echo $value;
     echo Html::endTag('script');
     if (!isset($options['config'])) {
         $options['config'] = [];
     }
     $ueditorConfig = ArrayHelper::merge(['serverUrl' => Url::to(['ueditor/controller'])], $options['config']);
     $config = Json::encode($ueditorConfig);
     $view = $this->getView();
     UEditorAsset::register($view);
     $view->registerJs("var ue = UE.getEditor('{$id}', {$config});");
 }
All Usage Examples Of yii\helpers\BaseHtml::getAttributeValue