Problem/Motivation
The critical_css_ui module declares support for Drupal 10 in critical_css_ui.info.yml (core_version_requirement: ^10 || ^11), but the entity definition in src/Entity/CriticalCSS.php uses PHP 8 attributes (#[ContentEntityType(...)]) which are only supported in Drupal 11.
This causes the entity to fail registration and functionality when the module is used on Drupal 10. According to the official Drupal documentation (https://www.drupal.org/docs/drupal-apis/entity-api/creating-a-custom-content-entity), Drupal 10 requires annotations in docblocks, while PHP 8 attributes are only available in Drupal 11.
The module's composer.json and critical_css_ui.info.yml correctly declare support for Drupal 10, but the implementation is incompatible with Drupal 10.
Steps to reproduce
- Install Drupal 10.x
- Install the
critical_css_uimodule - Clear Drupal cache
- Attempt to access Critical CSS entity functionality
- The entity type will not be properly registered
Current behavior:
- The module fails to properly register the
critical_csscontent entity type on Drupal 10 - Entity-related functionality does not work
- Error messages may appear indicating the entity type is not found or improperly defined
- The module works correctly on Drupal 11
Expected behavior:
- The module should work correctly on both Drupal 10 and Drupal 11 as declared in
core_version_requirement - The entity should be properly registered and functional on Drupal 10
- All entity operations (create, read, update, delete) should work on both versions
Proposed resolution
Convert the PHP 8 attribute syntax to Drupal 10-compatible annotation syntax in src/Entity/CriticalCSS.php.
Current code (Drupal 11 only):
use Drupal\Core\Entity\Attribute\ContentEntityType; #[ContentEntityType( id: 'critical_css', label: new TranslatableMarkup('Critical CSS'), label_collection: new TranslatableMarkup('Critical CSS'), label_singular: new TranslatableMarkup('critical css'), label_plural: new TranslatableMarkup('critical css'), entity_keys: [ 'id' => 'id', 'label' => 'target_context', 'published' => 'status', 'uuid' => 'uuid', ], handlers: [ 'list_builder' => CriticalCSSListBuilder::class, 'views_data' => EntityViewsData::class, // ... rest of attributes ], // ... rest of configuration )] class CriticalCSS extends ContentEntityBase {
Should be (Drupal 10 compatible):
use Drupal\Core\Entity\Annotation\ContentEntityType; /** * Defines the critical css entity class. * * @ContentEntityType( * id = "critical_css", * label = @Translation("Critical CSS"), * label_collection = @Translation("Critical CSS"), * label_singular = @Translation("critical css"), * label_plural = @Translation("critical css"), * entity_keys = { * "id" = "id", * "label" = "target_context", * "published" = "status", * "uuid" = "uuid", * }, * handlers = { * "list_builder" = "Drupal\critical_css_ui\CriticalCSSListBuilder", * "views_data" = "Drupal\views\EntityViewsData", * // ... rest of annotations * }, * // ... rest of configuration * ) */ class CriticalCSS extends ContentEntityBase {
Key changes needed:
- Replace
use Drupal\Core\Entity\Attribute\ContentEntityType;withuse Drupal\Core\Entity\Annotation\ContentEntityType; - Convert
#[ContentEntityType(...)]to@ContentEntityType(...)in a docblock - Convert attribute syntax (
id: 'value') to annotation syntax (id = "value") - Convert
new TranslatableMarkup('...')to@Translation("...") - Convert arrays from
[...]with:to{...}with= - Convert class references from
::classto full string class names
This change maintains full functionality while ensuring compatibility with both Drupal 10 and 11, as the module's core_version_requirement declares.
Remaining tasks
- Convert
src/Entity/CriticalCSS.phpfrom PHP 8 attributes to Drupal 10 annotations - Test the entity functionality on Drupal 10
- Verify the module still works correctly on Drupal 11
- Update any related documentation if necessary
User interface changes
None. This is a backend compatibility fix that does not affect the user interface.
API changes
None. The API remains the same; only the internal entity definition syntax changes from PHP 8 attributes to annotations for Drupal 10 compatibility.
Data model changes
None. No database schema or data model changes are required. The entity structure and fields remain identical.
Issue fork critical_css_ui-3559679
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
diegodz commentedComment #5
diegodz commentedComment #7
diegodz commented