Html::a Not Rendering Data Attribute

Hi,

I don’t know if this is a bug, 'coz I can’t figure out why the Html::a is not rendering my expected element and it attributes in the GridView.

Here is how i implemented to render the cell data into a link.




['attribute' => 'name', 'format' => 'html', 'value' => function ($data) {

            return Html::a($data->name, '#', ['title'=>'Click to view product details','class' => 'modalLink', 'data' => ['path'=>'product/view/id/','id'=>$data->id]]);



And the problem is, i’m getting this html element.


<a class="modalLink" href="#" title="Click to view product details">Camel Air Dryer</a>

instead of




<a class="modalLink" href="#" title="Click to view product details" data-path="product/view/id/" data-id="1">Camel Air Dryer</a>



Here is what I did to debug the problem.

I echo the html being composed by BaseHtml::a


    

    public static function a($text, $url = null, $options = [])

    {

        if ($url !== null) {

            $options['href'] = Url::to($url);

        }

        $html= static::tag('a', $text, $options);

        echo $html;

        return $html;

    }

Using the code above i saw that the BaseHtml::a returns the actual:


<a class="modalLink" href="#" title="Click to view product details" data-path="product/view/id/" data-id="1">Camel Air Dryer</a>

But then in GridView, i got only this.


<a class="modalLink" href="#" title="Click to view product details">Camel Air Dryer</a>

Please help.

My first guess is that those attributes are being stripped by the HTML purifier. Try changing the format to ‘raw’.

1 Like

thanks a lot Keith, it works!