<p>You should be able to do this with <a href="https://docs.craftcms.com/v3/extend/asset-bundles.html" rel="nofollow noreferrer">Asset Bundles</a>. Full documentation on this is <a href="https://docs.craftcms.com/v3/extend/asset-bundles.html" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2019-01-30T12:22:06.027Z
2019-01-30T12:22:06.027Z
Oli
<p>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.</p>

<p>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.</p>

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

CC BY-SA 4.0
2019-02-01T10:22:52.343Z
2019-02-01T10:22:52.343Z
Martin Twigg
<p>I want entries in a specific PlainText field for a named group to be pre-filled with random content. I am aware of the <a href="https://github.com/lukeyouell/craft-randomkeygenerator" rel="nofollow noreferrer">https://github.com/lukeyouell/craft-randomkeygenerator</a> 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).</p>

<p>This <a href="https://craftcms.stackexchange.com/questions/267/can-a%20-plugin-add-js-or-css-to-the-control-panel">Can a plugin add JS or CSS to the control panel?</a> describes how to do this in Craft 2 (I assume that the code works). My equivalent in Craft3 is... </p>

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

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

<p><em>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.</em></p>

CC BY-SA 4.0
2019-01-30T20:37:03.287Z
Olivier's answer is correct. You may also want to look into the [Preparse](https://plugins.craftcms.com/preparse-field) and/or [Incognito](https://plugins.craftcms.com/incognito-field) plugins.
Lindsey D
<plugins><control-panel><javascript>
Can a plugin add JS or CSS to the control panel in Craft 3?
# Can a plugin add JS or CSS to the control panel in Craft 3?

(Asked by: username_0 on 2019-01-30)

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.

**Tags:** plugins, control-panel, javascript
> **Comments:**
> **username_1**: Olivier's answer is correct. You may also want to look into the [Preparse](https://plugins.craftcms.com/preparse-field) and/or [Incognito](https://plugins.craftcms.com/incognito-field) plugins.  

---

## 2 answers

### Answer by username_2 on 2019-01-30. Score: 3 | Accepted answer

You should be able to do this with [Asset Bundles](https://docs.craftcms.com/v3/extend/asset-bundles.html). Full documentation on this is [here](https://docs.craftcms.com/v3/extend/asset-bundles.html).


---

### Answer by username_0 on 2019-02-01. Score: 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).


---

