How to pass a variable set in Controller to Select Helper "value"? - phalcon

I have something like this in my Controller:
$this->view->userTagsComma = ($skills[0] == 'No Skills Set')?'':'"' . implode('", "', $skills) . '"';
And this generates something like "Cooking", "Swimming" when I echo this in View.
And in my View I have:
<?php echo Phalcon\Tag::select(array('user-skills-input',
TagsStandard::find("status = 1"),
"using" => array("name", "name"),
"class" => "select-chosen",
"type" => "text",
"data-role" => "tagsinput",
"value" => [],
"multiple" => "multiple"
)); ?>
I want to pass $userTagsComma into [] in my select helper so how can I do it? I tried doing "view" => [$userTagsComma] in the select helper but it does not work.
Update: I tried putting in "value" => ["Cooking", "Swimming"], and this works. So not sure why passing in a variable with everything looking the same wouldn't work.

Try this!
$this->view->userTagsComma = $skills[0] == 'No Skills Set' ? array() : $skills;
Phalcon\Tag::select(array('user-skills-input',
TagsStandard::find("status = 1"),
"using" => array("name", "name"),
"class" => "select-chosen",
"type" => "text",
"data-role" => "tagsinput",
"value" => $userTagsComma,
"multiple" => "multiple"
));

Use this within Controller $this->view->userTagsComma = ($skills[0] == "No Skills Set") ? [] : $skills; instead of imploding as it is already an array.

Related

Yii2 detailView highlight value on condition

How is it possible to highlight a value in yii2 detailView based on a condition?
I've tried this way (with and without 'type' => 'raw', 'html'), but no luck:
[
'attribute' => 'attribute',
'type' => 'raw',
'value' => $model->attribute > 1 ? '<b>' . $model->attribute . '</b>' : $model->attribute,
],
Thank you!
You need use the format property, not type:
'format' => 'html'

Yii 2.0 Select Pre-selected values from Database

I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.

Get images by custom field

I'm trying to display all images that have a certain custom field from the types plugin set to true. It would also work to filter them by post_content or post_excerpt but none of my attempts have worked so far.
<?
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_content' => 'foo',
'numberposts' => -1
);
?>
<? print_r(get_posts($args)); ?>
This get's all images allthough only one has the post_content foo. My attempt to use WP_Query failed miserably as well.
Any help is appreciated!
WP_Query method :
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
I am presuming that the reason why you failed with WP_Query is due to the following condition.
Codex states : The default WP_Query sets 'post_status'=>'publish', but attachments default to 'post_status'=>'inherit' so you'll need to explicitly set post_status to 'inherit' or 'any' as well.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
get_posts method :
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'meta_key' => 'custom-field',
'meta_value' => 'custom value',
'numberposts' => -1
);
print_r(get_posts($args));
The only draw back with this method is that the meta_value needs to exactly match what was entered in the custom field. If you still like to use get_posts then use the meta_query as shown in WP_Query example above.

ajaxlink shows blank # Yii

my ajaxlink shows # instead of the link. I tried using chtml::link, and that displayed it correctly.
<?php
echo CHtml::ajaxLink("<img alt=".$data->text."
src=".Yii::app()->assetManager->publish(Yii::app()->basePath."/images/".$data->img)."
title=".$data->text." height=100px width=100px/>",
Yii::app()->createUrl('controller/action',array('id'=>$data->id)),
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"));
?>
Look at the ajaxLink function
public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
{
if(!isset($htmlOptions['href']))
$htmlOptions['href']='#';
$ajaxOptions['url']=$url;
$htmlOptions['ajax']=$ajaxOptions;
self::clientChange('click',$htmlOptions);
return self::tag('a',$htmlOptions,$text);
}
As you see href comes from htmlOptions. The right code will be:
CHtml::ajaxLink("<img alt='".$data->text."'
src='".Yii::app()->assetManager->publish(Yii::app()->basePath."/images/".$data->img)."'
title='".$data->text."' height=100px width=100px/>",
'',
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"
),array(
'href'=>Yii::app()->createUrl('controller/action',array('id'=>$data->id))
));
Hope your other approach worked! But you could try repeating the link for both url and href like this.
echo CHtml::ajaxLink("<img alt='" . $data->text . "'
src='" . Yii::app()->assetManager->publish(Yii::app()->basePath . "/images/" . $data->img) . "'
title='" . $data->text . "' height=100px width=100px/>",
Yii::app()->createUrl('controller/action', array('id' => $data->id)),
array(
"type" => "post",
"data" => "js:{ids:$.fn.yiiGridView.getSelection('chck-id')}",
"update" => "#grid"
), array(
'href' => Yii::app()->createUrl('controller/action', array('id' => $data->id))
)
);

ZF2 form element description

in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here