📅  最后修改于: 2023-12-03 15:14:48.167000             🧑  作者: Mango
Drupal 8 provides powerful tools for building custom content types and managing them using entities. Entities allow developers to define and manipulate structured data in a consistent way across the Drupal ecosystem. One of the key components in this system is the Entity View feature, which makes it easy to define how entities should be rendered on the front-end.
Entity View is a core Drupal 8 module that provides developers with a set of functions for rendering entities. These functions can be used to define how a given entity should be displayed on the front-end. This can include defining the HTML markup, selecting the appropriate templates, and setting variables for the template.
To create a custom Entity View for your Drupal 8 site, you will need to:
hook_entity_view
function./**
* Implements hook_entity_view().
*/
function mymodule_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Set the template to use for our custom Entity View.
$build['#theme'] = 'my_custom_template';
// Set variables for the template.
$build['my_custom_variable'] = 'Hello, World!';
// Return the build array.
return $build;
}
This function defines a new Entity View for our site and sets the template to my_custom_template
. It also sets a custom variable my_custom_variable
that will be available to the template. Finally, it returns the $build
array that contains all the information that will be used to render the entity.
<article>
<header>
<h1><?php print $title; ?></h1>
</header>
<div class="content">
<?php print $my_custom_variable; ?>
</div>
</article>
This template file defines the markup for our custom Entity View. It uses the my_custom_variable
variable that we defined in the hook_entity_view
function.
Drupal 8 Entity View provides a powerful toolset for developers to define and render custom content types on the front-end. By using the hook_entity_view
and custom templates, it's possible to create rich, customizable displays for any content type.