1 | <?php |
||
38 | class DataColumn extends Column |
||
39 | { |
||
40 | /** |
||
41 | * @var string the attribute name associated with this column. When neither [[content]] nor [[value]] |
||
42 | * is specified, the value of the specified attribute will be retrieved from each data model and displayed. |
||
43 | * |
||
44 | * Also, if [[label]] is not specified, the label associated with the attribute will be displayed. |
||
45 | */ |
||
46 | public $attribute; |
||
47 | /** |
||
48 | * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting |
||
49 | * link label when sorting is enabled for this column. |
||
50 | * If it is not set and the models provided by the GridViews data provider are instances |
||
51 | * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]]. |
||
52 | * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label. |
||
53 | */ |
||
54 | public $label; |
||
55 | /** |
||
56 | * @var bool whether the header label should be HTML-encoded. |
||
57 | * @see label |
||
58 | * @since 2.0.1 |
||
59 | */ |
||
60 | public $encodeLabel = true; |
||
61 | /** |
||
62 | * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column. |
||
63 | * |
||
64 | * If this is an anonymous function, it will be called for each row and the return value will be used as the value to |
||
65 | * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`. |
||
66 | * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
||
67 | * and `$column` is a reference to the [[DataColumn]] object. |
||
68 | * |
||
69 | * You may also set this property to a string representing the attribute name to be displayed in this column. |
||
70 | * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for |
||
71 | * sorting and filtering. |
||
72 | * |
||
73 | * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]]. |
||
74 | */ |
||
75 | public $value; |
||
76 | /** |
||
77 | * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, |
||
78 | * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by |
||
79 | * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when |
||
80 | * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView. |
||
81 | */ |
||
82 | public $format = 'text'; |
||
83 | /** |
||
84 | * @var bool whether to allow sorting by this column. If true and [[attribute]] is found in |
||
85 | * the sort definition of [[GridView::dataProvider]], then the header cell of this column |
||
86 | * will contain a link that may trigger the sorting when being clicked. |
||
87 | */ |
||
88 | public $enableSorting = true; |
||
89 | /** |
||
90 | * @var array the HTML attributes for the link tag in the header cell |
||
91 | * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column. |
||
92 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
93 | */ |
||
94 | public $sortLinkOptions = []; |
||
95 | /** |
||
96 | * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list) |
||
97 | * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set. |
||
98 | * |
||
99 | * - If this property is not set, a text field will be generated as the filter input; |
||
100 | * - If this property is an array, a dropdown list will be generated that uses this property value as |
||
101 | * the list options. |
||
102 | * - If you don't want a filter for this data column, set this value to be false. |
||
103 | */ |
||
104 | public $filter; |
||
105 | /** |
||
106 | * @var array the HTML attributes for the filter input fields. This property is used in combination with |
||
107 | * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to |
||
108 | * render the HTML attributes for the generated filter input fields. |
||
109 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
110 | */ |
||
111 | public $filterInputOptions = ['class' => 'form-control', 'id' => null]; |
||
112 | |||
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | protected function renderHeaderCellContent() |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | * @since 2.0.8 |
||
139 | */ |
||
140 | 2 | protected function getHeaderCellLabel() |
|
170 | |||
171 | /** |
||
172 | * @inheritdoc |
||
173 | */ |
||
174 | 2 | protected function renderFilterCellContent() |
|
175 | { |
||
176 | 2 | if (is_string($this->filter)) { |
|
177 | 1 | return $this->filter; |
|
178 | } |
||
179 | |||
180 | 1 | $model = $this->grid->filterModel; |
|
181 | |||
182 | 1 | if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) { |
|
183 | 1 | if ($model->hasErrors($this->attribute)) { |
|
184 | Html::addCssClass($this->filterOptions, 'has-error'); |
||
185 | $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions); |
||
186 | } else { |
||
187 | 1 | $error = ''; |
|
188 | } |
||
189 | 1 | if (is_array($this->filter)) { |
|
190 | 1 | $options = array_merge(['prompt' => ''], $this->filterInputOptions); |
|
191 | 1 | return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error; |
|
192 | } elseif ($this->format === 'boolean') { |
||
193 | $options = array_merge(['prompt' => ''], $this->filterInputOptions); |
||
194 | return Html::activeDropDownList($model, $this->attribute, [ |
||
195 | $this->grid->formatter->booleanFormat[0], |
||
196 | $this->grid->formatter->booleanFormat[1], |
||
197 | ], $options) . $error; |
||
198 | } else { |
||
199 | return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error; |
||
200 | } |
||
201 | } else { |
||
202 | return parent::renderFilterCellContent(); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Returns the data cell value. |
||
208 | * @param mixed $model the data model |
||
209 | * @param mixed $key the key associated with the data model |
||
210 | * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. |
||
211 | * @return string the data cell value |
||
212 | */ |
||
213 | public function getDataCellValue($model, $key, $index) |
||
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | protected function renderDataCellContent($model, $key, $index) |
||
238 | } |
||
239 |
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: