- Drupal 8 Development Cookbook(Second Edition)
- Matt Glaman
- 201字
- 2025-04-04 18:20:51
Using the Embed display
Each of the available display types has a method to expose itself through the user interface, except for Embed. Often, contributed and custom modules use Views to render displays instead of manually writing queries and rendering the output. Drupal 8 provides a special display type to simplify this.
If we were to add an Embed display to the view created in the recipe, we could pass the following render array to output our view programmatically:
$view_render = [ '#type' => 'view', '#name' => 'articles', '#display_id' => 'embed_1', ];
When rendered, the #type key tells Drupal that this is a view element. We then point it to our new display embed_1. The Embed display type has no special functionality, in fact, it is a simplistic display plugin. The benefit is that it does not have additional operations conducted for the sake of performance.
Using an Embed display is beneficial when you want to use a View in a custom page, block, or even form. For example, Drupal Commerce uses this pattern for its shopping cart block and the order summary in the checkout. A view is used to display the order information within a custom block and form.