Custom wordpress taxonomy with v2 api and vue - vue.js

I'm using the wordpress v2 api to display custom posttypes. Everything works as expected, only the custom taxonomy returns their ID instead of the name.
I've read that adding ?embed to the endpoint and show_in_rest adds posttypes and taxonomies to the api result and this sort of looks to be the case. But the taxonomy name isn't found in the result.
Am I missing something?
Here are some snippets of my code...
// taxonomy snippet
register_taxonomy('types',array('work'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
'show_in_rest' => true,
'rest_base' => 'work-api',
'rest_controller_class' => 'WP_REST_Terms_Controller'
// custom posttype snippet
$args = array(
'labels' => $labels,
'description' => 'All projects',
'public' => true,
'menu_icon' => 'dashicons-layout',
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true,
'show_in_rest' => true,
'rest_base' => 'work-api',
'rest_controller_class' => 'WP_REST_Posts_Controller'
);
// DOM snippet
<div id="workfeed" class="work" style="margin-top: 100px;">
<div v-for="(item,index) in work" class="row" :data-index="index">
<div class="col">
<img :src="item._embedded['wp:featuredmedia'][0].source_url" />
</div>
<div class="col">
<h3>{{ item.title.rendered }}</h3>
{{ item.content.rendered }}
{{ item._embedded['wp:term'][0].taxonomy }}
</div>
</div>
</div>

Related

Auto fill radio button in volt template without using model

I rendered elements of a form in volt file using below code. I want to auto fill the default radio button.
<div class="form-group" >
<div><label>Gender *:</label>
<label class="radio-inline popup-gender">{{ element }}{{ element.label()}}:</label>
</div>
If you're using PHP to prepare the form:
$radio = new Radio('my_radio', ['id' => null, 'value' => 'one']);
$radio->setDefault('one');
$form->add($radio);
$radio = new Radio('my_radio', ['id' => null, 'value' => 'two']);
$form->add($radio);
If you're using Volt:
{{ set_default('my_radio', 'one') }}
{{ radio_field('my_radio', ['id' => null, 'value' => 'one']) }}
{{ radio_field('my_radio', ['id' => null, 'value' => 'two']) }}

Add field in product Prestashop 1.7

Why is prestashop don't save my modification into database?
Using prestashop 1.7
/override/classes/Product.php
class Product extends ProductCore {
public $por_gan; public function __construct ($idProduct = null, $idLang = null, $idShop = null) {
$definition = self::$definition;
$definition['fields']['por_gan'] = array('type' => self::TYPE_INT, 'required' => false);
parent::__construct($idProduct, $idLang, $idShop); } }
In ProductInformation.php
->add('por_gan', 'Symfony\Component\Form\Extension\Core\Type\NumberType', array(
'required' => false,
'label' => $this->translator->trans('Beneficio', [], 'Admin.Catalog.Feature'),
'constraints' => array(
new Assert\NotBlank(),
new Assert\Type(array('type' => 'numeric'))
),
))
In form.html.twing
<div class="col-md-6">
<label class="form-control-label">% de beneficio</label
{{ form_widget(form.step1.por_gan) }}
</div>
Thanks
I’ve successfully added an extra tab in admin product page.
It's working fine. I think a better approach would be to create a module in order to make that modification easier to maintain.
Or you can use displayAdminProductsExtra hook, actionProductUpdate hook and actionProductAdd
The extra field is : frais_a_prevoir
I show all the files to modify but you have to check where the modification should be done inside the file (make a search and you will find)
Override /classes/Product.php
In class /classes/Product.php, there are 3 modifications to do :
1)
/** #var string Frais à prévoir */
public $frais_a_prevoir;
2)
'frais_a_prevoir' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
3)
$sql->select(
'p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`, pl.`frais_a_prevoir`, pl.`link_rewrite`, pl.`meta_description`,
pl.`meta_keywords`, pl.`meta_title`, pl.`name`, pl.`available_now`, pl.`available_later`, image_shop.`id_image` id_image, il.`legend`, m.`name` AS manufacturer_name,
(DATEDIFF(product_shop.`date_add`,
DATE_SUB(
"'.$now.'",
INTERVAL '.$nb_days_new_product.' DAY
)
) > 0) as new'
);
In /src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
<ul class="nav nav-tabs bordered">
<li id="tab_description_short" class="nav-item">{{ 'Summary'|trans({}, 'Admin.Catalog.Feature') }}</li>
<li id="tab_description" class="nav-item">{{ 'Description'|trans({}, 'Admin.Global') }}</li>
<li id="tab_frais_a_prevoir" class="nav-item">{{ 'frais_a_prevoir'|trans({}, 'Admin.Global') }}</li>
</ul>
<div class="tab-content bordered">
<div class="tab-pane panel panel-default active" id="description_short">
{{ form_widget(form.step1.description_short) }}
</div>
<div class="tab-pane panel panel-default " id="description">
{{ form_widget(form.step1.description) }}
</div>
<div class="tab-pane panel panel-default " id="frais_a_prevoir">
{{ form_widget(form.step1.frais_a_prevoir) }}
</div>
</div>
In /src/PrestaShopBundle/Form/Admin/Product/productInformation.php
->add('frais_a_prevoir', 'PrestaShopBundle\Form\Admin\Type\TranslateType', array(
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextareaType',
'options' => [
'attr' => array('class' => 'autoload_rte'),
'required' => false
],
'locales' => $this->locales,
'hideTabs' => true,
'label' => $this->translator->trans('frais_a_prevoir', [], 'Admin.Global'),
'required' => false
))
in src/PrestaShopBundle/Model/Product/AdminModelAdapter.php:
$this->translatableKeys = array(
'name',
'description',
'description_short',
'frais_a_prevoir',
'link_rewrite',
'meta_title',
'meta_description',
'available_now',
'available_later',
'tags',
);
//define unused key for manual binding
$this->unmapKeys = array('name',
'description',
'description_short',
'frais_a_prevoir',
'images',
'related_products',
'categories',
'suppliers',
'display_options',
'features',
'specific_price',
'virtual_product',
'attachment_product',
);
2)
'frais_a_prevoir' => $this->product->frais_a_prevoir,
In database, add a column frais_a_prevoir in table product_lang
Here is an option to do this using module and does not change core files
in your MyModule.php
use PrestaShopBundle\Form\Admin\Type\TranslateType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
public function hookDisplayAdminProductsExtra($params)
{
$productAdapter = $this->get('prestashop.adapter.data_provider.product');
$product = $productAdapter->getProduct($params['id_product']);
$formData = [
'ebay_reference' => $product->ebay_reference,
];
$formFactory = $this->get('form.factory');
$form = $formFactory->createBuilder(FormType::class, $formData)
->add('ebay_reference', TranslateType::class, array(
'required' => false,
'label' => 'Ebay reference',
'locales' => Language::getLanguages(),
'hideTabs' => true,
'required' => false
))
->getForm()
;
return $this->get('twig')->render(_PS_MODULE_DIR_.'MyModule/views/display-admin-products-extra.html.twig', [
'form' => $form->createView()
]) ;
}
public function hookActionAdminProductsControllerSaveBefore($params)
{
$productAdapter = $this->get('prestashop.adapter.data_provider.product');
$product = $productAdapter->getProduct($_REQUEST['form']['id_product']);
foreach(Language::getLanguages() as $language){
$product->ebay_reference[ $language['id_lang'] ] =
$_REQUEST['form']['ebay_reference'][$language['id_lang']];
}
$product->save();
}
in your display-admin-products-extra.html.twig
<div class="row" >
<div class="col-md-12">
<div class="form-group">
<h3>Ebay reference</h3>
{{ form_errors(form.ebay_reference) }}
{{ form_widget(form.ebay_reference) }}
</div>
</div>
</div>

