Class override doesn't work anymore in Prestashop 1.7.6? - prestashop

I simply need to add a custom field named 'color' in Category admin. I've always used this method in previous version but it seems that's not working anymore in 1.7.6 :
override/classes/Category.php
/**
* Class CategoryCore
*/
class Category extends CategoryCore
{
public $color;
public function __construct($id_category = null, $id_lang = null, $id_shop = null){
self::$definition['fields']['color'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml');
parent::__construct($id_category, $id_lang, $id_shop);
}
}
override/controllers/admin/AdminCategoriesController.php
class AdminCategoriesController extends AdminCategoriesControllerCore
{
public function renderForm()
{
$this->fields_form_override =array(
array(
'type' => 'text',
'label' => $this->trans('Color', array(), 'Admin.Global'),
'name' => 'color',
'maxlength' => 70,
'maxchar' => 70,
'lang' => true,
'rows' => 5,
'cols' => 100,
'hint' => $this->trans('Forbidden characters:', array(), 'Admin.Notifications.Info').' <>;=#{}'
),
);
return parent::renderForm();
}
}
Finally, I added a text field in the Database (ps_category_lang) and I deleted all the cache.
The field doesn't appear. Someone has this problem? Do I need to use a module instead of this method?
Thanks !

It seems that this process is not longer supported in 1.7.6 version. We also need to create a module with the new Symfony model.
There is a french source that explain how to. Here is the link.
I created a new post with my own code and the new procedure. I'm stucked in the save of the field in database. Here is the link.

Same here, i don't know why its not working. I follow all the instruction.
Im using Prestashop 1.7.6, maybe this procedure is not working with the latest prestashop version. Because based on what i read, they use prestashop v1.6 older.

You don't need a module.
I tried your code and it work for me
Try to clear cache in the backoffice interface and where are you put each file ?

Related

Drupal9: Saving form as node in SQL (custom module example)

i develop a custom module with forms to save data in SQL-Datebase. I want to use for that the node-structure.
Normal SQL-savings for example table works but not for the node-tables.
Any idea what is going wrong?
This ist my Code for saving, which works in non-node-tables:
public function submitForm(array &$form, FormStateInterface $form_state) { $connection = \Drupal::service('database');
$result = $connection->insert('node.node__body')
->fields(['body_value'])
->values([
'body_value' => 'text for body',
])
->execute();
$form_state->setRedirect('modulname.form');
}
Use Entity API in Drupal to manipulate or create a node.
In your case,
$node = \Drupal::entityTypeManager()->getStorage('node')->create(
[
'type' => 'page',
'title' => 'New Basic Page',
'body' => 'text for body'
]
);
Here, type is the content type machine name. Don't forget to update with your own. Also you probably want to inject the entity_type.manager service and use in the code.
Get more info here: Working with entities in Drupal

Prestashop adding css issue

I try to add external css but is not work property,
I am use prestashop version 1.7.4.3
in install() function I call the hooks
&& $this->registerHook('displayHeader')
&& $this->registerHook('backOfficeHeader')
inside the hook, i registering the css and js files
public function hookDisplayHeader($params)
{
$this->context->controller->addCSS(($this->_path) .'views/css/style.css');
$this->context->controller->addJS(($this->_path) .'views/js/script.js');
$this->context->controller->addCSS('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
}
public function hookBackOfficeHeader(){
$this->context->controller->addCSS('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
$this->context->controller->addCSS(($this->_path) .'views/css/module.css');
$this->context->controller->addJqueryUI('ui.sortable');
}
in hookBackOfficeHeader() I don't have any problem, but in hookDisplayHeader() doesn't want to register font awesome.
I try to use registerStylesheet() instead to addCss() but is not working at all.
Why this different between the two functions? it is, maybe because hookDisplayHeader doesn't accept external file?
Thank you
edit:
I solved with registerStylesheet() and registerJavascript()
public function hookDisplayHeader($params)
{
$this->context->controller->registerStylesheet(
'sidemenu',
($this->_path) .'views/css/style.css',
['server' => 'remote', 'position' => 'head', 'priority' => 150]
);
$this->context->controller->registerJavascript(
'sidemenu-js',
($this->_path) .'views/js/script.js',
['server' => 'remote', 'position' => 'head', 'priority' => 120]
);
$this->context->controller->registerStylesheet(
'remote-font-awesom',
'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css',
['server' => 'remote', 'position' => 'head', 'priority' => 20]
);
}
Also for adding font-awesome you better to use requireAssets(array('font-awesome')) something like this
$this->context->controller->requireAssets(array('font-awesome'));
Backward compatibility is kept for the addJS(), addCSS(), addJqueryUI() and addJqueryPlugin() methods. Incidentally, now is the best time to update your libraries and use the new method.
When developing a PrestaShop module, you may want to add specific styles for your templates. The best way is to use the registerStylesheet and registerJavascript methods provided by the parent FrontController class.
Look link : https://devdocs.prestashop.com/1.7/themes/getting-started/asset-management/
Regards

Form builder for a related entity Symfony 3

Well let's say I have 2 entities:
category
product
I want to create a form builder for the entity product in which there is a select to choose the category of the product.
How can I do that with Symfony 3.4?
Can you guys give me an example?
You want to use the EntityType.
See below for an example:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
// ...
->add('category', EntityType::class, [
'class' => 'AppBundle:Category',
'multiple' => false,
'expanded' => false,
'choice_label' => 'title',
'query_builder' => function (EntityRepository $er) {
// use query builder for ordering/filtering choices
return $er->createQueryBuilder('cat')
->orderBy('cat.title', 'ASC');
},
])
I assume your category entity has a property called title. This would be in your Product form type class.

Add column from another table to CGridView

Hi I'm using yii crud and trying to add a column from another table to Admin view
This is my admin view CGridView widget code.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'package-days-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'package_days_id',
'package_days_description',
array(
'header' => 'Package Title',
'name' => 'package_days_package_id',
'value' => function ($data){
echo $data->packagePackagedays->package_title;
}
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
And this is relations function in 'PackageDays' model.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'packagePackagedays' => array(self::BELONGS_TO, 'Packages', 'package_days_package_id'),
);
}
This is search function in 'PackageDays' model.
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = "packagePackagedays";
$criteria->compare('package_days_id',$this->package_days_id);
$criteria->compare('packagePackagedays.package_title',$this->package_days_package_id);
$criteria->compare('package_days_description',$this->package_days_description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I added the column successfully but I can't search values of newly
added column.
It would be great if someone can looking to it
Make sure you added package_days_package_id as a public property of your Packages model. Otherwise $this->package_days_package_id doesn't exist
class Packages extends CActiveRecord{
public $package_days_package_id;
...
Also Make sure you added package_days_package_id in your "safe" validation rule for the "search" scenario (also in your Packages model). Without this, the value you type in the text box won't be assigned to your $this->package_days_package_id
public function rules(){
return array(
...
// The following rule is used by search()
array('bunch, of, stuff, ..., package_days_package_id', 'safe', 'on'=>'search'),
If you also want the grid column to be sortable on click, you'll also have to create a custom CSort and provide it to your CActiveDataProvider

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.