Skip to content

[#787] Fixed isBaseField() to include computed base fields.#303

Merged
AlexSkrypnyk merged 1 commit intomasterfrom
feature/787-computed-base-fields
Mar 17, 2026
Merged

[#787] Fixed isBaseField() to include computed base fields.#303
AlexSkrypnyk merged 1 commit intomasterfrom
feature/787-computed-base-fields

Conversation

@AlexSkrypnyk
Copy link
Collaborator

@AlexSkrypnyk AlexSkrypnyk commented Mar 17, 2026

Relates to jhedstrom/drupalextension#787

Summary

isBaseField() previously used getFieldStorageDefinitions() to look up base fields, which only returns base fields that have field storage (i.e. non-computed ones). Computed base fields — such as moderation_state provided by the Content Moderation module — are returned only by getBaseFieldDefinitions() and were therefore silently excluded, causing them to be ignored when creating entities.

The fix switches isBaseField() to use getBaseFieldDefinitions() directly. getEntityFieldTypes() is also updated to merge base field definitions into the field list when $base_fields is non-empty, ensuring computed base fields can be resolved to their type. Both isField() and getEntityFieldTypes() are refactored to use a new protected getEntityFieldManager() helper, which removes duplicated \Drupal::service() calls and makes the class more testable.

Changes

src/Drupal/Driver/Cores/Drupal8.php

  • isBaseField(): switched from getFieldStorageDefinitions() to getBaseFieldDefinitions() so computed base fields are recognised.
  • getEntityFieldTypes(): merges getBaseFieldDefinitions() into the field list when $base_fields is non-empty, allowing computed base fields to be included in the result.
  • Extracted getEntityFieldManager() protected helper — replaces three inline \Drupal::service('entity_field.manager') calls and enables unit testing without a full Drupal bootstrap.

tests/Drupal/Tests/Driver/Drupal8FieldMethodsTest.php (new)

  • Unit tests covering isBaseField(), isField(), and getEntityFieldTypes() with data providers.
  • Uses a TestDrupal8Core subclass that injects a mock EntityFieldManagerInterface, avoiding a full Drupal bootstrap.
  • Includes a namespace-isolated FieldStorageConfig stub to satisfy instanceof checks.

Before / After

Before — isBaseField() lookup:
  getFieldStorageDefinitions()
    ├── title             (BaseFieldDefinition) ✓ found
    ├── field_tags        (FieldStorageConfig)  — skipped
    └── [moderation_state MISSING — computed, no storage]  ✗ not found

After — isBaseField() lookup:
  getBaseFieldDefinitions()
    ├── title             ✓ found
    └── moderation_state  ✓ found  ← previously missed
Before — getEntityFieldTypes() with $base_fields = ['moderation_state']:
  fields = getFieldStorageDefinitions()  → {title, field_tags}
  isBaseField('moderation_state') → FALSE (not in storage defs)
  result → {field_tags}  ← moderation_state absent

After — getEntityFieldTypes() with $base_fields = ['moderation_state']:
  fields = getFieldStorageDefinitions() + getBaseFieldDefinitions()
         → {title, field_tags, moderation_state}
  isBaseField('moderation_state') → TRUE
  result → {field_tags, moderation_state}  ← now included

Summary by CodeRabbit

  • Tests

    • Added comprehensive test coverage for field methods, validating field detection, field type retrieval, and proper handling of base fields, configurable fields, and computed field scenarios.
  • Refactor

    • Improved field management access patterns and enhanced field definition consolidation logic to better recognize the full set of available fields and improve overall code maintainability.

@coderabbitai
Copy link

coderabbitai bot commented Mar 17, 2026

📝 Walkthrough

Walkthrough

The changes refactor entity field manager access in Drupal8 core by introducing a centralized helper method and expand field type detection to include base field definitions. A new comprehensive PHPUnit test suite validates the refactored methods using mocked dependencies.

Changes

Cohort / File(s) Summary
Core Refactoring
src/Drupal/Driver/Cores/Drupal8.php
Introduces getEntityFieldManager() helper method to centralize entity field manager service access. Updates getEntityFieldTypes() to conditionally merge base field definitions alongside storage definitions. Refactors isField() and isBaseField() to use the new helper instead of direct static service calls.
Test Suite
tests/Drupal/Tests/Driver/Drupal8FieldMethodsTest.php
Adds comprehensive PHPUnit test class with data-driven tests for isBaseField(), isField(), and getEntityFieldTypes(). Includes TestDrupal8Core subclass for dependency injection of mocked EntityFieldManager and a stub FieldStorageConfig class to support instanceof checks in test environment.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A helper method hops into place,
Fields and bases find their space,
Mocks dance and tests align,
The entity manager logic shines,
No more static calls to chase!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing isBaseField() to recognize computed base fields, which is the core problem solved by the PR.
Docstring Coverage ✅ Passed Docstring coverage is 86.67% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/787-computed-base-fields
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@AlexSkrypnyk AlexSkrypnyk merged commit 71a97a3 into master Mar 17, 2026
5 of 6 checks passed
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.

1 participant