diff -u b/core/includes/entity.inc b/core/includes/entity.inc --- b/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -352,10 +352,12 @@ * return $view_builder->view($entity, $view_mode, $langcode); * @endcode * + * @see https://www.drupal.org/node/3033656 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder() * @see \Drupal\Core\Entity\EntityViewBuilderInterface::view() */ function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $reset = FALSE) { + @trigger_error('entity_view() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode); instead. See https://www.drupal.org/node/3033656', E_USER_DEPRECATED); $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId()); if ($reset) { $render_controller->resetCache([$entity]); @@ -390,13 +392,12 @@ * return $view_builder->viewMultiple($entities, $view_mode, $langcode); * @endcode * + * @see https://www.drupal.org/node/3033656 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder() * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewMultiple() */ function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $reset = FALSE) { - $error_msg = __METHOD__ . ' is deprecated as of Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode);'; - trigger_error($error_msg, E_USER_DEPRECATED); - + @trigger_error('entity_view_multiple() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode); instead. See https://www.drupal.org/node/3033656', E_USER_DEPRECATED); $render_controller = \Drupal::entityManager()->getViewBuilder(reset($entities)->getEntityTypeId()); if ($reset) { $render_controller->resetCache($entities); diff -u b/core/modules/comment/comment.module b/core/modules/comment/comment.module --- b/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -272,10 +272,15 @@ * An array as expected by \Drupal\Core\Render\RendererInterface::render(). * * @deprecated in Drupal 8.x and will be removed before Drupal 9.0. - * Use \Drupal::entityManager()->getViewBuilder('comment')->view(). + * Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view(). + * + * @see https://www.drupal.org/node/3033656 */ function comment_view(CommentInterface $comment, $view_mode = 'full', $langcode = NULL) { - return entity_view($comment, $view_mode, $langcode); + @trigger_error("comment_view() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); + return \Drupal::entityTypeManager() + ->getViewBuilder('comment') + ->view($comment, $view_mode, $langcode); } /** @@ -298,9 +303,11 @@ * @deprecated in Drupal 8.x and will be removed before Drupal 9.0. * Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple(). * + * @see https://www.drupal.org/node/3033656 * @see \Drupal\Core\Render\RendererInterface::render() */ function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) { + @trigger_error("comment_view_multiple() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); return \Drupal::entityTypeManager() ->getViewBuilder('comment') ->viewMultiple($comments, $view_mode, $langcode); @@ -596,7 +603,9 @@ $field_name = $comment->getFieldName(); $entity = clone $entity; $entity->$field_name->status = CommentItemInterface::HIDDEN; - $build = entity_view($entity, 'full'); + $build = \Drupal::entityTypeManager() + ->getViewBuilder($entity->getEntityTypeId()) + ->view($entity, 'full'); } $preview_build['comment_output_below'] = $build; @@ -654,7 +663,9 @@ if (theme_get_setting('features.comment_user_picture')) { // To change user picture settings (for instance, image style), edit the // 'compact' view mode on the User entity. - $variables['user_picture'] = user_view($account, 'compact'); + $variables['user_picture'] = \Drupal::entityTypeManager() + ->getViewBuilder('user') + ->view($account, 'compact'); } else { $variables['user_picture'] = []; diff -u b/core/modules/comment/src/Plugin/views/field/EntityLink.php b/core/modules/comment/src/Plugin/views/field/EntityLink.php --- b/core/modules/comment/src/Plugin/views/field/EntityLink.php +++ b/core/modules/comment/src/Plugin/views/field/EntityLink.php @@ -16,7 +16,7 @@ class EntityLink extends FieldPluginBase { /** - * Stores the result of node_view_multiple for all rows to reuse it later. + * Stores the result of parent entities build for all rows to reuse it later. * * @var array */ diff -u b/core/modules/node/node.module b/core/modules/node/node.module --- b/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -644,7 +644,9 @@ // To change user picture settings (e.g. image style), edit the 'compact' // view mode on the User entity. Note that the 'compact' view mode might // not be configured, so remember to always check the theme setting first. - $variables['author_picture'] = user_view($node->getOwner(), 'compact'); + $variables['author_picture'] = \Drupal::entityTypeManager() + ->getViewBuilder('user') + ->view($node->getOwner(), 'compact'); } } @@ -815,9 +817,17 @@ * * @return array * An array as expected by \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager()->getViewBuilder('node')->view(). + * + * @see https://www.drupal.org/node/3033656 */ function node_view(NodeInterface $node, $view_mode = 'full', $langcode = NULL) { - return entity_view($node, $view_mode, $langcode); + @trigger_error("node_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->view(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); + return \Drupal::entityTypeManager() + ->getViewBuilder('node') + ->view($node, $view_mode, $langcode); } /** @@ -834,8 +844,14 @@ * @return array * An array in the format expected by * \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple(). + * + * @see https://www.drupal.org/node/3033656 */ function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) { + @trigger_error("node_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); return \Drupal::entityTypeManager() ->getViewBuilder('node') ->viewMultiple($nodes, $view_mode, $langcode); diff -u b/core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php b/core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php --- b/core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php +++ b/core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php @@ -99,7 +99,7 @@ // Render the node. $node_render_array = \Drupal::entityTypeManager() ->getViewBuilder('node') - ->viewMultiple([$node], 'teaser'); + ->view($node, 'teaser'); $html = \Drupal::service('renderer')->renderRoot($node_render_array); // Parse the teaser. diff -u b/core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php b/core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php --- b/core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php +++ b/core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php @@ -77,7 +77,7 @@ // Render the teaser. $node_render_array = \Drupal::entityTypeManager() ->getViewBuilder('node') - ->viewMultiple([$this->node], 'teaser'); + ->view($this->node, 'teaser'); $html = \Drupal::service('renderer')->renderRoot($node_render_array); // Parses front page where the node is displayed in its teaser form. diff -u b/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module --- b/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -197,9 +197,17 @@ * @return array * A $page element suitable for use by * \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->view(). + * + * @see https://www.drupal.org/node/3033656 */ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) { - return entity_view($term, $view_mode, $langcode); + @trigger_error("taxonomy_term_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->view(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); + return \Drupal::entityTypeManager() + ->getViewBuilder('taxonomy_term') + ->view($term, $view_mode, $langcode); } /** @@ -216,8 +224,14 @@ * @return array * An array in the format expected by * \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use + * \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple(). + * + * @see https://www.drupal.org/node/3033656 */ function taxonomy_term_view_multiple(array $terms, $view_mode = 'full', $langcode = NULL) { + @trigger_error("taxonomy_term_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); return \Drupal::entityTypeManager() ->getViewBuilder('taxonomy_term') ->viewMultiple($terms, $view_mode, $langcode); diff -u b/core/modules/user/user.module b/core/modules/user/user.module --- b/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -905,9 +905,17 @@ * * @return array * An array as expected by \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager()->getViewBuilder('user')->view(). + * + * @see https://www.drupal.org/node/3033656 */ function user_view($account, $view_mode = 'full', $langcode = NULL) { - return entity_view($account, $view_mode, $langcode); + @trigger_error("user_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('user')->view(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); + return \Drupal::entityTypeManager() + ->getViewBuilder('user') + ->view($account, $view_mode, $langcode); } /** @@ -924,8 +932,14 @@ * @return array * An array in the format expected by * \Drupal\Core\Render\RendererInterface::render(). + * + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. + * Use \Drupal::entityTypeManager()->getViewBuilder('user')->viewMultiple(). + * + * @see https://www.drupal.org/node/3033656 */ function user_view_multiple($accounts, $view_mode = 'full', $langcode = NULL) { + @trigger_error("user_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('user')->viewMultiple(); instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED); return \Drupal::entityTypeManager() ->getViewBuilder('user') ->viewMultiple($accounts, $view_mode, $langcode); only in patch2: unchanged: --- a/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php +++ b/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php @@ -83,7 +83,8 @@ public function testBasicRendering() { // Test the rendering of a block. $entity = Block::load('test_block1'); - $output = entity_view($entity, 'block'); + $builder = \Drupal::entityTypeManager()->getViewBuilder('block'); + $output = $builder->view($entity, 'block'); $expected = []; $expected[] = '
'; $expected[] = ' '; @@ -107,7 +108,7 @@ public function testBasicRendering() { ], ]); $entity->save(); - $output = entity_view($entity, 'block'); + $output = $builder->view($entity, 'block'); $expected = []; $expected[] = '
'; $expected[] = ' '; only in patch2: unchanged: --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -140,7 +140,9 @@ function contact_mail($key, &$message, $params) { case 'page_copy': $message['subject'] .= t('[@form] @subject', $variables, $options); $message['body'][] = t("@sender-name (@sender-url) sent a message using the contact form at @form-url.", $variables, $options); - $build = entity_view($contact_message, 'mail'); + $build = \Drupal::entityTypeManager() + ->getViewBuilder($contact_message->getEntityTypeId()) + ->view($contact_message, 'mail'); $message['body'][] = \Drupal::service('renderer')->renderPlain($build); break; @@ -159,7 +161,9 @@ function contact_mail($key, &$message, $params) { $message['body'][] = t('Hello @recipient-name,', $variables, $options); $message['body'][] = t("@sender-name (@sender-url) has sent you a message via your contact form at @site-name.", $variables, $options); $message['body'][] = t("If you don't want to receive such emails, you can change your settings at @recipient-edit-url.", $variables, $options); - $build = entity_view($contact_message, 'mail'); + $build = \Drupal::entityTypeManager() + ->getViewBuilder($contact_message->getEntityTypeId()) + ->view($contact_message, 'mail'); $message['body'][] = \Drupal::service('renderer')->renderPlain($build); break; } only in patch2: unchanged: --- a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php @@ -156,7 +156,9 @@ public function testAccess() { ->save(); // Invoke entity view. - entity_view($referencing_entity, 'default'); + \Drupal::entityTypeManager() + ->getViewBuilder($referencing_entity->getEntityTypeId()) + ->view($referencing_entity, 'default'); // Verify the un-accessible item still exists. $this->assertEqual($referencing_entity->{$field_name}->target_id, $this->referencedEntity->id(), format_string('The un-accessible item still exists after @name formatter was executed.', ['@name' => $name])); only in patch2: unchanged: --- a/core/modules/field_ui/tests/src/Functional/ManageDisplayTest.php +++ b/core/modules/field_ui/tests/src/Functional/ManageDisplayTest.php @@ -242,7 +242,9 @@ public function assertNodeViewTextHelper(EntityInterface $node, $view_mode, $tex // Render a cloned node, so that we do not alter the original. $clone = clone $node; - $element = node_view($clone, $view_mode); + $element = \Drupal::entityTypeManager() + ->getViewBuilder($clone->getEntityTypeId()) + ->view($clone, $view_mode); $output = (string) \Drupal::service('renderer')->renderRoot($element); $this->verbose(t('Rendered node - view mode: @view_mode', ['@view_mode' => $view_mode]) . '
' . $output); only in patch2: unchanged: --- a/core/modules/file/tests/src/Kernel/FileItemTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemTest.php @@ -141,7 +141,9 @@ public function testFileItem() { $entity = EntityTest::create(); $entity->file_test = ['entity' => $file3]; $uri = $file3->getFileUri(); - $output = entity_view($entity, 'default'); + $output = \Drupal::entityTypeManager() + ->getViewBuilder($entity->getEntityTypeId()) + ->view($entity, 'default'); \Drupal::service('renderer')->renderRoot($output); $this->assertTrue(!empty($entity->file_test->entity)); $this->assertEqual($entity->file_test->entity->getFileUri(), $uri); only in patch2: unchanged: --- a/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php +++ b/core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php @@ -22,9 +22,12 @@ class UnpublishByKeywordNode extends ConfigurableActionBase { * {@inheritdoc} */ public function execute($node = NULL) { + $elements = \Drupal::entityTypeManager() + ->getViewBuilder('node') + ->view(clone $node); + $render = \Drupal::service('renderer')->render($elements); foreach ($this->configuration['keywords'] as $keyword) { - $elements = node_view(clone $node); - if (strpos(\Drupal::service('renderer')->render($elements), $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) { + if (strpos($render, $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) { $node->setUnpublished(); $node->save(); break; only in patch2: unchanged: --- a/core/modules/node/src/Plugin/views/row/Rss.php +++ b/core/modules/node/src/Plugin/views/row/Rss.php @@ -126,7 +126,9 @@ public function render($row) { $build_mode = $display_mode; - $build = node_view($node, $build_mode); + $build = \Drupal::entityTypeManager() + ->getViewBuilder('node') + ->view($node, $build_mode); unset($build['#theme']); if (!empty($node->rss_namespaces)) { only in patch2: unchanged: --- a/core/modules/rdf/tests/src/Functional/ImageFieldAttributesTest.php +++ b/core/modules/rdf/tests/src/Functional/ImageFieldAttributesTest.php @@ -87,7 +87,9 @@ public function testNodeTeaser() { ->save(); // Render the teaser. - $node_render_array = node_view($this->node, 'teaser'); + $node_render_array = \Drupal::entityTypeManager() + ->getViewBuilder('node') + ->view($this->node, 'teaser'); $html = \Drupal::service('renderer')->renderRoot($node_render_array); // Parse the teaser. only in patch2: unchanged: --- a/core/modules/rdf/tests/src/Kernel/Field/FieldRdfaTestBase.php +++ b/core/modules/rdf/tests/src/Kernel/Field/FieldRdfaTestBase.php @@ -90,7 +90,9 @@ protected function assertFormatterRdfa($formatter, $property, $expected_rdf_valu entity_get_display('entity_test', 'entity_test', 'default') ->setComponent($this->fieldName, $formatter) ->save(); - $build = entity_view($this->entity, 'default'); + $build = \Drupal::entityTypeManager() + ->getViewBuilder($this->entity->getEntityTypeId()) + ->view($this->entity, 'default'); $output = \Drupal::service('renderer')->renderRoot($build); $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa'); $this->setRawContent($output); only in patch2: unchanged: --- a/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php +++ b/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php @@ -43,7 +43,8 @@ public function testTwigDebugMarkup() { // Create a node and test different features of the debug markup. $node = $this->drupalCreateNode(); - $build = node_view($node); + $builder = \Drupal::entityTypeManager()->getViewBuilder('node'); + $build = $builder->view($node); $output = $renderer->renderRoot($build); $this->assertTrue(strpos($output, '') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.'); $this->assertTrue(strpos($output, "THEME HOOK: 'node'") !== FALSE, 'Theme call information found.'); @@ -55,7 +56,7 @@ public function testTwigDebugMarkup() { // Create another node and make sure the template suggestions shown in the // debug markup are correct. $node2 = $this->drupalCreateNode(); - $build = node_view($node2); + $build = $builder->view($node2); $output = $renderer->renderRoot($build); $this->assertTrue(strpos($output, '* node--2--full' . $extension . PHP_EOL . ' * node--2' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.'); @@ -63,7 +64,7 @@ public function testTwigDebugMarkup() { // debug markup are correct. $node3 = $this->drupalCreateNode(); $build = ['#theme' => 'node__foo__bar']; - $build += node_view($node3); + $build += $builder->view($node3); $output = $renderer->renderRoot($build); $this->assertTrue(strpos($output, "THEME HOOK: 'node__foo__bar'") !== FALSE, 'Theme call information found.'); $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--<script type="text/javascript">alert('yo');</script>' . $extension . PHP_EOL . ' * node--3--full' . $extension . PHP_EOL . ' * node--3' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.'); @@ -75,7 +76,7 @@ public function testTwigDebugMarkup() { $this->rebuildContainer(); $this->resetAll(); - $build = node_view($node); + $build = $builder->view($node); $output = $renderer->renderRoot($build); $this->assertFalse(strpos($output, '') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.'); } only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/src/Kernel/Entity/EntityViewLegacyTest.php @@ -0,0 +1,229 @@ +getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode); instead. See https://www.drupal.org/node/3033656 + */ + public function testEntityView() { + $entity = EntityTest::create(); + $this->assertEntityView(entity_view($entity, 'default')); + } + + /** + * Tests deprecation of the entity_view_multiple() function. + * + * @expectedDeprecation entity_view_multiple() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode); instead. See https://www.drupal.org/node/3033656 + */ + public function testEntityViewMultiple() { + $entities = [ + EntityTest::create(), + EntityTest::create(), + ]; + $this->assertEntityViewMultiple(entity_view_multiple($entities, 'default')); + } + + /** + * Installs all comment related dependencies. + * + * @return int + * The entity ID to which the comments are attached to. + */ + protected function setUpComment() { + $this->installEntitySchema('entity_test'); + $this->installEntitySchema('user'); + $this->installEntitySchema('comment'); + $this->installSchema('comment', ['comment_entity_statistics']); + CommentType::create([ + 'id' => 'comment', + 'label' => $this->randomString(), + 'target_entity_type_id' => 'entity_test', + ])->save(); + FieldStorageConfig::create([ + 'entity_type' => 'entity_test', + 'type' => 'comment', + 'field_name' => 'comments', + 'settings' => [ + 'comment_type' => 'comment', + ], + ])->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + 'field_name' => 'comments', + ])->save(); + + $host = EntityTest::create(['name' => $this->randomString()]); + $host->save(); + return $host->id(); + } + + /** + * Constructs comment entity. + * + * @param int $commentedId + * The commented entity id. + * + * @return \Drupal\comment\CommentInterface + * Comment entity. + */ + protected function createComment($commentedId) { + return Comment::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'comments', + 'entity_id' => $commentedId, + 'comment_type' => 'comment', + ]); + } + + /** + * Tests deprecation of the comment_view() function. + * + * @expectedDeprecation comment_view() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view(); instead. See https://www.drupal.org/node/3033656 + */ + public function testCommentView() { + $entity = $this->createComment($this->setUpComment()); + $this->assertEntityView(comment_view($entity)); + } + + /** + * Tests deprecation of the entity_view_multiple() function. + * + * @expectedDeprecation comment_view_multiple() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple(); instead. See https://www.drupal.org/node/3033656 + */ + public function testCommentViewMultiple() { + $commented_id = $this->setupComment(); + $entities = [ + $this->createComment($commented_id), + $this->createComment($commented_id), + ]; + $this->assertEntityViewMultiple(comment_view_multiple($entities)); + } + + /** + * Tests deprecation of the node_view() function. + * + * @expectedDeprecation node_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->view(); instead. See https://www.drupal.org/node/3033656 + */ + public function testNodeView() { + $entity = Node::create(['type' => 'test']); + $this->assertEntityView(node_view($entity)); + } + + /** + * Tests deprecation of the node_view_multiple() function. + * + * @expectedDeprecation node_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple(); instead. See https://www.drupal.org/node/3033656 + */ + public function testNodeViewMultiple() { + $entities = [ + Node::create(['type' => 'test']), + Node::create(['type' => 'test']), + ]; + $this->assertEntityViewMultiple(node_view_multiple($entities)); + } + + /** + * Tests deprecation of the taxonomy_term_view() function. + * + * @expectedDeprecation taxonomy_term_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->view(); instead. See https://www.drupal.org/node/3033656 + */ + public function testTaxonomyTermView() { + $entity = Term::create(['vid' => 'test']); + $this->assertEntityView(taxonomy_term_view($entity)); + } + + /** + * Tests deprecation of the taxonomy_term_view_multiple() function. + * + * @expectedDeprecation taxonomy_term_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple(); instead. See https://www.drupal.org/node/3033656 + */ + public function testTaxonomyTermViewMultiple() { + $entities = [ + Term::create(['vid' => 'test']), + Term::create(['vid' => 'test']), + ]; + $this->assertEntityViewMultiple(taxonomy_term_view_multiple($entities)); + } + + /** + * Tests deprecation of the user_view() function. + * + * @expectedDeprecation user_view() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('user')->view(); instead. See https://www.drupal.org/node/3033656 + */ + public function testUserView() { + $entity = User::create(); + $this->assertEntityView(user_view($entity)); + } + + /** + * Tests deprecation of the user_view_multiple() function. + * + * @expectedDeprecation user_view_multiple() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('user')->viewMultiple(); instead. See https://www.drupal.org/node/3033656 + */ + public function testUserViewMultiple() { + $entities = [ + User::create(['type' => 'test']), + User::create(['type' => 'test']), + ]; + $this->assertEntityViewMultiple(user_view_multiple($entities)); + } + + /** + * Asserts that build array of one entity is valid. + * + * @param array $build + * An array returned by entity builder for one entity. + */ + protected function assertEntityView(array $build) { + $this->assertNotEmpty($build); + } + + /** + * Asserts that build array of multiple entities is valid. + * + * @param array $build + * An array returned by entity builder for multiple entities. + */ + protected function assertEntityViewMultiple(array $build) { + unset($build['#sorted'], $build['#pre_render']); + $this->assertEquals(2, count($build)); + } + +} only in patch2: unchanged: --- a/core/modules/user/tests/src/Kernel/UserFieldsTest.php +++ b/core/modules/user/tests/src/Kernel/UserFieldsTest.php @@ -42,7 +42,9 @@ public function testUserFields() { 'name' => 'foobar', 'mail' => 'foobar@example.com', ]); - $build = user_view($user); + $build = \Drupal::entityTypeManager() + ->getViewBuilder('user') + ->view($user); $output = \Drupal::service('renderer')->renderRoot($build); $this->setRawContent($output); $userEmail = $user->getEmail(); only in patch2: unchanged: --- a/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php +++ b/core/modules/user/tests/src/Kernel/WhosOnlineBlockTest.php @@ -106,7 +106,9 @@ public function testWhosOnlineBlock() { // Test the rendering of a block. $entity = Block::load('views_block__who_s_online_who_s_online_block'); - $output = entity_view($entity, 'block'); + $output = \Drupal::entityTypeManager() + ->getViewBuilder($entity->getEntityTypeId()) + ->view($entity, 'block'); $this->setRawContent($this->renderer->renderRoot($output)); $this->assertRaw('2 users', 'Correct number of online users (2 users).'); $this->assertText($user1->getAccountName(), 'Active user 1 found in online list.');