[英]Get the Book title in custom twig template
I'd like to display the main Book title on all pages of a book, instead of only the current page's title.我想在一本书的所有页面上显示主要的书名,而不仅仅是当前页面的标题。 However, I can't find a way to print this Book title in my Twig template.
但是,我找不到在我的 Twig 模板中打印此书名的方法。
How can I get the main Book title and print it in a custom twig template?如何获取主要书名并将其打印在自定义 twig 模板中?
In your hook_preprocess_node在你的 hook_preprocess_node
function hook_preprocess_node(&$variables) {
// Get node object;
$node = $variables['node'];
$node_id = $node->id();
$entity = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);
if (isset($entity->book)) {
$book_id = $entity->book['p1'];
$book_parent_entity = \Drupal::entityTypeManager()->getStorage('node')->load($book_id);
$title = $book_parent_entity->getTitle();
$variables['book_title'] = $title;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.