Drupal Behaviors
apps/docs/src/content/docs/drupal-integration/behaviors Click to copy Copied!
apps/docs/src/content/docs/drupal-integration/behaviors Drupal Behaviors provide lifecycle hooks for initializing JavaScript on page load, AJAX responses, and BigPipe streaming. WC-2026 components integrate cleanly with this system.
Basic Behavior
Section titled “Basic Behavior”(function (Drupal) { 'use strict';
Drupal.behaviors.wcComponents = { attach(context) { // Initialize any WC-2026 components in the context const cards = context.querySelectorAll('wc-card:not([data-initialized])'); cards.forEach((card) => { card.setAttribute('data-initialized', 'true'); // Additional initialization if needed }); },
detach(context, settings, trigger) { if (trigger === 'unload') { // Cleanup when elements are removed const cards = context.querySelectorAll('wc-card[data-initialized]'); cards.forEach((card) => { card.removeAttribute('data-initialized'); }); } }, };})(Drupal);Event Handling
Section titled “Event Handling”Drupal.behaviors.wcEvents = { attach(context) { // Listen for WC-2026 custom events context.querySelectorAll('wc-accordion').forEach((accordion) => { accordion.addEventListener('wc-toggle', (e) => { // Track analytics if (typeof gtag !== 'undefined') { gtag('event', 'accordion_toggle', { panel: e.detail.panel, open: e.detail.open, }); } }); }); },};AJAX Integration
Section titled “AJAX Integration”WC-2026 components work with Drupal AJAX automatically because:
- Components use standard HTML tags (no framework dependency)
Drupal.behaviors.attach()re-runs on AJAX-loaded content- Custom Elements upgrade automatically when added to DOM
BigPipe Streaming
Section titled “BigPipe Streaming”No special handling is needed. Components register via customElements.define() and upgrade as they enter the DOM through BigPipe streaming.
Detailed Guide
Section titled “Detailed Guide”See the Pre-Planning: Drupal Integration Guide for advanced Behaviors patterns.