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

  1. Install Drupal 10.x
  2. Install the critical_css_ui module
  3. Clear Drupal cache
  4. Attempt to access Critical CSS entity functionality
  5. The entity type will not be properly registered

Current behavior:

  • The module fails to properly register the critical_css content 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:

  1. Replace use Drupal\Core\Entity\Attribute\ContentEntityType; with use Drupal\Core\Entity\Annotation\ContentEntityType;
  2. Convert #[ContentEntityType(...)] to @ContentEntityType(...) in a docblock
  3. Convert attribute syntax (id: 'value') to annotation syntax (id = "value")
  4. Convert new TranslatableMarkup('...') to @Translation("...")
  5. Convert arrays from [...] with : to {...} with =
  6. Convert class references from ::class to 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.php from 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.

Command icon 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

diegodz created an issue. See original summary.

diegodz’s picture

Status: Active » Needs review

  • diegodz committed 1dd0cd39 on 1.0.x
    feat: #3559679 Module uses PHP 8 attributes instead of annotations,...
diegodz’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

diegodz’s picture

Status: Fixed » Closed (fixed)