drupal-javascript
Skill71 starsChanged 7 months agoMITView on GitHub
Drupal JavaScript behaviors, libraries.yml, drupalSettings, attach_library(), and custom client-side AJAX commands using Drupal.AjaxCommands.prototype.
In this repository35 other files it gives its agents.
- ddev-expertSkill
- docker-localSkill
- drupal-accessSkill
- drupal-cachingSkill
- drupal-composerSkill
- drupal-configSkill
- drupal-contrib-mgmtSkill
- drupal-debuggingSkill
- drupal-drushSkill
- drupal-dttSkill
- drupal-entity-apiSkill
- drupal-eventsSkill
- drupal-fieldsSkill
- drupal-form-ajaxSkill
- drupal-form-alterSkill
- drupal-form-apiSkill
- drupal-form-validationSkill
- drupal-hooksSkill
- drupal-htmxSkill
- drupal-kernelSkill
- drupal-menusSkill
- drupal-migrationsSkill
- drupal-paragraphsSkill
- drupal-pluginsSkill
- drupal-queriesSkill
- drupal-renderSkill
- drupal-routesSkill
- drupal-search-apiSkill
- drupal-securitySkill
- drupal-servicesSkill
- drupal-stateSkill
- drupal-taxonomySkill
- drupal-twigSkill
- drupal-unitSkill
- SKILL.mdSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 117
Use this pattern when creating a custom AJAX command with:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Drupal JavaScript
Defining a Library (*.libraries.yml)
# my_module.libraries.yml
my-behavior:
version: 1.0
js:
js/my-behavior.js: {}
css:
component:
css/my-style.css: {}
dependencies:
- core/drupal
- core/jquery
- core/onceDrupal Behavior Pattern
// js/my-behavior.js
(function (Drupal, once) {
'use strict';
Drupal.behaviors.myBehavior = {
attach(context, settings) {
// Use `once` to prevent double-processing
once('my-behavior', '.my-selector', context).forEach(function (element) {
// Process element
element.addEventListener('click', function (e) {
e.preventDefault();
// Handle click
});
});
},
detach(context, settings, trigger) {
// Clean up if needed (e.g., 'unload' trigger)
},
};
}(Drupal, once));Attaching a Library in Twig
Attach a JavaScript/CSS library from a Twig template:
{{ attach_library('my_module/my-behavior') }}Accessing drupalSettings in JS
Drupal.behaviors.myBehavior = {
attach(context, settings) {
const myKey = settings.myModule?.key;
if (myKey) {
console.log('Value:', myKey);
}
},
};AJAX Commands from PHP
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\HtmlCommand;
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#wrapper', $build));
$response->addCommand(new HtmlCommand('#message', $this->t('Done!')));
$response->addCommand(new InvokeCommand('.my-class', 'addClass', ['active']));
return $response;Custom AJAX Command
// Register command
Drupal.AjaxCommands.prototype.myCommand = function (ajax, response, status) {
document.querySelector(response.selector).textContent = response.data;
};// PHP side
use Drupal\Core\Ajax\CommandInterface;
final class MyCommand implements CommandInterface {
public function __construct(
private readonly string $selector,
private readonly string $data,
) {}
public function render(): array {
return [
'command' => 'myCommand',
'selector' => $this->selector,
'data' => $this->data,
];
}
}Custom Drupal AJAX Commands
Use this pattern when creating a custom AJAX command with:
Drupal.AjaxCommands.prototype- a matching PHP
CommandInterfaceclass - a custom command name returned in the AJAX response
This is for defining new AJAX command types, not general form AJAX callbacks.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

