addJS function not working for admin in prestashop - prestashop

I am trying to add javascript file in prestashop admin using backOfficeHeader hook using a module but nothing happened. My code is given below.
public function install()
{
if (!parent::install()
|| !$this->registerHook('backOfficeHeader'))
return false;
return parent::install() &&
$this->registerHook('backOfficeHeader');
}
public function hookBackOfficeHeader() {
$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'js/hs_custom.js');
}

If you are using PS 1.5 or 1.6 you should use hook "actionAdminControllerSetMedia".
Your module installer should check which prestashop version is used and then register the needed hook.
if (version_compare(substr(_PS_VERSION_, 0, 3), '1.5', '<'))
$this->registerHook('BackOfficeHeader');
else
$this->registerHook('actionAdminControllerSetMedia');
Then you need to addJS on each hook in its version format:
PS>=1.5
public function hookActionAdminControllerSetMedia($params) {
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
}
PS<=1.4
public function hookBackOfficeHeader($params) {
Tools::addJS($this->_path.'views/js/hs_custom.js');
}

did u try to check addJS path? I think nothing more can be possible if other JS files working.
Try to use $this->_path.
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
1) Output path and check if it is valid.
2) Reload page and check network. Page load your script or not?
3) Remember to reset module if u change something with hooks.
4) Check module hooks.

You did several mistakes.
This is the invalid access to the property: $this->module->name. Must be $this->name. I.e., the correct code to generate a path to JavaScript file is:
_MODULE_DIR_ . $this->name . '/js/hs_custom.js'
Or like this (shorted):
$this->_path . 'js/hs_custom.js'
You are also did the double installation of the module and of the hook.
You can use the hook BackOfficeHeader, but the hook ActionAdminControllerSetMedia is preferred.
So, the correct example to add a JS and a CSS files for a back-office (i.e. for AdminController) 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).

I also met this problem, there is no error and warning, all grammar is right. But cannot find my js File.
I found the reason finally. In my case there is nothing in JS file and system passes this file which has no content always.

