drupal 8 entity_view
The entity_view
function in Drupal 8 is used to render an entity, such as a node or a user, into HTML. It takes the entity object and an optional view mode as parameters, and returns a render array that can be rendered into HTML using the render
function.
Here's an example of how you can use the entity_view
function:
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$view_mode = 'full'; // The view mode to use, default is 'default'
$build = \Drupal::entityTypeManager()
->getViewBuilder('node')
->view($node, $view_mode);
$output = \Drupal::service('renderer')->render($build);
In the example above, we load a node object using the entity type manager, specify the view mode as 'full', and then use the getViewBuilder
method to get the view builder for the 'node' entity type. We then call the view
method on the view builder, passing in the node object and the view mode. This returns a render array representing the rendered entity.
Finally, we use the renderer service to render the render array into HTML, which is stored in the $output
variable.
Note that the entity_view
function is deprecated in Drupal 8 and will be removed in Drupal 9. Instead, you should use the entity type's view builder directly, as shown in the example above.