ptmr.io Posted August 8, 2017 Share Posted August 8, 2017 (edited) Hi, I want to register a Javascript file within an module by using registerJavascript(). While using it with inline = false, everything works fine. However I want it to be inlined, so I use inline = true and the debug tool throws an error: Are inline .js files handled different? What's my error? Warning: file_get_contents(/modules/xxx/js/cookieconsent.inline.js): failed to open stream: No such file or directory public function hookActionFrontControllerSetMedia( $params ) { ... $this->context->controller->registerJavascript( 'cookieconsentinline', 'modules/' . $this->name . '/js/cookieconsent.inline.js', [ 'position' => 'bottom', 'inline' => true, 'priority' => 100, ] ); ... } Edited August 8, 2017 by Yuest (see edit history) Link to comment Share on other sites More sharing options...
bellini13 Posted August 8, 2017 Share Posted August 8, 2017 Is it this? /modules/xxx/js/cookieconsent.inline.js Or is it this? /modules/xxx/views/js/cookieconsent.inline.js Link to comment Share on other sites More sharing options...
ptmr.io Posted August 8, 2017 Author Share Posted August 8, 2017 /modules/xxx/js/cookieconsent.inline.js The file location itself is correct, as it loads the file with inline => false correctly. Is thr views Subfolder required? Link to comment Share on other sites More sharing options...
bellini13 Posted August 8, 2017 Share Posted August 8, 2017 if you are going to add the module to the PS addons store, they will require that you place your media assets in specific folders. However it is not required for the functionality to work properly. The warning is pretty clear though, the standard php function 'file_get_contents' is not able to locate that file. I'm not sure if that is a Prestashop issue at all Warning: file_get_contents(/modules/xxx/js/cookieconsent.inline.js): failed to open stream: No such file or directory Perhaps create a simple php script that attempts to open the javascript file using the same approach as PS, and see what happens. Link to comment Share on other sites More sharing options...
ptmr.io Posted August 9, 2017 Author Share Posted August 9, 2017 (edited) Thanks, however: calling file_get_contents works when called directly. The file definitly exists and will be included when inline => false Provinding the full path does not show an error, however it also doesn't show the javascript code at all in the source code with inline => true: _PS_MODULE_DIR_ . $this->name . '/js/cookieconsent.inline.js' So, what could be the problem? Edited August 9, 2017 by Yuest (see edit history) Link to comment Share on other sites More sharing options...
ptmr.io Posted August 9, 2017 Author Share Posted August 9, 2017 After debugging a while, I found that $this->getPathFromUri($fullPath) in AbstractAssetManager.php returns false if one uses an absolute path (like _PS_MODULE_DIR_) and therefore no errors are displayed, the line is simply ignored. However using the correct relative path, file_exists() returns true, but in JavascriptManager.php the corresponding file_get_contents returns false which causes the error. on can fix this by changing file_get_contents($item['path']); to file_get_contents(_PS_ROOT_DIR_ . $item['path']); in JavascriptManager.php I guess it's a bug. Link to comment Share on other sites More sharing options...
bellini13 Posted August 9, 2017 Share Posted August 9, 2017 I would suggest either reporting the issue to PS on their forge so they can research and fix, or committing the change to their github so the change can be merged into a future release. Link to comment Share on other sites More sharing options...
ptmr.io Posted August 9, 2017 Author Share Posted August 9, 2017 already posted: http://forge.prestashop.com/browse/BOOM-3625 Link to comment Share on other sites More sharing options...
Diakro Posted May 16, 2020 Share Posted May 16, 2020 On 8/9/2017 at 10:44 AM, ptmr.io said: on can fix this by changing file_get_contents($item['path']); to file_get_contents(_PS_ROOT_DIR_ . $item['path']); in JavascriptManager.php I guess it's a bug. this is an old post, but the problem is still present. I think is a mistake. $Item [ 'path'] instead of $item['uri'] would be the right Link to comment Share on other sites More sharing options...
Sasni Posted June 26, 2022 Share Posted June 26, 2022 try to use 'server' => 'remote' like this public function hookActionFrontControllerSetMedia($params) { $this->context->controller->registerJavascript( 'mymodule-javascript', $this->_path.'views/js/mymodule.js', [ 'position' => 'bottom', 'inline' => true, 'priority' => 1000, 'server' => 'remote' ] ); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now