一键导入
drupal-state
Drupal State API — storing and retrieving runtime state that persists across requests but is not configuration.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Drupal State API — storing and retrieving runtime state that persists across requests but is not configuration.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
HTMX in Drupal 11.3+ core — the Htmx PHP fluent builder, dynamic forms with swapOob, partial routes with _htmx_route, response headers, and Drupal.behaviors integration. Use when building interactive UI without full-page reloads using Drupal's native HTMX support.
DDEV local development expertise. Use when working with DDEV projects, containers, configuration, or troubleshooting DDEV environments.
Custom Docker Compose local development patterns. Use when working with Docker-based local environments, container configuration, or troubleshooting Docker setups.
Drupal access control — permissions.yml, access callbacks, AccessResult, custom access checkers, and entity access.
Drupal Cache API — cache tags, contexts, max-age, cache bins, invalidation, and adding cache metadata to render arrays.
Drupal Composer management — requiring modules, updates, patches via composer-patches, and version constraints.
| name | drupal-state |
| description | Drupal State API — storing and retrieving runtime state that persists across requests but is not configuration. |
State is for runtime data that should persist across requests but is NOT configuration. Use Config for user-managed settings. Use State for internal operational data (last cron run, migration status, etc.).
use Drupal\Core\State\StateInterface;
// Inject
public function __construct(
private readonly StateInterface $state,
) {}
// Get
$value = $this->state->get('my_module.last_run');
$value = $this->state->get('my_module.last_run', $default_value);
// Set
$this->state->set('my_module.last_run', \Drupal::time()->getRequestTime());
// Delete
$this->state->delete('my_module.last_run');
// Get multiple
$values = $this->state->getMultiple(['key1', 'key2']);
// Set multiple
$this->state->setMultiple([
'my_module.status' => 'active',
'my_module.count' => 42,
]);
// Delete multiple
$this->state->deleteMultiple(['key1', 'key2']);
Use a namespaced key: module_name.key_name
// Good
$this->state->get('my_module.last_import_time');
$this->state->get('my_module.processed_count');
// Bad
$this->state->get('last_import'); // Not namespaced
| Storage | Use for | Exportable |
|---|---|---|
| State | Runtime operational data (last run times, counters, flags) | No |
| Config | User-managed settings, site configuration | Yes (via cex/cim) |
| Cache | Expensive computed data, invalidated on change | No (temporary) |
| UserData | Per-user preferences/data | No |
// Track last cron run
$this->state->set('my_module.cron_last', \Drupal::time()->getRequestTime());
// Feature flag (internal)
$isEnabled = $this->state->get('my_module.feature_enabled', FALSE);
// Track migration progress
$this->state->set('my_module.migration_offset', $offset);
// Store external API token (not sensitive — use KeyModule for secrets)
$this->state->set('my_module.api_token_expires', $expires);
# Get
drush state:get my_module.last_run
# Set
drush state:set my_module.feature_enabled 1
# Delete
drush state:del my_module.last_run
state