HTML in Yii2 GridView - html

I'm trying to display some HTML encoded strings as pure HTML in a column positioned in Yii2 GridView. The string that comes from the database looks like this:
Testing <span class='test'>HTML</span>
If I just display it using HTML decode Html::decode($theStringAbove); then I get this in the column:
Testing <span class='test'>HTML</span>
However, what I'm aiming at is simply getting Testing HTML and having the span tag around the HTML word in the code but not displayed as a string.
I've tried setting different values to the format attribute such as raw, url and html to no success. I also have no problem writing a custom function to return the correct output next to the value attribute, it's just that I can't figure out how to get to the output I need. Any suggestions are welcome, thank you!
EDIT: Here's a small code snippet if that's helpful:
[ 'format' => 'raw',
'value' => function($model) { return Html::decode($model->text); },
'label' => Yii::t('app', 'Some Label')]

The database output is probably encoded twice so instead of
Testing <span class='test'>HTML</span>
it's
Testing &lt;span class=&#039;test&#039;&gt;HTML&lt;/span&‌​gt;
Try to decode it twice like:
'value' => function ($model) {
return Html::decode(Html::decode($model->replace));
}

Related

How to do a breakline in yii2?

I'm trying to combine two data into one column. I already managed to combine two data in one column. But my problem is I don't know how to do a break line to separate the data. I have tried this way but doesn't work.
This is my code
'NAME_PROG_ENG'.'REMARKS' => ['label' => "Programme Name",'value' => function ($data) {return ($data->NAME_PROG_ENG)."\r\n".nl2br("Previously known as: ".($data->REMARKS));}],
I think you refer to GridView component.
Some explanation:
'NAME_PROG_ENG'.'REMARKS' =>
the array index is the model field name, you can't combine 2 columns just by concatenating the column name. In this way you just define a column in the grid wich is not connected to the model. This is not an error, is just to notice that this does not give you the expected result.
'value' => function ($data) {return ($data->NAME_PROG_ENG)."\r\n".nl2br("Previously known as: ".($data->REMARKS));}
Between $data->NAME_PROG_ENG and $data->REMARKS you put a "\r\n" which is fine, but you apply nl2br() function only to the second part of the string.
Now about Gridview. Text are automatically converted to entities, this is a security feature to avoid script injection from user input. If you plan to use input from an untrusted user, ensure that you clean it using htmlpurifier before saving it in database or before showing it (in your 'value'=>... function)
https://www.yiiframework.com/search?language=en&version=2.0&type=guide&q=htmlpurifier
You can bypass html entity conversion by specifies the output format as raw in grid column option
'format'=>'raw'
So you column definition should be something like
'prog_and_remark_combined' => [
'format' => 'raw',
'label' => "Programme Name",
'value' => function ($data) {
return nl2br(
$data->NAME_PROG_ENG .
"\r\nPreviously known as: " .
$data->REMARKS
);
}
],
Gridview allow a short notation
https://www.yiiframework.com/doc/api/2.0/yii-grid-gridview#$columns-detail
which is like this
<column name>:<fomat>:<label>
your code can be as follow as well
'prog_and_remark_combined:raw:Programme Name' => [
'value' => function ($data) {
return nl2br(
$data->NAME_PROG_ENG .
"\r\nPreviously known as: " .
$data->REMARKS
);
}
],
Last note, if the fields does not contain new line to be converted, just concatenate them without using nltobr() function
return $data->NAME_PROG_ENG . "<br>Previously known as: " . $data->REMARKS

HTML in YII2 Kartik Gridview column label (not heading)

