1 | <?php |
||
52 | class DetailView extends Widget |
||
53 | { |
||
54 | /** |
||
55 | * @var array|object the data model whose details are to be displayed. This can be a [[Model]] instance, |
||
56 | * an associative array, an object that implements [[Arrayable]] interface or simply an object with defined |
||
57 | * public accessible non-static properties. |
||
58 | */ |
||
59 | public $model; |
||
60 | /** |
||
61 | * @var array a list of attributes to be displayed in the detail view. Each array element |
||
62 | * represents the specification for displaying one particular attribute. |
||
63 | * |
||
64 | * An attribute can be specified as a string in the format of `attribute`, `attribute:format` or `attribute:format:label`, |
||
65 | * where `attribute` refers to the attribute name, and `format` represents the format of the attribute. The `format` |
||
66 | * is passed to the [[Formatter::format()]] method to format an attribute value into a displayable text. |
||
67 | * Please refer to [[Formatter]] for the supported types. Both `format` and `label` are optional. |
||
68 | * They will take default values if absent. |
||
69 | * |
||
70 | * An attribute can also be specified in terms of an array with the following elements: |
||
71 | * |
||
72 | * - `attribute`: the attribute name. This is required if either `label` or `value` is not specified. |
||
73 | * - `label`: the label associated with the attribute. If this is not specified, it will be generated from the attribute name. |
||
74 | * - `value`: the value to be displayed. If this is not specified, it will be retrieved from [[model]] using the attribute name |
||
75 | * by calling [[ArrayHelper::getValue()]]. Note that this value will be formatted into a displayable text |
||
76 | * according to the `format` option. Since version 2.0.11 it can be defined as closure with the following |
||
77 | * parameters: |
||
78 | * |
||
79 | * ```php |
||
80 | * function ($model, $widget) |
||
81 | * ``` |
||
82 | * |
||
83 | * `$model` refers to displayed model and `$widget` is an instance of `DetailView` widget. |
||
84 | * |
||
85 | * - `format`: the type of the value that determines how the value would be formatted into a displayable text. |
||
86 | * Please refer to [[Formatter]] for supported types and [[Formatter::format()]] on how to specify this value. |
||
87 | * - `visible`: whether the attribute is visible. If set to `false`, the attribute will NOT be displayed. |
||
88 | * - `contentOptions`: the HTML attributes to customize value tag. For example: `['class' => 'bg-red']`. |
||
89 | * Please refer to [[\yii\helpers\BaseHtml::renderTagAttributes()]] for the supported syntax. |
||
90 | * - `captionOptions`: the HTML attributes to customize label tag. For example: `['class' => 'bg-red']`. |
||
91 | * Please refer to [[\yii\helpers\BaseHtml::renderTagAttributes()]] for the supported syntax. |
||
92 | */ |
||
93 | public $attributes; |
||
94 | /** |
||
95 | * @var string|callable the template used to render a single attribute. If a string, the token `{label}` |
||
96 | * and `{value}` will be replaced with the label and the value of the corresponding attribute. |
||
97 | * If a callback (e.g. an anonymous function), the signature must be as follows: |
||
98 | * |
||
99 | * ```php |
||
100 | * function ($attribute, $index, $widget) |
||
101 | * ``` |
||
102 | * |
||
103 | * where `$attribute` refer to the specification of the attribute being rendered, `$index` is the zero-based |
||
104 | * index of the attribute in the [[attributes]] array, and `$widget` refers to this widget instance. |
||
105 | * |
||
106 | * Since Version 2.0.10, the tokens `{captionOptions}` and `{contentOptions}` are available, which will represent |
||
107 | * HTML attributes of HTML container elements for the label and value. |
||
108 | */ |
||
109 | public $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'; |
||
110 | /** |
||
111 | * @var array the HTML attributes for the container tag of this widget. The `tag` option specifies |
||
112 | * what container tag should be used. It defaults to `table` if not set. |
||
113 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
114 | */ |
||
115 | public $options = ['class' => 'table table-striped table-bordered detail-view']; |
||
116 | /** |
||
117 | * @var array|Formatter the formatter used to format model attribute values into displayable texts. |
||
118 | * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] |
||
119 | * instance. If this property is not set, the `formatter` application component will be used. |
||
120 | */ |
||
121 | public $formatter; |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Initializes the detail view. |
||
126 | * This method will initialize required property values. |
||
127 | */ |
||
128 | 9 | public function init() |
|
149 | |||
150 | /** |
||
151 | * Renders the detail view. |
||
152 | * This is the main entry of the whole detail view rendering. |
||
153 | * @return string the result of widget execution to be outputted. |
||
154 | */ |
||
155 | public function run() |
||
167 | |||
168 | /** |
||
169 | * Renders a single attribute. |
||
170 | * @param array $attribute the specification of the attribute to be rendered. |
||
171 | * @param int $index the zero-based index of the attribute in the [[attributes]] array |
||
172 | * @return string the rendering result |
||
173 | */ |
||
174 | 4 | protected function renderAttribute($attribute, $index) |
|
189 | |||
190 | /** |
||
191 | * Normalizes the attribute specifications. |
||
192 | * @throws InvalidConfigException |
||
193 | */ |
||
194 | 9 | protected function normalizeAttributes() |
|
252 | } |
||
253 |