Bootstrap3 Modal not working with GridView's pjax

I have a following GridView, but unfortunately Bootstrap modal isn't working properly if I have used Pjax pagination or search. Well, the modal does show up, but it won't submit the form. And when I leave the modal out of Pjax(), then it isn't loaded at all.
Pjax::begin(['id' => 'pjax_id', 'timeout' => false, 'enablePushState' => false]);
echo GridView::widget([
'dataProvider' => $details,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['format' => 'raw',
'label' => 'Category',
'value' => function ($data) {
$string = '';
foreach ($data['Category'] as $category) {
$string .= $category->category_name . '<br/>';
}
return $string;
}],
'book_name',
['format' => 'raw',
'contentOptions' => ['class' => 'form-cell'],
'label' => '',
'value' => function ($data) {
$buttons = Html::button('Edit', ['class' => 'btn btn-default mleft modallink', 'data-toggle' => 'modal', 'data-target' => '#'.$data->cat_id]);
return $buttons;
}
],
[
'format' => 'raw',
'label' => '',
'value' => function ($data) {
if (Yii::$app->user->identity->user_type == 'admin') {
$string = '</span>';
$string .= '<a class=\'mleft\' href=\''.Url::to(['category/update', 'id' => $data->cat_id]).'\'><span class="glyphicon glyphicon-pencil"></span></a>';
return $string;
}
}
],
],
]);
$details = $details->getModels();
foreach ($details as $detail) {
?>
<div class="modal fade" id="<?= $detail->cat_id ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?= $detail->category_name ?></h4>
</div>
<div class="modal-body">
<?php
echo Html::beginForm(Url::to(['category/details', 'id' => $detail->cat_id]), 'post', ['id' => 'popup']);
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'cat_description');
echo Html::activeTextarea($detail, 'cat_description', ['class' => 'form-control']);
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'link');
echo Html::activeInput('text', $detail, 'link', ['class' => 'form-control']);
echo Html::endTag('div');
echo Html::beginTag('div', ['class' => 'form-group']);
echo Html::activeLabel($detail, 'resource');
echo Html::activeInput('text', $detail, 'resource', ['class' => 'form-control']);
echo Html::endTag('div');
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<?= Html::submitButton('Submit', ['class' => 'btn btn-success']) ?>
<?= Html::endForm(); ?>
</div>
</div>
</div>
</div>
<?php
}
Pjax::end();
So how do I keep GridView's Pjax functionality so that the modal still works?
Pjax widget has a formSelector attribute that, by default, will find all forms with data-pjax attribute set to something not falsy.
The easiest way to fix your form is to add that attribute to your form definition:
echo Html::beginForm(
Url::to(['category/details', 'id' => $detail->cat_id]),
'post',
[
'id' => 'popup',
'data-pjax' => 1,
]
);