I have a Kartik gridview in YII2 and I need to be able to put HTML into the label (not header, I'll come to that) of a column.
My column definition is as such
[
'attribute' => 'picked_percent',
'format' => 'raw',
'label' => 'P<span class="responsive">icked</span>',
],
But if I do this, it outputs
P<span class="drawn_head">icked </span>
I can change label to header and it looks fine but I need it to be clickable and it isn't when I change it to header.
I've also tried changing the format from raw to HTML and that makes no difference.
Any help is much appreciated.
If you want the label to not be encoded you need to add this config option in the column definition array:
'encodeLabel' => false

Cakephp 3: Modifying Results from the database

In my database there is a content table and when fetching data from this table I would like to append field url to the results, which is based on slug field which is contained in the table. Anyway, I have seen a way to do this in the previous versions of cakephp using behavior for the model of this table and then modifying results in afterFind callback in the behavior class. But in version 3 there is no afterFind callback, and they recommend using mapReduce() method instead in the manual, but this method is poorly explained in the manual and I cant figure out how to achieve this using mapReduce().
After little bit of research I realized that the best way to append the url field field to find results is using formatResults method, So this is what I did in my finders:
$query->formatResults(function (\Cake\Datasource\ResultSetInterface $results) {
return $results->map(function ($row) {
$row['url'] = array(
'controller' => 'content',
'action' => 'view',
$row['slug'],
$row['content_type']['alias']
);
return $row;
});
});

Raw HTML in HelperList column

I have a HelperList in Prestashop (1.6) with a few columns/rows. One of the columns is an anchor element (a href), and I need to show it like that in the list, but Prestashop escapes the value and represents it as a literal string.
How can I show an actual anchor element in one of the columns of a HelperList?
Plus: I'd like to not have to override PS classes nor copy the entire template just to change a single line of code. I do know how to do it using either of those ways, but I'm looking for something less "aggressive".
You can use this trick:
$fields_list = array(
'your_link' => array(
'title' => $this->l('Your title'),
'type' => 'bool',
'float' => true, // a trick - prevents from html escaping
// else code
),
);

Zend Forms and Ext.grid.Panel

I am working for a company who use tabulated html/JS interfaces. These are home grown (real honest to god s) with query events attached to each cell. For the old usage they were suitable, but the interactions required between rows and cells are becoming much more complex on the client side. Specifically they want both server and client side validation.
To facilitate this, the devs I report to are super keen on Zend_Forms, and insist that to use a framework like ExtJS, they don't want to have to write back end and front end code twice (please ignore that if it's all home grown they'll have to do this anyway).
So with that in mind, I'm trying to leverage Zend_Form decorators to create Ext.grid.Panel column defintions. For this, I would need to use decorators to export an array (and then json it using the ViewHelper), or render a JSON string directly.
So this would be something like:
$dateElement = new Zend_Form_Element_Text('startDate', array(
'label' => 'Start Date',
'validators' => array(
new Zend_Validate_Date()
)
));
echo (string)$dateElement;
would output:
{ text: 'Start Date', dataIndex:'startDate', xtype:'datecolumn'}
or (obviously not with string cast, but maybe with ->toArray() or something):
array( 'text' => 'Start Date', 'dataIndex' => 'startDate', 'xtype' => 'datecolumn')
I think if I could get it to this stage, I could get what I need out of it.
Has anyone here tried to do anything similiar to this (getting a JSON/XML/other markups output, rather than HTML from Zend_Forms using Decorators) or if they could point me to any resources?
I think I have a solution...
Make a decorator similar to this:
class My_Form_JSON_Decorator extends Zend_Form_Decorator_Abstract{
protected $xtype;
protected $dataIndex;
public function __construct($dataIndex,$xtype){
$this->xtype=$xtype;
$this->dataIndex=$dataIndex;
}
public function render($content){
$element=$this->getElement();
$label=$element->getLabel
//if you need errors here too do the same with $element->getMessages();
return 'array ("text"=>"'.$label.'","dataIndex"=>"'.$this->dataIndex.'","datecolumn"=>"'.$this->xtype.'")';
}
}
Then, on the form, use something similar to this:
$dateElement = new Zend_Form_Element_Text('startDate', array(
'label' => 'Start Date',
'validators' => array(
new Zend_Validate_Date()
)
$dateElement->setDecorators(array(
new My_Form_JSON_Decorator("startDate","datecolumn");
));
And finally, on the View, you should have this:
{
Date: <?php echo $this->form->startDate; ?>,
}
I didn't tried the code above but, I did it with a similar code I used once when I needed to change Decorators of a Form.
It could not be all correct but, I think that it shows you a way of doing that.
Good work =)