Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 132 Vote(s) - 3.75 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to load js and css files in prestashop 1.7 admin module

#1
I'm learning to write modules to prestashop 1.7, currently I'm trying to load css and js files that will be used when the user tried to configure the module.

this is the code of my module:

class TuxInModComments extends Module
{

function __construct()
{
$this->name = 'tuxinmodcomments';
$this->tab = 'quick_bulk_update';
$this->version = '0.1';
$this->author = 'Kfir Ozer';
$this->displayName = 'Tux-In Comments and Ranks';
$this->description = 'With this module, your costumers will be able to grade and comment your products';
$this->bootstrap = true;

parent::__construct();
}

public function install() {
parent::install();
$this->registerHook('actionAdminControllerSetMedia');
return true;
}

public function processConfiguration()
{
if (Tools::isSubmit('mymod_pc_form')) {
$enable_grades = Tools::getValue('enable_grades');
$enable_comements = Tools::getValue('enable_comments');
$csvFile = Tools::getValue('csv_file');
die(var_export($csvFile));
Configuration::updateValue('MYMOD_GRADES', $enable_grades);
Configuration::updateValue('MYMOD_COMMENTS', $enable_comements);
$this->context->smarty->assign('confirmation', 'ok');
}
}

public function assignConfiguration()
{
$enable_grades = Configuration::get('MYMOD_GRADES');
$enable_comments = Configuration::get('MYMOD_COMMENTS');
$this->context->smarty->assign('enable_grades', $enable_grades);
$this->context->smarty->assign('enable_comments', $enable_comments);
}

public function hookActionAdminControllerSetMedia($params){
$this->registerStylesheet('module-tuxinmodcomments-css','modules/tuxinmodcomments/js/getcontent.css');
$this->registerJavascript('module-tuxinmodcomments-js','modules/tuxinmodcomments/js/getcontent.js');
}


public function getContent() {
$this->processConfiguration();
$this->assignConfiguration();
return $this->display(__FILE__,'getContent.tpl');
}

}

so I registered the admin set Media hook with the name `actionAdminControllerSetMedia` but it seems that it doesn't have the functions to set stylesheet and javascript cause I get the same error for both: `Uncaught Symfony\Component\Debug\Exception\UndefinedMethodException: Attempted to call an undefined method named "registerStylesheet" OR "registerJavascript" of class "AdminModulesController"`.

I'm really new to this.. I read that I need to set it in the front controller.. but doesn't that mean that it will appear in the regular page and not in the configuration page?

don't know how to resolve this and a bit confused, so any information regarding the issue would be greatly appreciated.

Reply

#2
To load CSS or JS you have tu use this hook, with this snippet:

public function hookDisplayBackOfficeHeader()
{
$this->context->controller->addCSS($this->_path.'pathtocss/module.css', 'all');
$this->context->controller->addJS($this->_path.'pathtojs/module.js', 'all');
}

Enjoy :)

PS: You have to register the display backoffice header hook first
Reply

#3
**As you need to register assets for a back-office, i.e. for `AdminController`, then you need to use `addJS` and `addCSS` methods.** So, the correct example to add a JS and a CSS files via a module class is:
```
public function hookActionAdminControllerSetMedia($params)
{
// Adds your's CSS file from a module's directory
$this->context->controller->addCSS($this->_path . 'views/css/example.css');

// Adds your's JavaScript file from a module's directory
$this->context->controller->addJS($this->_path . 'views/js/example.js');
}
```
Here is the detailed information, [how to register JavaScript in a back-office (in admin pages).][1]

**If you would need to register assets for a front-office (i.e. `FrontController`) in PrestaShop 1.7 then you need to use `registerJavascript` and `registerStylesheet` methods:**
```
public function hookHeader($params)
{
$this->context->controller->registerJavascript(
'module-tuxinmodcomments',
'modules/' . $this->name . '/views/js/getcontent.js'
);

$this->context->controller->registerStylesheet(
'module-tuxinmodcomments',
'modules/' . $this->name . '/views/css/getcontent.css'
);
}
```


[1]:

[To see links please register here]

Reply

#4
Add CSS and JS files to hookHeader:

public function hookHeader()
{
$this->context->controller->addCSS($this->_path . 'views/css/styles.css');
$this->context->controller->addJS($this->_path . 'views/js/script.js');
}

Register hookHeader:

public function install()
{
return parent::install()
&& $this->registerHook('header');
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through