Laravel 4 password confirm doesn't seem to work how I expect it to

I am basing my code off of a video on Laracasts.com. Here is the video. My idea is that I could use that validation method to create a user. When I try to confirm the passwords, it doesn't seem to match no matter what. My initial thought was that it is hashing the password prior to validation which causes it to return false no matter what.
Here's all of my code thus far.
Users Model
protected $fillable = ['username' ,'email', 'password'];
public static $rules = [
'username' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed'
];
public function isValid()
{
$validation = Validator::make($this->attributes, static::$rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
UsersController#Store
public function store()
{
$input = Input::all();
if (! $this->user->fill($input)->isValid())
{
return Redirect::back()->withInput()->withErrors($this->user->errors);
}
//if the user input is valid then save it and assign associated role
$this->user->save();
$this->user->assignRole(Input::get('role'));
return Redirect::to('/user')->with('flash_message', 'User added to the database!');
}
Create User View - Form
{{ Form::open(['role' => 'form', 'route' => 'user.store']) }}
<div class='form-group'>
{{ Form::label('username', 'First Name') }}
{{ Form::text('username', null, ['placeholder' => 'First Name', 'class' => 'form-control']) }}
</div>
<div class='form-group'>
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['placeholder' => 'Last Name', 'class' => 'form-control']) }}
</div>
<div class='form-group'>
{{ Form::label('role', 'Role') }}
{{ Form::select('role', $roles, "member", ['class' => 'form-control']) }}
</div>
<div class='form-group'>
{{ Form::label('password', 'Password') }}
{{ Form::password('password', ['placeholder' => 'Password', 'class' => 'form-control']) }}
</div>
<div class='form-group'>
{{ Form::label('password_confirmation', 'Password Confirm') }}
{{ Form::password('password_confirmation', ['placeholder' => 'Password Confirm', 'class' => 'form-control']) }}
</div>
<div class='form-group'>
{{ Form::submit('Create User', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
What's happening is that your password_confirmation field is always empty, no matter what you enter.
This is because you are using $this->user->fill($input) within the store method. However, in your model you have
protected $fillable = ['username' ,'email', 'password'];
So, this will never fill the password_confirmation field.
If you change the fillable to the following, you should have no problems
protected $fillable = ['username' ,'email', 'password', 'password_confirmation'];

How to show timepicker based jqrelcopy yii?

How to show timepicker based yiibooster, with Jqrelcopy.
I've code like this :
<div class="control-group "><label class="control-label required">Jam Kerja</label>
<div class="controls">
<a id="copylink" href="#" rel=".copy">Tambah Jam Kerja / Shift</a>
<div class="row copy">
<?php echo CHtml::label('',''); ?>
<?php $this->widget('bootstrap.widgets.TbTimePicker',
array(
'name' => 'some_time',
'value' => '00:00',
'htmlOptions'=>array('width'=>'50px'),
'noAppend' => true, // mandatory
'options' => array(
'disableFocus' => true, // mandatory
'showMeridian' => false // irrelevant
),
)
);
?>
<?php
$this->widget('bootstrap.widgets.TbTimePicker',
array(
'name' => 'some_time',
'value' => '00:00',
'noAppend' => true, // mandatory
'options' => array(
'disableFocus' => true, // mandatory
'showMeridian' => false // irrelevant
)
)
);
?>
</div>
</div>
</div>
When I add new, Timepicker always on field 1. Can't on field changed.