For me "this->_path" dosn't work. My solution is to use $_SERVER['DOCUMENT_ROOT']
public function hookActionAdminControllerSetMedia($params)
{
// add necessary javascript to products back office
if($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product'))
{
$this->context->controller->addJS($_SERVER['DOCUMENT_ROOT']."/modules/apl/views/js/jquery.ui.touch-punch.min.js");
}
}

Related

Problem of custom module translation prestashop 1.7.8.1

I have built my own module and in the BO when I search my module and trying to translate it I am redirected to
"shopadmin/index.php/improve/international/translations/?lang=fr&type=modules&locale=fr-FR&selected=Module_Name&_token=OeEwKt4lWTUTcBerD-_w7LXvoA1Dyl3lcu3cBrYtkaA"
instead of
"shopadmin/index.php?controller=AdminTranslations&type=modules&module=ag_jsccustom&lang=fr&token=a74aaad65ca627b7ca6bfdc37235594f"
Anyone can help me with this weird issue?
I found the solution. You have to add this function to your module’s main class:
public function isUsingNewTranslationSystem()
{
return false;
}

Cant access helper methods inside mongoose schema

My directory looks like bellow
--controllers
-helper.js
--models
-userModel.js
--server.js
My helper module is like
module.exports = {
check: function() {
return 'check';
}
}
I want to access helper module inside userModel.js. So I put like
var helper = require('.././controllers/helper');
Then I do console.log(helper.check()); but it shows error helper.check is not a function Or if I do console.log(helper); only it returns {}. How to access the helper module inside models? Thank you.
Since you said it returns {}, can you please check in your helper module that you have imported userModel.js. Because it forms circular dependencies and sometimes result empty json.

Prestashop beforeRequest Middleware

I am trying to build a module for Prestashop 1.6 that would redirect the user if the targeted URL is present in a database.
What I'm going to do is the following:
public function checkRedirection ($url) {
$line = Db::getInstance()->executeS('SELECT * FROM ps_custom_redirection WHERE url = ' . pSQL($url));
if (!sizeof($line)) {
return null;
}
header('Location: ' . $line[0]['destination']);
http_response_code($line[0]['http_code']);
exit();
}
Now, I could run this function when the displayTop hook is fired. But I would rather launch this function at the beginning of the request's process.
Does Prestashop provide such a hook? If not, can I create one? Where should I write the code to fire it?
The fist hook executed is actionDispatcher – you can use it if you want.
You'll find this hook executed in /classes/Dispatcher.php. Search for the code Hook::exec('actionDispatcher', $params_hook_action_dispatcher);.
If you want to add this hook to your module, you need to use its name in the main module file like this:
public function install() {
return parent::install()
&& $this->registerHook('actionDispatcher');
}
public function hookActionDispatcher($params) {
// your code
Tools::redirect($url);
}
In Prestashop Tools::redirect($url); is used if redirecting.

What is module's "themes" folder used for?

PS documentation says that when developing a module you can create the /themes/[theme_name]/modules subfolder and that it is used for:
"Sub-folder for overriding .tpl files and languages files, if necessary."
and that it:
"enables you to handle the module's template files in various ways, depending on the current theme.",
but i don't really understand its practical usage. What would be a use case of it?
Thanks
When you develop a Prestashop website you should never change core files. This mean that you can only create new modules in the /modules/ folder but not alter existing one. Because if you update a module you altered, all your changes will be gone.
Each time Prestashop needs to load a module template file it will first look in the current theme /themes/your_theme/modules/the_module/path_to_tpl.tpl if an override of this template exists. If not it will load the template from the /modules directory.
This recommandation is also valid for .css and .js files.
The documentation you mentioned in the comment below is wrong and should be updated. You can't put a themes folder inside a module.
Here is the _isTemplateOverloadedStatic() method from Module class called everytime we need a module template:
/*
** Template management (display, overload, cache)
*/
protected static function _isTemplateOverloadedStatic($module_name, $template)
{
if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/'.$template;
} elseif (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/hook/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/hook/'.$template;
} elseif (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/front/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/front/'.$template;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/views/templates/hook/'.$template)) {
return false;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/views/templates/front/'.$template)) {
return false;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/'.$template)) {
return false;
}
return null;
}
As you can see in this code, Prestashop will never look into a themes folder inside your module when loading a template.

How to check if a YII app is running from a console or from browser?

I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?
Thanks!
This reply is a bit late but there is a Yii-specific way to do this:
In Yii1 you can do:
if (Yii::app() instanceof CConsoleApplication)
In Yii2 that would be:
if (Yii::$app instanceof Yii\console\Application)
Hope that's useful to someone...
You should also be able to do:
echo get_class(Yii::app());
which will tell you what type of app you're in ...
Same way you would determine if a PHP application is being run in the console or not.
What is the canonical way to determine commandline vs. http execution of a PHP script?
check Yii::$app->id
when running from console Yii::$app->id = 'app-console'
when running from frontend (browser) Yii::$app->id = 'app-frontend'
The most efficient way seems to define in the root file index.php this line :
define ('WEBAPP', true)
Later you can check in any point the application
if (defined('WEBAPP')) {
echo "This is webapp";
} else {
echo "app was launched via console";
}
Checked in Yii 1.7
You can use
if(is_a(Yii::$app,'yii\console\Application'))
for console, and
if(is_a(Yii::$app,'yii\web\Application'))
for web.
https://stackoverflow.com/a/30635800/4916039
I am using Yii 1 and I use this function to check
public static function isWebRequest()
{
return Yii::app() instanceof CWebApplication;
}
public static function isConsoleRequest()
{
return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
}
I put these functions in a helper(Componenet) Class and I use it as:
if(MyRandomHelper::isConsoleRequest())
{
Email::shoot();
}