Jump to content

Register Javascript 1.7 inline


Recommended Posts

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 by Yuest (see edit history)

Share this post


Link to post
Share on other sites

/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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Thanks, however:

  1. calling file_get_contents works when called directly. The file definitly exists and will be included when inline => false
  2. 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 by Yuest (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
  • 2 years later...
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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More