Skip to content

Fix hook_entity_operation cacheability parameter should not be nullable#985

Merged
mglaman merged 1 commit intomainfrom
fix/984-hook-entity-operation-cacheability-not-nullable
Apr 23, 2026
Merged

Fix hook_entity_operation cacheability parameter should not be nullable#985
mglaman merged 1 commit intomainfrom
fix/984-hook-entity-operation-cacheability-not-nullable

Conversation

@mglaman
Copy link
Copy Markdown
Owner

@mglaman mglaman commented Apr 23, 2026

Summary

  • Fixes CacheableMetadata parameter in entity operation hooks is not nullable #984: $cacheability in hook_entity_operation and hook_entity_operation_alter is not nullable — only EntityListBuilder methods use nullable for BC (removed in D12)
  • Changed isValidParam(..., true, ...)false in both hook rules so non-nullable CacheableMetadata $cacheability is accepted (nullable is also still accepted)
  • Updated error messages to recommend \Drupal\Core\Cache\CacheableMetadata $cacheability without ? / = NULL
  • Added non-nullable "good" fixture examples; kept nullable variants to confirm both pass

Test plan

  • php vendor/bin/phpunit --filter=HookEntityOperationCacheabilityRuleTest
  • php vendor/bin/phpunit --filter=ProceduralHookEntityOperationCacheabilityRuleTest
  • php vendor/bin/phpunit --filter=EntityListBuilderOperationsCacheabilityRuleTest (unchanged, still nullable)
  • php vendor/bin/phpcs src/

🤖 Generated with Claude Code

… as nullable

The $cacheability parameter in hook_entity_operation and
hook_entity_operation_alter is not nullable — only EntityListBuilder
methods use nullable for BC purposes (removed in D12). The previous
rules passed isNullable=true, rejecting non-nullable CacheableMetadata
and suggesting the wrong ?CacheableMetadata $cacheability = NULL form.

Fixes #984

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the hook entity operation cacheability rules to accept the canonical non-nullable CacheableMetadata $cacheability parameter (while keeping nullable forms accepted) and aligns test fixtures/error messages with that recommendation.

Changes:

  • Adjusted both procedural and attribute-based hook rules to accept non-nullable CacheableMetadata parameters.
  • Updated rule error messages to recommend the non-nullable canonical signature.
  • Expanded test fixtures to include “good” non-nullable examples and updated PHPUnit expectations accordingly.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Rules/Drupal/ProceduralHookEntityOperationCacheabilityRule.php Accepts non-nullable CacheableMetadata parameter for procedural hook signatures and updates guidance message.
src/Rules/Drupal/HookEntityOperationCacheabilityRule.php Accepts non-nullable CacheableMetadata parameter for #[Hook(...)] methods and updates guidance message.
tests/src/Rules/ProceduralHookEntityOperationCacheabilityRuleTest.php Updates expected error messages to recommend non-nullable signature.
tests/src/Rules/HookEntityOperationCacheabilityRuleTest.php Updates expected error messages to recommend non-nullable signature.
tests/src/Rules/data/mymodule.module Adds “good” examples for procedural hooks (but currently not actually validated as hooks by the procedural rule).
tests/src/Rules/data/hook-entity-operation-cacheability.php Adds “good” non-nullable and nullable OOP hook implementations for fixture coverage.

Comment thread tests/src/Rules/data/mymodule.module
@mglaman mglaman merged commit fe66ebc into main Apr 23, 2026
18 of 19 checks passed
@mglaman mglaman deleted the fix/984-hook-entity-operation-cacheability-not-nullable branch April 23, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CacheableMetadata parameter in entity operation hooks is not nullable

2 participants