1 | <?php |
||
36 | class DataColumn extends Column |
||
37 | { |
||
38 | /** |
||
39 | * @var string the attribute name associated with this column. When neither [[content]] nor [[value]] |
||
40 | * is specified, the value of the specified attribute will be retrieved from each data model and displayed. |
||
41 | * |
||
42 | * Also, if [[label]] is not specified, the label associated with the attribute will be displayed. |
||
43 | */ |
||
44 | public $attribute; |
||
45 | /** |
||
46 | * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting |
||
47 | * link label when sorting is enabled for this column. |
||
48 | * If it is not set and the models provided by the GridViews data provider are instances |
||
49 | * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]]. |
||
50 | * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label. |
||
51 | */ |
||
52 | public $label; |
||
53 | /** |
||
54 | * @var boolean whether the header label should be HTML-encoded. |
||
55 | * @see label |
||
56 | * @since 2.0.1 |
||
57 | */ |
||
58 | public $encodeLabel = true; |
||
59 | /** |
||
60 | * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column. |
||
61 | * |
||
62 | * If this is an anonymous function, it will be called for each row and the return value will be used as the value to |
||
63 | * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`. |
||
64 | * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
||
65 | * and `$column` is a reference to the [[DataColumn]] object. |
||
66 | * |
||
67 | * You may also set this property to a string representing the attribute name to be displayed in this column. |
||
68 | * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for |
||
69 | * sorting and filtering. |
||
70 | * |
||
71 | * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]]. |
||
72 | */ |
||
73 | public $value; |
||
74 | /** |
||
75 | * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, |
||
76 | * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by |
||
77 | * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when |
||
78 | * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView. |
||
79 | */ |
||
80 | public $format = 'text'; |
||
81 | /** |
||
82 | * @var boolean whether to allow sorting by this column. If true and [[attribute]] is found in |
||
83 | * the sort definition of [[GridView::dataProvider]], then the header cell of this column |
||
84 | * will contain a link that may trigger the sorting when being clicked. |
||
85 | */ |
||
86 | public $enableSorting = true; |
||
87 | /** |
||
88 | * @var array the HTML attributes for the link tag in the header cell |
||
89 | * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column. |
||
90 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
91 | */ |
||
92 | public $sortLinkOptions = []; |
||
93 | /** |
||
94 | * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list) |
||
95 | * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set. |
||
96 | * |
||
97 | * - If this property is not set, a text field will be generated as the filter input; |
||
98 | * - If this property is an array, a dropdown list will be generated that uses this property value as |
||
99 | * the list options. |
||
100 | * - If you don't want a filter for this data column, set this value to be false. |
||
101 | */ |
||
102 | public $filter; |
||
103 | /** |
||
104 | * @var array the HTML attributes for the filter input fields. This property is used in combination with |
||
105 | * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to |
||
106 | * render the HTML attributes for the generated filter input fields. |
||
107 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
108 | */ |
||
109 | public $filterInputOptions = ['class' => 'form-control', 'id' => null]; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | protected function renderHeaderCellContent() |
||
116 | { |
||
117 | if ($this->header !== null || $this->label === null && $this->attribute === null) { |
||
118 | return parent::renderHeaderCellContent(); |
||
119 | } |
||
120 | |||
121 | $label = $this->getHeaderCellLabel(); |
||
122 | if ($this->encodeLabel) { |
||
123 | $label = Html::encode($label); |
||
124 | } |
||
125 | |||
126 | if ($this->attribute !== null && $this->enableSorting && |
||
127 | ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { |
||
128 | return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label])); |
||
129 | } else { |
||
130 | return $label; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | * @since 2.0.8 |
||
137 | */ |
||
138 | 2 | protected function getHeaderCellLabel() |
|
139 | { |
||
140 | 2 | $provider = $this->grid->dataProvider; |
|
141 | |||
142 | 2 | if ($this->label === null) { |
|
143 | 2 | if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) { |
|
144 | /* @var $model Model */ |
||
145 | $model = new $provider->query->modelClass; |
||
|
|||
146 | $label = $model->getAttributeLabel($this->attribute); |
||
147 | 2 | } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) { |
|
148 | /* @var $model Model */ |
||
149 | 1 | $model = new $provider->modelClass; |
|
150 | 1 | $label = $model->getAttributeLabel($this->attribute); |
|
151 | 2 | } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) { |
|
152 | 1 | $label = $this->grid->filterModel->getAttributeLabel($this->attribute); |
|
153 | 1 | } else { |
|
154 | $models = $provider->getModels(); |
||
155 | if (($model = reset($models)) instanceof Model) { |
||
156 | /* @var $model Model */ |
||
157 | $label = $model->getAttributeLabel($this->attribute); |
||
158 | } else { |
||
159 | $label = Inflector::camel2words($this->attribute); |
||
160 | } |
||
161 | } |
||
162 | 2 | } else { |
|
163 | $label = $this->label; |
||
164 | } |
||
165 | |||
166 | 2 | return $label; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | 2 | protected function renderFilterCellContent() |
|
173 | { |
||
174 | 2 | if (is_string($this->filter)) { |
|
175 | 1 | return $this->filter; |
|
176 | } |
||
177 | |||
178 | 1 | $model = $this->grid->filterModel; |
|
179 | |||
180 | 1 | if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) { |
|
181 | 1 | if ($model->hasErrors($this->attribute)) { |
|
182 | Html::addCssClass($this->filterOptions, 'has-error'); |
||
183 | $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions); |
||
184 | } else { |
||
185 | 1 | $error = ''; |
|
186 | } |
||
187 | 1 | if (is_array($this->filter)) { |
|
188 | 1 | $options = array_merge(['prompt' => ''], $this->filterInputOptions); |
|
189 | 1 | return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error; |
|
190 | } else { |
||
191 | return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error; |
||
192 | } |
||
193 | } else { |
||
194 | return parent::renderFilterCellContent(); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Returns the data cell value. |
||
200 | * @param mixed $model the data model |
||
201 | * @param mixed $key the key associated with the data model |
||
202 | * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. |
||
203 | * @return string the data cell value |
||
204 | */ |
||
205 | public function getDataCellValue($model, $key, $index) |
||
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | protected function renderDataCellContent($model, $key, $index) |
||
230 | } |
||
231 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: