{"action":"closed","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9162","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9162/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9162/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9162/events","html_url":"https://github.com/yiisoft/yii2/issues/9162","id":96155172,"number":9162,"title":"A reason to support DetailView value as Closure - delayed evaluation of value","user":{"login":"PowerGamer1","id":8275897,"avatar_url":"https://avatars.githubusercontent.com/u/8275897?v=3","gravatar_id":"","url":"https://api.github.com/users/PowerGamer1","html_url":"https://github.com/PowerGamer1","followers_url":"https://api.github.com/users/PowerGamer1/followers","following_url":"https://api.github.com/users/PowerGamer1/following{/other_user}","gists_url":"https://api.github.com/users/PowerGamer1/gists{/gist_id}","starred_url":"https://api.github.com/users/PowerGamer1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PowerGamer1/subscriptions","organizations_url":"https://api.github.com/users/PowerGamer1/orgs","repos_url":"https://api.github.com/users/PowerGamer1/repos","events_url":"https://api.github.com/users/PowerGamer1/events{/privacy}","received_events_url":"https://api.github.com/users/PowerGamer1/received_events","type":"User","site_admin":false},"labels":[{"id":33330247,"url":"https://api.github.com/repos/yiisoft/yii2/labels/status:ready%20for%20adoption","name":"status:ready for adoption","color":"02e10c","default":false},{"id":33332028,"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:enhancement","name":"type:enhancement","color":"d7e102","default":false}],"state":"closed","locked":false,"assignee":{"login":"samdark","id":47294,"avatar_url":"https://avatars.githubusercontent.com/u/47294?v=3","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"assignees":[{"login":"samdark","id":47294,"avatar_url":"https://avatars.githubusercontent.com/u/47294?v=3","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/20","html_url":"https://github.com/yiisoft/yii2/milestones/2.0.11","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/20/labels","id":1736699,"number":20,"title":"2.0.11","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"open_issues":123,"closed_issues":78,"state":"open","created_at":"2016-04-28T15:04:50Z","updated_at":"2016-11-18T08:32:08Z","due_on":"2016-12-18T00:00:00Z","closed_at":null},"comments":14,"created_at":"2015-07-20T20:41:58Z","updated_at":"2016-11-18T08:32:08Z","closed_at":"2016-11-18T08:32:08Z","body":"Currently DataColumn supports setting value as Closure, while DetailView - does not, which is inconsistent. Often exactly the same formatting function is used to format the value in both GridView and DetailView.\n\nAlso, allowing setting value as Closure in DetailView delays the evaluation of that value until the actual rendering of that value takes place. And that evaluation may not happen at all if visible is set to false. This allows for the following code to work.\n\nInitial (non-working) code:\n\n```\n<?= \\yii\\widgets\\DetailView::widget([\n    'model' => $model,\n    'attributes' => [\n        [\n            'label' => 'User Info',\n            // When $model->user is null we get PHP error.\n            'value' => '<span class=\"' ($model->user->is_enabled ? 'green' : 'red') . '\">' . $model->user->name . '</span>',\n            'visible' => $model->user !== null,\n        ],\n    ],\n]); ?>\n```\n\nA workaround without support for Closures:\n\n```\n'value' => ($model->user !== null ? '<span class=\"' ($model->user->is_enabled ? 'green' : 'red') . '\">' . $model->user->name . '</span>' : ''),\n'visible' => $model->user !== null,\n```\n\nBut why add additional check for $model->user !== null (when it already present in 'visible') and define an empty string value for 'value' when that empty string case will never happen?\n\nBetter code with support for Closures:\n\n```\n// When $model->user is null Closure will not be called at all (due to visible being false), no PHP error.\n'value' => function(){ return '<span class=\"' ($model->user->is_enabled ? 'green' : 'red') . '\">' . $model->user->name . '</span>'; },\n'visible' => $model->user !== null,\n```\n"}}
{"actor":{"display_login":"klimov-paul"}}