DetailView and URL value generation

I have a database table within which there is a column containing URLs.
The generated index/view views allow me to peruse the content of the table as expected. Somehow the Gii tool has determined to specify the relevant attribute in the DetailView specification as ‘fieldname:url’.
My objective is for the link generated to support the linked page to be presented in a separate browser tab. I have been totally unable to find a way to tell the DetailView widget to do what I want.
I have inserted the following in the view.php file just before the invocation of the DetailView widget:

<?= Html::a($model->fieldnamel, Url::to($model->fieldname,true) , ['target' => '_blank']) ?>

As expected, I see the value of the column fieldname presented as an URL The generated HTML is as follows:
fieldname value
And when I click on the link the requested page is presented in a separate browser tab. So far so good I thought.
I replaced the attribute speciation as generated by Gii:
‘fieldname:url’,
To the following:
[ ‘attribute’ => ‘fieldname’,
‘label’ => ‘Test URL’,
‘value’ => function($model) {
return Html::a(‘Link URL’, Url::to($model->fieldname,true) , [‘target’ => ‘_blank’]);
},
‘format’ => ‘html’,
],
The generated HTML was not as I expected:
Test URL
If I use a format value of ‘url’, then the generated HTML is as follows:
<a href=“fieldname value” target="_blank">TVmaze episode URL</a>
Which generates an HTTP 404 when clicking on the link.
My trial and error strategy is clearly failing and I suspect I am missing something basic, but I am flummoxed. Any help/guidance would be appreciated.

1 Like

You need this to be in the raw format for GridView:

[ ‘attribute’ => ‘fieldname’,
‘label’ => ‘Test URL’,
‘value’ => function($model) {
return Html::a(‘Link URL’, Url::to($model->fieldname,true) , [‘target’ => ‘_blank’]);
},
‘format’ => ‘raw’,
]

1 Like

Many thanks for the response. Logical when I look at it, but for me it is part of a learning curve. I have success.

1 Like