Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Drupal.php

function 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 use case 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.

7 calls to Drupal::classResolver()
LayoutBuilderHooks::cron in core/modules/layout_builder/src/Hook/LayoutBuilderHooks.php
Implements hook_cron().
LayoutBuilderHooks::entityDelete in core/modules/layout_builder/src/Hook/LayoutBuilderHooks.php
Implements hook_entity_delete().
LayoutBuilderHooks::entityPresave in core/modules/layout_builder/src/Hook/LayoutBuilderHooks.php
Implements hook_entity_presave().
NavigationHooks::menuLinksDiscoveredAlter in core/modules/navigation/src/Hook/NavigationHooks.php
Implements hook_menu_links_discovered_alter().
search_post_update_block_with_empty_page_id in core/modules/search/search.post_update.php
Updates Search Blocks' without an explicit `page_id` from '' to NULL.

... See full list

File

core/lib/Drupal.php, line 364

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');
}
RSS feed
Powered by Drupal