Jump to content

Passing Javascript in admin page


Recommended Posts

I am trying to use $config->jsConfig(). A very basic module I crafted does that after a page is saved:

$this->wire('config')->jsConfig('mySettings', [
  'foo' => 'bar',
  'bar' => 123,

… and the same module has a JS file that tries to get those data back:
(right now I am sticking to the doc’s exemple as you can see)

var mySettings = ProcessWire.config.mySettings;

But that last JS returns “mySettings is undefined”. That is where I need some help.

More generally, Is this the best way to deal with php <—> JS data interchange, considering my goal is to fill a field with js generated content with a URL from an upload field as parameter:

Upload field in page edit -> fileURL -> javascript doStuff(fileURL) -> fill a PW field with the result of javascript doStuff(fileURL)

Link to comment
Share on other sites

I think both works, and my issue is more about retrieving this value once the pages is saved and reloaded:

$this->wire('log')->save('vtf_custom', $this->wire("config")->js('mySettings')['foo']);

returns “bar” as expected, but there is still no trace of “mySettings” in the js object ProcessWire.config when I log it.


Link to comment
Share on other sites

One possible solution:

$wire->addHookBefore("Inputfield(name=title)::render", function ($event) {
  $f = $event->object;
  $id = $this->wire->input->get('id', 'int');
  $f->appendMarkup = "<script>alert('You are editing page #$id')</script>";


  • Like 1
Link to comment
Share on other sites

Let me ask another question: could it be possible to log in JavaScript, the path of a file that just has been Ajax uploaded without saving the page?
I tried that with no result:

let uploadField = document.querySelector('#Inputfield_webfont_archive')
uploadField.addEventListener("AjaxUploadDone", (e) => console.log(e));


Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...