Problem/Motivation

DateTimeItem uses a hardcoded string in https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/datet...
DateTimeRangeItem uses the ::class constant https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/datet...

These strings are slightly different "\Drupal\datetime\DateTimeComputed" vs "Drupal\datetime\DateTimeComputed"

We have custom code that checks for this class to generate a list of date fields for some custom functionality. It would be nice if these were consistent.

Steps to reproduce

N/A

Proposed resolution

Use ::class in DateTimeItem

Remaining tasks

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3559755

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

acbramley created an issue. See original summary.

acbramley’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Seems straight forward. Searched the module for other instances and this was the only one.

mstrelan’s picture

Don't want to hold this up, but wonder if we should do this for all setClass calls. There are 20 instances of setClass('Drupal or setClass('\Drupal that could use the class constant instead.

$ grep -RE "setClass\('\\\\?Drupal\\\\" core
core/lib/Drupal/Core/Installer/InstallerServiceProvider.php:    $definition->setClass('Drupal\Core\Installer\InstallerRouteBuilder')
core/lib/Drupal/Core/CoreServiceProvider.php:      $uuid_service->setClass('Drupal\Component\Uuid\Pecl');
core/lib/Drupal/Core/CoreServiceProvider.php:      $uuid_service->setClass('Drupal\Component\Uuid\Com');
core/lib/Drupal/Core/Entity/entity.api.php:      ->setClass('\Drupal\my_module\EntityComputedText');
core/lib/Drupal/Core/Entity/entity.api.php:    $fields['my_module_text']->setClass('\Drupal\another_module\EntityComputedText');
core/lib/Drupal/Core/Entity/entity.api.php:    $fields['my_module_text']->setClass('\Drupal\another_module\EntityComputedText');
core/modules/language/src/LanguageServiceProvider.php:    $definition->setClass('Drupal\language\ConfigurableLanguageManager')
core/modules/system/tests/modules/service_provider_test/src/ServiceProviderTestServiceProvider.php:      $definition->setClass('Drupal\service_provider_test\TestFileUsage');
core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php:      ->setClass('\Drupal\datetime\DateTimeComputed')
core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php:      ->setClass('\Drupal\text\TextProcessed')
core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php:      ->setClass('\Drupal\text\TextProcessed')
core/modules/user/src/Entity/User.php:    $fields['name']->getItemDefinition()->setClass('\Drupal\user\UserNameItem');
core/tests/Drupal/KernelTests/Core/TypedData/ValidKeysConstraintValidatorTest.php:    $definition->setClass('Drupal\Core\Config\Schema\Mapping');
core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php:    $data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('\Drupal\Tests\Core\Plugin\TestPluginForm');
core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php:    $data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('Drupal\Tests\Core\Plugin\TestPluginForm');
core/tests/Drupal/Tests/Core/Theme/Icon/IconExtractorTest.php:    $plugin->setClass('\Drupal\Foo');
core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php:    $definition->setClass('Drupal\Core\Field\FieldItemList');
core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php:    $definition->setClass('Drupal\Core\Field\FieldItemList');
core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php:    $definition->setClass('Drupal\Core\Field\FieldItemList');
core/core.api.php: *     $definition->setClass('Drupal\my_module\MyLanguageManager');

There are 55 instances using class constants:

$ grep -RE "setClass\([A-Za-z0-9_\\]+::class\)" core | wc -l
55
acbramley’s picture

I wanted to keep a tight scope to keep this nice and easy. There is #2886352: Replace string 'path/to/class' with ::class constant which has an even wider scope and has gone stale for 7 years.

xjm’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

I get wanting to just fix the one instance, but this is a child/duplicate of #2886352: Replace string 'path/to/class' with ::class constant. Adding your credits over there. Thanks!

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.