Skip to content
Snippets Groups Projects

Issue #2834982: Replace non-test usages of \Drupal::service('renderer') with IoC injection

Open spokje requested to merge issue/drupal-2834982:2834982-replace-non-test-usages into 10.1.x
Files
23
@@ -10,6 +10,7 @@
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -42,6 +43,11 @@ class BookAdminEditForm extends FormBase {
*/
protected $entityRepository;
/**
* The renderer service.
*/
protected RendererInterface $renderer;
/**
    • Comment on lines 44 to 45
      Suggested change
      45
      46 /**
      47 * The renderer service.
      48 */
      49 protected RendererInterface $renderer;
      50
      51 /**
      45
      46 /**
Please register or sign in to reply
* Constructs a new BookAdminEditForm.
*
@@ -51,11 +57,18 @@ class BookAdminEditForm extends FormBase {
* The book manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository) {
public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository, RendererInterface $renderer = NULL) {
    • Suggested change
      63 public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository, RendererInterface $renderer = NULL) {
      63 public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository, protected RendererInterface $renderer = NULL) {
Please register or sign in to reply
$this->nodeStorage = $node_storage;
$this->bookManager = $book_manager;
$this->entityRepository = $entity_repository;
if (!$renderer) {
@trigger_error('Calling ' . __METHOD__ . '() without the $renderer argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/2876656', E_USER_DEPRECATED);
$renderer = \Drupal::service('renderer');
}
$this->renderer = $renderer;
}
    • Comment on lines 58 to 59
      Suggested change
      66 $this->entityRepository = $entity_repository;
      67 if (!$renderer) {
      68 @trigger_error('Calling ' . __METHOD__ . '() without the $renderer argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/2876656', E_USER_DEPRECATED);
      69 $renderer = \Drupal::service('renderer');
      70 }
      71 $this->renderer = $renderer;
      72 }
      66 $this->entityRepository = $entity_repository;
      67 if ($this->renderer === NULL) {
      68 @trigger_error('Calling ' . __METHOD__ . '() without the $renderer argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/2876656', E_USER_DEPRECATED);
      69 $this->renderer = \Drupal::service('renderer');
      70 }
      71 }
Please register or sign in to reply
/**
@@ -66,7 +79,8 @@ public static function create(ContainerInterface $container) {
return new static(
$entity_type_manager->getStorage('node'),
$container->get('book.manager'),
$container->get('entity.repository')
$container->get('entity.repository'),
$container->get('renderer')
);
}
@@ -235,7 +249,7 @@ protected function bookAdminTableTree(array $tree, array &$form) {
}
$form[$id]['title'] = [
'#prefix' => !empty($indentation) ? \Drupal::service('renderer')->render($indentation) : '',
'#prefix' => !empty($indentation) ? $this->renderer->render($indentation) : '',
'#type' => 'textfield',
'#default_value' => $data['link']['title'],
'#maxlength' => 255,
Loading