1 | <?php |
||
39 | class DataColumn extends Column |
||
40 | { |
||
41 | /** |
||
42 | * @var string the attribute name associated with this column. When neither [[content]] nor [[value]] |
||
43 | * is specified, the value of the specified attribute will be retrieved from each data model and displayed. |
||
44 | * |
||
45 | * Also, if [[label]] is not specified, the label associated with the attribute will be displayed. |
||
46 | */ |
||
47 | public $attribute; |
||
48 | /** |
||
49 | * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting |
||
50 | * link label when sorting is enabled for this column. |
||
51 | * If it is not set and the models provided by the GridViews data provider are instances |
||
52 | * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]]. |
||
53 | * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label. |
||
54 | */ |
||
55 | public $label; |
||
56 | /** |
||
57 | * @var bool whether the header label should be HTML-encoded. |
||
58 | * @see label |
||
59 | * @since 2.0.1 |
||
60 | */ |
||
61 | public $encodeLabel = true; |
||
62 | /** |
||
63 | * @var string|Closure an anonymous function or a string that is used to determine the value to display in the current column. |
||
64 | * |
||
65 | * If this is an anonymous function, it will be called for each row and the return value will be used as the value to |
||
66 | * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`. |
||
67 | * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
||
68 | * and `$column` is a reference to the [[DataColumn]] object. |
||
69 | * |
||
70 | * You may also set this property to a string representing the attribute name to be displayed in this column. |
||
71 | * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for |
||
72 | * sorting and filtering. |
||
73 | * |
||
74 | * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]]. |
||
75 | */ |
||
76 | public $value; |
||
77 | /** |
||
78 | * @var string|array|Closure in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, |
||
79 | * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by |
||
80 | * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when |
||
81 | * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView. |
||
82 | * @see \yii\i18n\Formatter::format() |
||
83 | */ |
||
84 | public $format = 'text'; |
||
85 | /** |
||
86 | * @var bool whether to allow sorting by this column. If true and [[attribute]] is found in |
||
87 | * the sort definition of [[GridView::dataProvider]], then the header cell of this column |
||
88 | * will contain a link that may trigger the sorting when being clicked. |
||
89 | */ |
||
90 | public $enableSorting = true; |
||
91 | /** |
||
92 | * @var array the HTML attributes for the link tag in the header cell |
||
93 | * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column. |
||
94 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
95 | */ |
||
96 | public $sortLinkOptions = []; |
||
97 | /** |
||
98 | * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list) |
||
99 | * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set. |
||
100 | * |
||
101 | * - If this property is not set, a text field will be generated as the filter input with attributes defined |
||
102 | * with [[filterInputOptions]]. See [[\yii\helpers\BaseHtml::activeInput]] for details on how an active |
||
103 | * input tag is generated. |
||
104 | * - If this property is an array, a dropdown list will be generated that uses this property value as |
||
105 | * the list options. |
||
106 | * - If you don't want a filter for this data column, set this value to be false. |
||
107 | */ |
||
108 | public $filter; |
||
109 | /** |
||
110 | * @var array the HTML attributes for the filter input fields. This property is used in combination with |
||
111 | * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to |
||
112 | * render the HTML attributes for the generated filter input fields. |
||
113 | * By default a `'class' => 'form-control'` element will be added if no class has been specified. |
||
114 | * If you do not want to create a class attribute, you can specify `['class' => null]`. |
||
115 | * |
||
116 | * Empty `id` in the default value ensures that id would not be obtained from the model attribute thus |
||
117 | * providing better performance. |
||
118 | * |
||
119 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
120 | */ |
||
121 | public $filterInputOptions = []; |
||
122 | |||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | protected function renderHeaderCellContent() |
||
128 | { |
||
129 | if ($this->header !== null || $this->label === null && $this->attribute === null) { |
||
130 | return parent::renderHeaderCellContent(); |
||
131 | } |
||
132 | |||
133 | $label = $this->getHeaderCellLabel(); |
||
134 | if ($this->encodeLabel) { |
||
135 | $label = Html::encode($label); |
||
136 | } |
||
137 | |||
138 | if ($this->attribute !== null && $this->enableSorting && |
||
139 | ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { |
||
140 | return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label])); |
||
141 | } |
||
142 | |||
143 | return $label; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc] |
||
148 | * @since 2.0.8 |
||
149 | */ |
||
150 | 2 | protected function getHeaderCellLabel() |
|
151 | { |
||
152 | 2 | $provider = $this->grid->dataProvider; |
|
153 | |||
154 | 2 | if ($this->label === null) { |
|
155 | 2 | if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) { |
|
156 | /* @var $modelClass Model */ |
||
157 | $modelClass = $provider->query->modelClass; |
||
|
|||
158 | $model = $modelClass::instance(); |
||
159 | $label = $model->getAttributeLabel($this->attribute); |
||
160 | 2 | } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) { |
|
161 | /* @var $modelClass Model */ |
||
162 | 1 | $modelClass = $provider->modelClass; |
|
163 | 1 | $model = $modelClass::instance(); |
|
164 | 1 | $label = $model->getAttributeLabel($this->attribute); |
|
165 | 1 | } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) { |
|
166 | 1 | $label = $this->grid->filterModel->getAttributeLabel($this->attribute); |
|
167 | } else { |
||
168 | $models = $provider->getModels(); |
||
169 | if (($model = reset($models)) instanceof Model) { |
||
170 | /* @var $model Model */ |
||
171 | $label = $model->getAttributeLabel($this->attribute); |
||
172 | } else { |
||
173 | 2 | $label = Inflector::camel2words($this->attribute); |
|
174 | } |
||
175 | } |
||
176 | } else { |
||
177 | $label = $this->label; |
||
178 | } |
||
179 | |||
180 | 2 | return $label; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 11 | protected function renderFilterCellContent() |
|
220 | |||
221 | /** |
||
222 | * Returns the data cell value. |
||
223 | * @param mixed $model the data model |
||
224 | * @param mixed $key the key associated with the data model |
||
225 | * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. |
||
226 | * @return string the data cell value |
||
227 | */ |
||
228 | public function getDataCellValue($model, $key, $index) |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | protected function renderDataCellContent($model, $key, $index) |
||
255 | } |
||
256 |
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: