You are here

public static function Drupal::classResolver in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal.php \Drupal::classResolver()
  2. 9 core/lib/Drupal.php \Drupal::classResolver()

Retrieves the class resolver.

This is to be used in procedural code such as module files to instantiate an object of a class that implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface.

One common usecase is to provide a class which contains the actual code of a hook implementation, without having to create a service.

Parameters

string $class: (optional) A class name to instantiate.

Return value

\Drupal\Core\DependencyInjection\ClassResolverInterface|object The class resolver or if $class is provided, a class instance with a given class definition.

Throws

\InvalidArgumentException If $class does not exist.

14 calls to Drupal::classResolver()
demo_umami_content_module_preinstall in core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
Implements hook_module_preinstall().
demo_umami_content_uninstall in core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
Implements hook_uninstall().
DrupalTest::testClassResolver in core/tests/Drupal/Tests/Core/DrupalTest.php
Tests the classResolver method.
DrupalTest::testClassResolverWithClass in core/tests/Drupal/Tests/Core/DrupalTest.php
Tests the classResolver method when called with a class.
field_field_config_insert in core/modules/field/field.module
Implements hook_ENTITY_TYPE_insert() for 'field_config'.

... See full list

File

core/lib/Drupal.php, line 352

Class

Drupal
Static Service Container wrapper.

Code

public static function classResolver($class = NULL) {
  if ($class) {
    return static::getContainer()
      ->get('class_resolver')
      ->getInstanceFromDefinition($class);
  }
  return static::getContainer()
    ->get('class_resolver');
}