Commit 0613dbf5 authored by catch's avatar catch

Issue #2949965 by alexpott, Lendude: \Drupal::classResolver() could be more helpful

parent 0a97488e
......@@ -320,10 +320,20 @@ public static function cache($bin = 'default') {
* One common usecase is to provide a class which contains the actual code
* of a hook implementation, without having to create a service.
*
* @return \Drupal\Core\DependencyInjection\ClassResolverInterface
* The class resolver.
* @param string $class
* (optional) A class name to instantiate.
*
* @return \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.
*/
public static function classResolver() {
public static function classResolver($class = NULL) {
if ($class) {
return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class);
}
return static::getContainer()->get('class_resolver');
}
......
......@@ -50,8 +50,7 @@ function field_layout_entity_type_alter(array &$entity_types) {
*/
function field_layout_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($display instanceof EntityDisplayWithLayoutInterface) {
\Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class)
->buildView($build, $display);
\Drupal::classResolver(FieldLayoutBuilder::class)->buildView($build, $display);
}
}
......@@ -62,8 +61,7 @@ function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_i
$form_object = $form_state->getFormObject();
if ($form_object instanceof ContentEntityFormInterface && $display = $form_object->getFormDisplay($form_state)) {
if ($display instanceof EntityDisplayWithLayoutInterface) {
\Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class)
->buildForm($form, $display);
\Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form, $display);
}
}
}
......@@ -59,8 +59,7 @@ function inline_form_errors_preprocess_datetime_wrapper(&$variables) {
* Implements hook_element_info_alter().
*/
function inline_form_errors_element_info_alter(array &$info) {
\Drupal::classResolver()->getInstanceFromDefinition(RenderElementHelper::class)
->alterElementInfo($info);
\Drupal::classResolver(RenderElementHelper::class)->alterElementInfo($info);
}
/**
......
......@@ -12,7 +12,7 @@
*/
function demo_umami_content_install() {
if (!\Drupal::service('config.installer')->isSyncing()) {
\Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->importContent();
\Drupal::classResolver(InstallHelper::class)->importContent();
}
}
......@@ -21,6 +21,6 @@ function demo_umami_content_install() {
*/
function demo_umami_content_uninstall() {
if (!\Drupal::service('config.installer')->isSyncing()) {
\Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->deleteImportedContent();
\Drupal::classResolver(InstallHelper::class)->deleteImportedContent();
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -120,8 +121,21 @@ public function testCache() {
* @covers ::classResolver
*/
public function testClassResolver() {
$this->setMockContainerService('class_resolver');
$this->assertNotNull(\Drupal::classResolver());
$class_resolver = $this->prophesize(ClassResolverInterface::class);
$this->setMockContainerService('class_resolver', $class_resolver->reveal());
$this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver());
}
/**
* Tests the classResolver method when called with a class.
*
* @covers ::classResolver
*/
public function testClassResolverWithClass() {
$class_resolver = $this->prophesize(ClassResolverInterface::class);
$class_resolver->getInstanceFromDefinition(static::class)->willReturn($this);
$this->setMockContainerService('class_resolver', $class_resolver->reveal());
$this->assertSame($this, \Drupal::classResolver(static::class));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment