I'd recommend to write a widget for displaying a links list of related records. It's reusable, prevents HTML generation in model / controller, reduces the amount of code in view.
<?php
namespace common\widgets;
use yii\base\Widget;
use yii\helpers\Html;
/**
* Widget for display list of links to related models
*/
class RelatedList extends Widget
{
/**
* @var \yii\db\ActiveRecord[] Related models
*/
public $models = [];
/**
* @var string Base to build text content of the link.
* You should specify attribute name. In case of dynamic generation ('getFullName()') you should specify just 'fullName'.
*/
public $linkContentBase = 'name';
/**
* @var string Route to build url to related model
*/
public $viewRoute;
/**
* @inheritdoc
*/
public function run()
{
if (!$this->models) {
return null;
}
$items = [];
foreach ($this->models as $model) {
$items[] = Html::a($model->{$this->linkContentBase}, [$this->viewRoute, 'id' => $model->id]);
}
return Html::ul($items, [
'class' => 'list-unstyled',
'encode' => false,
]);
}
}
Here are some examples (assuming tag name is stored in name column).
Usage in GridView:
[
'attribute' => 'tags',
'format' => 'raw',
'value' => function ($model) {
/* @var $model common\models\Post */
return RelatedList::widget([
'models' => $model->tags,
'viewRoute' => '/tags/view',
]);
},
],
Usage in DetailView:
/* @var $model common\models\Post */
...
[
'attribute' => 'tags',
'format' => 'raw',
'value' => RelatedList::widget([
'models' => $model->tags,
'viewRoute' => '/tags/view',
]),
],
Don't forget to set format raw, because by default content is rendered as plain text to prevent XSS attacks (html special characters are escaped).
You can modify this to fit your needs, this is just an example.