I just want to add Profile in navbar for logged user.
And I found /user/profile/show in dektrium yii2-user docs.
But it said:
Displays user's profile (requires id query param)
How to implement /user/profile/show in label that requires id query param?
Thanks in advance
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/user/registration/register']];
$menuItems[] = ['label' => 'Login', 'url' => ['/user/security/login']];
} else {
$menuItems[] = ['label' => 'Profile', 'url' => ['/user/profile/show']];
$menuItems[] = '<li>'
. Html::beginForm(['/user/security/logout'], 'post')
. Html::submitButton(
'Logout (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link']
)
. Html::endForm()
. '</li>';
}
When I use code above and click the Profile navbar, it said Bad Request (#400), Missing required parameters: id. Yea I know becuse of the id query param isn't defined.
You can add id to params like so:
$menuItems[] = ['label' => 'Profile', 'url' => ['/user/profile/show', 'id' => $id]];
url parameter is processed with Url::to() method in case of array, so you can pass additional parameters as key-value pairs.
Related
I want to use widget NumberControl in kartik's Form Bulder. But when I try this code, i have an empty form and there no errors. Couse I can't understand where is my mistake.
Please tell me if someone use a NumberControl in kartik's Form Builder.
use kartik\builder\Form;
echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 6,
'columnSize' => 'md',
'attributes' => [
'oi_count' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\number\NumberControl',
],
]
]);
According to this:https://demosbs3.krajee.com/number by default the type is "hidden", you should set it to "text".
please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.
You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.
Prepare the array to copying the values to the keys using array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?
I try to test my code with Codeception and my tests (both functional and acceptance) fails when I test logout option. They fails with "Method is not allowed (405)" because they are sent by GET method.
I have this code out of the box within the Nav widget:
$menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity['username'] . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
This code works fine when I click the link by hand. In this case they are sent by POST method.
But it doesn't work while testing.
Why does it happens?
for logout you can add the form as follows, which will send the request using POST
$menuItems[] = '<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
'Logout ('.Yii::$app->user->identity['username'].')', ['class' => 'btn btn-link btnlogout']
)
. Html::endForm()
. '</li>';
Here is the sample code from Yii2 checkboxList, I want to add custom class for each Item in checkboxList but I don't know how and where can I add that!
Could you please help me please ..
$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];
echo Html::checkboxList('CuisineId',$list2,$list,array('class' => 'test' ));
Thanks in advance.
If you want to add the same class, you should use itemOptions :
echo Html::checkboxList('CuisineId', $list2, $list, ['itemOptions'=>['class' => 'test']]);
Or if you want a custom class for each item, you should use item callback :
echo Html::checkboxList('CuisineId', $list2, $list, ['item'=>function ($index, $label, $name, $checked, $value){
return Html::checkbox($name, $checked, [
'value' => $value,
'label' => $label,
'class' => 'any class',
]);
}]);
Read more : http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#checkboxList()-detail
EDIT : add example
Just in case you only need to change the label options:
<?= Html::checkboxList('CuisineId', $list, $items, [
'itemOptions' => [
'labelOptions' => [
'style' => 'font-weight: normal',
'class' => 'some-custom-class',
],
],
]) ?>
Note: Everything you put inside itemOptions will be passed to Html::checkbox() as its own options when creating each checkbox. It means you can pass class, style, label, labelOptions, etc.
As you can use a index value inside the view when using a listview widget I wanted to change this index value as I use the same view on different places, so not every element that I use have the same index on the pages.
somepage.php
echo ListView::widget([
'summary' => false,
'viewParams' => [
'url' => '',
],
'index' => 'index + 4',//the idea
'options' => [
'class' => 'xxxx',
],
'itemOptions'=> [
'class' => 'xxxx',
],
'itemView' => '#frontend/views/product/_item_product_small',
'dataProvider' => $provider,
]);
frontend/views/product/_item_product_small.php
<div data-index="<?=$index?>">
// content
</div>
You need to create intermediate view for such cases:
somepage.php
echo ListView::widget([
// ...
'itemView' => '_view',
]);
_view.php
$this->render('#frontend/views/product/_item_product_small', ['index' => $index + 4]);