rootcastleco/rei-skills / registry/external-reviewed/drupal-javascript
drupal-javascript
VerifiedDrupal JavaScript behaviors, libraries.yml, drupalSettings, attach_library(), and custom client-side AJAX commands using Drupal.AjaxCommands.prototype.
README.md
View on GitHub# drupal-javascript skill — Developer Notes
## When to use this skill
Use when working with:
- Defining JS libraries in `*.libraries.yml`
- Writing Drupal behavior patterns (`Drupal.behaviors.myBehavior`)
- Using `once()` to prevent double-initialization
- Passing PHP data to JavaScript via `drupalSettings`
- Attaching libraries to render arrays or Twig templates
- Creating custom AJAX commands (PHP + JS pair)
## Mental Model
| Concept | Details |
|---|---|
| **Library** | Defined in `*.libraries.yml`, attached via PHP or Twig |
| **Behavior** | `Drupal.behaviors.name = { attach(context, settings) {} }` |
| **`once()`** | Prevents double-processing on AJAX-loaded content |
| **drupalSettings** | `$build['#attached']['drupalSettings']['ns']['key']` |
| **AJAX commands** | PHP `AjaxResponse` + matching JS `Drupal.AjaxCommands.prototype` |
## Example Prompts
- Create a Drupal behavior that initializes a slider
- Pass a PHP value to JavaScript via drupalSettings
- Load a JavaScript library in Twig
- Register a custom AJAX command in JavaScript and return it from PHP
## Library Conditions (conditional loading)
```yaml
# Load only if a module is installed
my-conditional:
js:
js/conditional.js: {}
dependencies:
- core/drupal
```
```php
// Load conditionally in PHP
if ($this->moduleHandler->moduleExists('views')) {
$build['#attached']['library'][] = 'my_module/views-integration';
}
```
## Sources
- [JavaScript API](https://www.drupal.org/docs/develop/standards/javascript)
- [Libraries API](https://www.drupal.org/docs/develop/creating-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-module)
- [AJAX API](https://www.drupal.org/docs/drupal-apis/ajax-api)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |