1

I want entries in a specific PlainText field for a named group to be pre-filled with random content. I am aware of the https://github.com/lukeyouell/craft-randomkeygenerator plugin and am aware that I could create my own field type, but I am intending to use jQuery to do this instead (which should be easier).

This Can a plugin add JS or CSS to the control panel? describes how to do this in Craft 2 (I assume that the code works). My equivalent in Craft3 is...

class MyPlugin extends Plugin {
    if (Craft::$app->request->getIsCpRequest())
    {
      Craft::$app->templates->includeJs('alert("xxx")');
    }
}

but $app->templates does not work. So is there a Craft3 equivalent of this line?

NB: I have already written a plugin that is adding a file volume (for encrypting files uploaded as Assets) so intend to add to that.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143

2 Answers2

3

You should be able to do this with Asset Bundles. Full documentation on this is here.

Oli
  • 7,495
  • 9
  • 17
1

Thanks for those. I found out for the specific need I had that I could take the existing Craft3 Plaintext field file, duplicate it (in my plugin folder), change the 'value' line of the getInputHtml function and then add a EVENT_REGISTER_FIELD_TYPES event to the main Plugin file.

However, there are other situations where I may need to add Javascript so I have reviewed the Asset Bundle page, but I will not be able to check it in detail until a specific job requires its use.

I hope this 'answer' matches the guidelines as it (strictly) does not answer my original question (only its need).