Skip to content
Snippets Groups Projects
Commit fffa8a25 authored by Dave Long's avatar Dave Long
Browse files

Merge branch '3394870-allow-controller-service' into '11.x'

Resolve #3394870 "Allow controller service"

See merge request !5034
parents 905415d9 f58054b1
No related branches found
No related tags found
No related merge requests found
Pipeline #33390 failed
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url; use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Messenger\MessengerTrait;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
...@@ -106,7 +107,21 @@ abstract class ControllerBase implements ContainerInjectionInterface { ...@@ -106,7 +107,21 @@ abstract class ControllerBase implements ContainerInjectionInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static(); $args = [];
if (method_exists(static::class, '__construct')) {
$constructor = new \ReflectionMethod(static::class, '__construct');
foreach ($constructor->getParameters() as $parameter) {
$type = (string) $parameter->getType();
foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
$type = (string) $attribute->newInstance()->value;
}
$args[] = $container->get($type);
}
}
return new static(...$args);
} }
/** /**
......
...@@ -57,17 +57,6 @@ public function __construct(DateFormatterInterface $date_formatter, RendererInte ...@@ -57,17 +57,6 @@ public function __construct(DateFormatterInterface $date_formatter, RendererInte
$this->entityRepository = $entity_repository; $this->entityRepository = $entity_repository;
} }
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
$container->get('renderer'),
$container->get('entity.repository')
);
}
/** /**
* Displays add content links for available content types. * Displays add content links for available content types.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment