Cgridview With An Image Column Doesn't Render

Hi guys,

I have an image column in a CGridView widget, it partially works, the following code works:




array('type'=>'html',

      'value'=>'CHtml::tag("img", array("src" => "http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&s=60"),"",true)'

),



As you can see, the url is hardcoded, so instead, I have created a function that recovers an url:


AccountController::getPhotoUrl($data->author_id, $data->photo)

I have tried in several ways, but I just can’t get this done, nothing of this works:




array('type'=>'html',

      'value'=>'CHtml::tag("img", array("src" => "AccountController::getPhotoUrl($data->author_id, $data->photo)"),"",true)'

),


//This render something like this: CHtml::image(AccountController::getPhotoUrl(1, https://graph.facebook.com/XXXXX/picture?type=square))

//Which is correct, because its passing the correct values to the function

array('type'=>'html',

      'value'=>'"CHtml::image(AccountController::getPhotoUrl($data->author_id, $data->photo))"'

),


array('type'=>'image',

      'value'=>"AccountController::getPhotoUrl($data->author_id, $data->photo)"

),



I would really appreciate if someone can help me. Thanks!

Remove the quotes around the method call:




array('type'=>'html',

      'value'=>'CHtml::tag("img", array("src" => AccountController::getPhotoUrl($data->author_id, $data->photo)),"",true)'

),



Thanks for your response Keith, but it doesn’t work, I got this error:

TbDataColumn and its behaviors do not have a method or closure named "getPhotoUrl".

Anyone? I have tried in many ways, does someone know some solution?

Thanks!

Try using an anonymous function instead:




array(

    'type'=>'html',

    'value'=>function($data){

        return CHtml::image(AccountController::getPhotoUrl($data->author_id, $data->photo));

    },

),



Hi Keith,

Thanks for your response again, unfortunately it doesn’t work, I don’t know why it throws an exception:

"Parse error: syntax error, unexpected T_FUNCTION in C:\AppServ\www\yii4\htdocs\protected\modules\causes\views\causeDonation\admin.php on line 69"

I have been reading the class reference and it should accept an anonymous function as you said:




'columns'=>array(		

     array('type'=>'html',

           'value'=>function($data){

               return CHtml::image(AccountController::getPhotoUrl($data->author_id, $data->photo));

           },

     ),



Thanks again!

Hi there,

you can try the below code


array(

'name'=>'image',

'header'=>'Image',

'filter'=>'',

'type' => 'html',

'value'=> 'CHtml::tag("div", array("style"=>"text-align: center" ) , CHtml::tag("img", array("height"=>\''.$height.'\',\'width\'=>\''.$width.'\',"src" => UtilityHtml::getImageCoupon(GxHtml::valueEx($data,\'image\')))))',




),   

Best Regards,

Ankit Modi

Yeeeessss!!! finally!!! thank you so much, without your help I wouldn’t had got by myself!