Unverified Commit b5238ac9 authored by alexpott's avatar alexpott
Browse files

Issue #2818677 by msankhala, NickWilde, Berdir, alexpott, dimaro, poornima.n,...

Issue #2818677 by msankhala, NickWilde, Berdir, alexpott, dimaro, poornima.n, sidharthap, dhirendra.mishra, keshav.k, xjm, mahtab_alam, ieguskiza, borisson_, catch, larowlan: Replace usages of deprecated method drupal_render_root()
parent ce3fa516
......@@ -1093,13 +1093,13 @@ services:
- { name: render.main_content_renderer, format: html }
main_content_renderer.ajax:
class: Drupal\Core\Render\MainContent\AjaxRenderer
arguments: ['@element_info']
arguments: ['@element_info', '@renderer']
tags:
- { name: render.main_content_renderer, format: drupal_ajax }
- { name: render.main_content_renderer, format: iframeupload }
main_content_renderer.dialog:
class: Drupal\Core\Render\MainContent\DialogRenderer
arguments: ['@title_resolver']
arguments: ['@title_resolver', '@renderer']
tags:
- { name: render.main_content_renderer, format: drupal_dialog }
main_content_renderer.off_canvas:
......@@ -1114,7 +1114,7 @@ services:
- { name: render.main_content_renderer, format: drupal_dialog.off_canvas_top }
main_content_renderer.modal:
class: Drupal\Core\Render\MainContent\ModalRenderer
arguments: ['@title_resolver']
arguments: ['@title_resolver', '@renderer']
tags:
- { name: render.main_content_renderer, format: drupal_modal }
controller.form:
......
......@@ -776,7 +776,7 @@ function drupal_pre_render_link($element) {
/**
* Pre-render callback: Collects child links into a single array.
*
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use
* @deprecated in drupal:8.0.0 and will be removed before drupal:9.0.0. Use
* \Drupal\Core\Render\Element\Link::preRenderLinks() instead.
*
* @see https://www.drupal.org/node/2966725
......@@ -789,13 +789,14 @@ function drupal_pre_render_links($element) {
/**
* Renders final HTML given a structured array tree.
*
* @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use the
* 'renderer' service instead.
* @deprecated as of drupal:8.0.0 and is removed from drupal:9.0.0. Use
* \Drupal\Core\Render\RendererInterface::renderRoot() instead.
*
* @see \Drupal\Core\Render\RendererInterface::renderRoot()
* @see https://www.drupal.org/node/2912696
*/
function drupal_render_root(&$elements) {
@trigger_error('drupal_render_root() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Render\RendererInterface::renderRoot() instead. See https://www.drupal.org/node/2912696', E_USER_DEPRECATED);
return \Drupal::service('renderer')->renderRoot($elements);
}
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -22,14 +23,28 @@ class AjaxRenderer implements MainContentRendererInterface {
*/
protected $elementInfoManager;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a new AjaxRenderer instance.
*
* @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info_manager
* The element info manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(ElementInfoManagerInterface $element_info_manager) {
public function __construct(ElementInfoManagerInterface $element_info_manager, RendererInterface $renderer = NULL) {
$this->elementInfoManager = $element_info_manager;
if ($renderer === NULL) {
@trigger_error('The renderer service must be passed to ' . __METHOD__ . ' and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400', E_USER_DEPRECATED);
$renderer = \Drupal::service('renderer');
}
$this->renderer = $renderer;
}
/**
......@@ -52,7 +67,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
}
}
$html = $this->drupalRenderRoot($main_content);
$html = $this->renderer->renderRoot($main_content);
$response->setAttachments($main_content['#attached']);
// The selector for the insert command is NULL as the new content will
......@@ -60,7 +75,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
// behavior can be changed with #ajax['method'].
$response->addCommand(new InsertCommand(NULL, $html));
$status_messages = ['#type' => 'status_messages'];
$output = $this->drupalRenderRoot($status_messages);
$output = $this->renderer->renderRoot($status_messages);
if (!empty($output)) {
$response->addCommand(new PrependCommand(NULL, $output));
}
......@@ -68,12 +83,16 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
}
/**
* Wraps drupal_render_root().
* Wraps \Drupal\Core\Render\RendererInterface::renderRoot().
*
* @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use
* $this->renderer->renderRoot() instead.
*
* @todo Remove as part of https://www.drupal.org/node/2182149.
* @see https://www.drupal.org/node/2912696
*/
protected function drupalRenderRoot(&$elements) {
return drupal_render_root($elements);
@trigger_error('\Drupal\Core\Render\MainContent\AjaxRenderer::drupalRenderRoot() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use $this->renderer->renderRoot() instead. See https://www.drupal.org/node/2912696', E_USER_DEPRECATED);
return $this->renderer->renderRoot($elements);
}
}
......@@ -7,6 +7,7 @@
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -21,14 +22,28 @@ class DialogRenderer implements MainContentRendererInterface {
*/
protected $titleResolver;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a new DialogRenderer.
*
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(TitleResolverInterface $title_resolver) {
public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer = NULL) {
$this->titleResolver = $title_resolver;
if ($renderer === NULL) {
@trigger_error('The renderer service must be passed to ' . __METHOD__ . ' and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400', E_USER_DEPRECATED);
$renderer = \Drupal::service('renderer');
}
$this->renderer = $renderer;
}
/**
......@@ -38,7 +53,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
$response = new AjaxResponse();
// First render the main content, because it might provide a title.
$content = drupal_render_root($main_content);
$content = $this->renderer->renderRoot($main_content);
// Attach the library necessary for using the OpenDialogCommand and set the
// attachments for this Ajax response.
......
......@@ -19,7 +19,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
$response = new AjaxResponse();
// First render the main content, because it might provide a title.
$content = drupal_render_root($main_content);
$content = $this->renderer->renderRoot($main_content);
// Attach the library necessary for using the OpenModalDialogCommand and set
// the attachments for this Ajax response.
......
......@@ -41,7 +41,7 @@ class OffCanvasRenderer extends DialogRenderer {
* (optional) The position to render the off-canvas dialog.
*/
public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer, $position = 'side') {
parent::__construct($title_resolver);
parent::__construct($title_resolver, $renderer);
$this->renderer = $renderer;
$this->position = $position;
}
......
......@@ -292,7 +292,7 @@ public function testProcessedTextElement() {
'#text' => '<p>Hello, world!</p>',
'#format' => 'element_test',
];
drupal_render_root($build);
\Drupal::service('renderer')->renderRoot($build);
// Verify the attachments and cacheability metadata.
$expected_attachments = [
......
......@@ -50,6 +50,13 @@ class StyleSerializerTest extends ViewTestBase {
*/
protected $adminUser;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
......@@ -63,6 +70,7 @@ protected function setUp($import_test_views = TRUE) {
}
$this->enableViewsTestModule();
$this->renderer = \Drupal::service('renderer');
}
/**
......@@ -133,7 +141,7 @@ public function testSerializerResponses() {
// Mock the request content type by setting it on the display handler.
$view->display_handler->setContentType('json');
$output = $view->preview();
$this->assertIdentical($actual_json, (string) drupal_render_root($output), 'The expected JSON preview output was found.');
$this->assertIdentical($actual_json, (string) $this->renderer->renderRoot($output), 'The expected JSON preview output was found.');
// Test a 403 callback.
$this->drupalGet('test/serialize/denied', ['query' => ['_format' => 'json']]);
......@@ -694,8 +702,6 @@ public function testFieldapiField() {
* Tests the "Grouped rows" functionality.
*/
public function testGroupRows() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$this->drupalCreateContentType(['type' => 'page']);
// Create a text field with cardinality set to unlimited.
$field_name = 'field_group_rows';
......@@ -738,7 +744,7 @@ public function testGroupRows() {
// Check if the field_group_rows field is grouped.
$expected = [];
$expected[] = [$field_name => implode(', ', $grouped_field_values)];
$this->assertEqual($serializer->serialize($expected, 'json'), (string) $renderer->renderRoot($build));
$this->assertEqual($serializer->serialize($expected, 'json'), (string) $this->renderer->renderRoot($build));
// Set the group rows setting to false.
$view = Views::getView('test_serializer_node_display_field');
$view->setDisplay('rest_export_1');
......@@ -750,7 +756,7 @@ public function testGroupRows() {
foreach ($grouped_field_values as $grouped_field_value) {
$expected[] = [$field_name => $grouped_field_value];
}
$this->assertEqual($serializer->serialize($expected, 'json'), (string) $renderer->renderRoot($build));
$this->assertEqual($serializer->serialize($expected, 'json'), (string) $this->renderer->renderRoot($build));
}
/**
......
......@@ -3,11 +3,11 @@ services:
# behave differently depending on the 2nd argument.
main_content_renderer.wide_modal:
class: Drupal\dialog_renderer_test\Render\MainContent\WideModalRenderer
arguments: ['@title_resolver', 'wide']
arguments: ['@title_resolver', '@renderer', 'wide']
tags:
- { name: render.main_content_renderer, format: drupal_modal.wide }
main_content_renderer.extra_wide_modal:
class: Drupal\dialog_renderer_test\Render\MainContent\WideModalRenderer
arguments: ['@title_resolver', 'extra_wide']
arguments: ['@title_resolver', '@renderer', 'extra_wide']
tags:
- { name: render.main_content_renderer, format: drupal_modal.extra_wide }
......@@ -6,6 +6,7 @@
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Render\MainContent\ModalRenderer;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -30,11 +31,13 @@ class WideModalRenderer extends ModalRenderer {
*
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param string $mode
* The mode, either 'wide' or 'extra_wide'.
*/
public function __construct(TitleResolverInterface $title_resolver, $mode = 'wide') {
parent::__construct($title_resolver);
public function __construct(TitleResolverInterface $title_resolver, RendererInterface $renderer, $mode = 'wide') {
parent::__construct($title_resolver, $renderer);
$this->mode = $mode;
}
......@@ -45,7 +48,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
$response = new AjaxResponse();
// First render the main content, because it might provide a title.
$content = drupal_render_root($main_content);
$content = $this->renderer->renderRoot($main_content);
// Attach the library necessary for using the OpenModalDialogCommand and set
// the attachments for this Ajax response.
......
......@@ -5,8 +5,12 @@
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
......@@ -39,6 +43,48 @@ class Feed extends PathPluginBase implements ResponseDisplayPluginInterface {
*/
protected $usesPager = FALSE;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a PathPluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
* @param \Drupal\Core\State\StateInterface $state
* The state key value store.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state);
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('router.route_provider'),
$container->get('state'),
$container->get('renderer')
);
}
/**
* {@inheritdoc}
*/
......@@ -91,7 +137,7 @@ public function preview() {
if (!empty($this->view->live_preview)) {
$output = [
'#prefix' => '<pre>',
'#plain_text' => drupal_render_root($output),
'#plain_text' => $this->renderer->renderRoot($output),
'#suffix' => '</pre>',
];
}
......
......@@ -77,4 +77,30 @@ public function testProcessAttached() {
}
}
/**
* Tests the drupal_render_root() deprecation.
*
* @group legacy
* @expectedDeprecation drupal_render_root() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Render\RendererInterface::renderRoot() instead. See https://www.drupal.org/node/2912696
*/
public function testRenderRootDeprecation() {
\Drupal::state()->set('theme_preprocess_attached_test', TRUE);
$test_element = [
'#theme' => 'common_test_render_element',
'foo' => [
'#markup' => 'Kittens!',
],
];
drupal_render_root($test_element);
$expected_attached = [
'library' => [
'test/generic_preprocess',
'test/specific_preprocess',
],
];
$this->assertEqual($expected_attached, $test_element['#attached'], 'All expected assets from theme preprocess hooks attached.');
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Controller\AjaxRendererTest.
*/
namespace Drupal\Tests\Core\Controller;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Render\MainContent\AjaxRenderer;
use Drupal\Core\Render\RendererInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @coversDefaultClass \Drupal\Core\Render\MainContent\AjaxRenderer
......@@ -45,15 +42,23 @@ protected function setUp() {
'#commands' => [],
'#error' => NULL,
]);
$this->ajaxRenderer = new TestAjaxRenderer($element_info_manager);
$this->renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$container = new ContainerBuilder();
$container->set('renderer', $this->renderer);
\Drupal::setContainer($container);
$renderer = $this->createMock(RendererInterface::class);
$renderer->expects($this->any())
->method('renderRoot')
->willReturnCallback(function (&$elements, $is_root_call = FALSE) {
$elements += ['#attached' => []];
if (isset($elements['#markup'])) {
return $elements['#markup'];
}
elseif (isset($elements['#type'])) {
return $elements['#type'];
}
else {
return 'Markup';
}
});
$this->ajaxRenderer = new AjaxRenderer($element_info_manager, $renderer);
}
/**
......@@ -78,24 +83,19 @@ public function testRenderWithFragmentObject() {
$this->assertEquals('status_messages', $commands[1]['data']);
}
}
class TestAjaxRenderer extends AjaxRenderer {
/**
* {@inheritdoc}
* @group legacy
* @expectedDeprecation The renderer service must be passed to Drupal\Core\Render\MainContent\AjaxRenderer::__construct and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400
*/
protected function drupalRenderRoot(&$elements, $is_root_call = FALSE) {
$elements += ['#attached' => []];
if (isset($elements['#markup'])) {
return $elements['#markup'];
}
elseif (isset($elements['#type'])) {
return $elements['#type'];
}
else {
return 'Markup';
}
public function testConstructorRendererArgument() {
$element_info_manager = $this->createMock(ElementInfoManagerInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
->method('get')
->with('renderer')
->willReturn(NULL);
\Drupal::setContainer($container);
new AjaxRenderer($element_info_manager);
}
}
<?php
namespace Drupal\Tests\Core\Controller;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Render\MainContent\DialogRenderer;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @coversDefaultClass \Drupal\Core\Render\MainContent\DialogRenderer
* @group Ajax
*/
class DialogRendererTest extends UnitTestCase {
/**
* @group legacy
* @expectedDeprecation The renderer service must be passed to Drupal\Core\Render\MainContent\DialogRenderer::__construct and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400
*/
public function testConstructorRendererArgument() {
$title_resolver = $this->createMock(TitleResolverInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
->method('get')
->with('renderer')
->willReturn(NULL);
\Drupal::setContainer($container);
new DialogRenderer($title_resolver);
}
}
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