Yii1 - use array in the GridView column and push value in that array - yii

I have following arrays outside GridView:
$companyIDs = [];
$companyNames = [];
And following column:
'company_id' => [
'name' => 'company_id',
'header' => 'Client',
'type' => 'raw',
'value' => function ($data, $row) use($companyIDs, $companyNames) {
array_push($companyIDs, $data->company_id);
var_dump('<pre>', $companyIDs, '</pre>');
return $data->company->company_name;
},
],
At the moment the array is destroyed each time and only contains one value. Is there a way to push values to the array?

What if you push your items into the array before the Gridview?

Related

Yii2- ArrayHelper change index of array

I have an array as follows:
[
0 => [
'name' => 'CARD'
'id' => '0'
]
1 => [
'name' => 'MOBILE'
'id' => '1'
]
2 => [
'name' => 'GIFT'
'id' => '2'
]
]
I want to change the key id to type in all the array. Is there a way to do this in Yii2 using ArrayHelper?
You can use getColumn() for this:
$result = ArrayHelper::getColumn($array, function ($data) {
return [
'name' => $data['name'],
'type' => $data['id'],
];
});
But it will not really differ from array_map() or simple foreach.
There is not an array helper for this but you could do this with a php foreach
foreach ($myArray as $key => $value) {
$myArray[$key]['type'] = $value['id'];
unset($myArray[$key]['id']);
}

How to add an entity content programmatically in drupal

Good night: I used to create node programmatically with a code similar to:
use Drupal\node\Entity\Node;
$nodeObj = Node::create([
'type' => 'article',
'title' => 'Programatically created Article',
'body' => "CodeExpertz is contains best Blogs.",
'field_date' => '2017-10-24',
'field_category' => 'Study',
'uid' => 1,
]);
$nodeObj->save(); // Saving the Node object.
$nid = $nodeObj->id(); // Get Nid from the node object.
Print "Node Id is " . $nid;
Now I want to create entities content (no nodes) but I can't find something about this. I tried to adapt the next snippet:
$term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'test_vocabulary',
'name' => 'My tag',
]);
$term->save();
to this (vehicle is my entity):
$newvehicle = \Drupal\vehicle\Entity\Vehicle::create([
'title' => 'Ferrari'
]);
$newvehicle->save();
The result is the white page of death.
Thanks for your help.
I was able to do it with this code
use Drupal\customModule\Entity\entityCustom;
$entityCustom = entityCustom::create([
'type' => 'custom_entity',
'uid' => 1,
'status' => TRUE,
'promote' => 0,
'created' => time(),
'langcode' => 'en',
'name' => 'NAME',
]);
$entityCustom->save();

How can i get first and last record id in yii CGridview?

I want first and last record id form $dataprovider which is passed to gridview which i need to pass to this link.
array(
'name' => 'msg',
'value' => 'CHtml::link(substr(strip_tags($data->msg),0,30)." .....",Yii::app()->createUrl("Mail/view",array("id"=>$data->primaryKey,"flag"=>"inbox","tab"=>'.$tab.',"category"=>'.$category.')))',
'type' => 'raw',
'header' => 'Message',
),
Declare two attributes in your controller.
public $first=null;
public $last=null;
Define a function in controller to render your link
public function renderMailViewLink($data,$row){
// you can return anything you want here. whether a link or whatever
// access $this->first->id , $this->last->id
return CHtml::link($data->anyAttribute,array('someRoute','id'=>$data->id,'first'=>$this->first->id))
}
CActiveDataProvider has a method getData(), which return array of all active records.
in actionIndex
$dataProvider = $model->search()
$data = $dataProvider->getData();
$this->first = reset($data );
$this->last = end($data);
And finally in your view
array(
'name' => 'msg',
'value' => array($this,'renderMailViewLink'),
'type' => 'raw',
'header' => 'Message',
),
Please note this code is not tested. But thats how it can be achieved

Dynamically update highcharts via setData for Yii-created Highcharts Widget

Is it possible to update a highcharts chart using the chart.series[i].setData method if the chart has been created by the Yii extension? I have used highcharts before without Yii extension and able to reference my variables once I set the chart up via declared variable var chart = .... I would just like to to able to call the setData methods for a Yii-created highcharts...this is current code for creating chart:
$this->Widget('ext.highcharts.HighchartsWidget', array(
'id' => 'shop_pie',
'options' => array(
'chart' => array(
'backgroundColor' => '#efefef'
),
'colors' => $colors,
'title' => array('text' => 'Shop'),
'plotOptions' => array(
'pie' => array(
'showInLegend' => 'true',
'dataLabels' => array(
'formatter' => 'js:function(){return this.point.y}',
'distance' => -10,
'color' => '#000'
),
),
'series' => $results['series'],
)
));
My ajax call to update the chart:
"$('#city_filter').on('change',function(e) {
$.ajax({
url: ". $quotedUrl . ",
data: { 'cities': e.val },
success: function(data) {
$('#name_span').text(data[0]);
//chart.series[0].setData(data[1]); // THIS IS WHERE I NEED TO BE ABLE TO REFERENCE THE CHART
}
});
});",
Many thanks in advance.
Andy
"$('#city_filter').on('change',function(e) {
$.ajax({
url: ". $quotedUrl . ",
data: { 'cities': e.val },
success: function(data) {
$('#name_span').text(data[0]);
var chart = $('#shop_pie').highcharts();
chart.series[0].setData(data[1]); // THIS IS WHERE I NEED TO BE ABLE TO REFERENCE THE CHART
}
});
});",
referring to
http://api.highcharts.com/highcharts#Series.setData
and example from that article
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/

Attach inputFilter to dynamcially created field elements

Until now I have been binding input filters to the form in the module, in other words I have been creating elements in the form, adding input filters to the elements on the module side.
For example check this example
Right now im creating text field elements dynamically depending upon the requirements, like this in my form
//Form
public function addNamesTextFieldElement($names)
{
foreach($names as $name)
{
$nameTextField = new Element\Text($name->getName());
$nameTextField->setAttribute('type', "text");
$nameTextField->setLabel($name->getName());
$this->add($nameTextField );
}
}
What would be best approach where to add/attach input filters to such dynamically generated elements.
I probably wouldn't use this approach, but something like this would work, providing you have already assigned an InputFilter to the form:
public function addNamesTextFieldElement($names)
{
$factory = new InputFactory();
foreach($names as $name)
{
$nameTextField = new Element\Text($name->getName());
$nameTextField->setAttribute('type', "text");
$nameTextField->setLabel($name->getName());
$this->add($nameTextField );
$this->getInputFilter()->add(
$factory->createInput(array(
'name' => $name->getName(),
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
))
);
}
}