基于@NoSssweat的信息编辑问题
图书模块对图书树块和图书页中呈现的索引视图使用相同的钩子。
我使用了下面的代码来返回文件名建议,这样我就可以覆盖特定区域的图书树块的html输出--“侧栏优先”。我希望下面的代码会显示出类似于book-tree--book-toc-180--sidebar-first.html.twig的内容。相反,它只是提出了一个已经被建议的文件名建议:
此文件名建议与图书页上的主要内容相同。如何针对sidebar-first区域内的任何图书树块?
// Add a region variable to a block.
// http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
function uswds_subtheme_preprocess_book_tree__book_toc_180(&$variables) {
if (isset($variables["elements"]["#id"])) {
$block_id = $variables["elements"]["#id"];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
$variables["content"]["#attributes"]["region"] = $block->getRegion();
}
}
}
// add a template suggestion based on region name
// http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
function uswds_subtheme_theme_suggestions_book_tree__book_toc_180_alter(array &$suggestions, array $variables) {
if (isset($variables["attributes"]["region"])) {
$suggestions[] = $variables["theme_hook_original"] . "__" . $variables["attributes"]["region"];
}
}2nd尝试我做了下面的工作,但它只适用于图书模块默认块,尽管任何其他图书模块块插件都主要使用book-tree.html.twig
// Add a region variable to a block.
function uswds_subtheme_preprocess_block(&$variables) {
if (isset($variables["elements"]["#id"])) {
$block_id = $variables["elements"]["#id"];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
$variables["content"]["#attributes"]["region"] = $block->getRegion();
}
}
}
// add a template suggestion based on region name
function uswds_subtheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (isset($variables["attributes"]["region"])) {
$suggestions[] = $variables["theme_hook_original"] . "__" . $variables["attributes"]["region"];
}
}发布于 2021-11-15 22:31:28
或者我怎样才能在树枝上做一些事情,比如
{% if region == 'sidebar-first' %},然后重写booktree.html.twig
// Adds a region attribute to a block.
function uswds_subtheme_preprocess_block(&$variables) {
if (isset($variables["elements"]["#id"])) {
$block_id = $variables["elements"]["#id"];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
$variables["content"]["#attributes"]["region"] = $block->getRegion();
}
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Creates region variable
*
* Gets rid of the region attribute added in uswds_subtheme_preprocess_block() as it's
* not a valid HTML attribute.
*/
function uswds_subtheme_preprocess_book_tree(&$variables) {
if (isset($variables['attributes']['region'])) {
// creates region variable for book-tree.html.twig
$variables['region'] = $variables['attributes']['region'];
// unset invalid html attribute.
unset($variables['attributes']['region']);
}
}发布于 2021-11-13 02:02:03
不错的尝试,因为这是一本书,而不是菜单,过程是相似的,但不完全一样。我希望你能搞清楚。
但不管怎样,给你:
/**
* Implements hook_preprocess_HOOK().
*
* Pass block region value to content so this can be used in
* uswds_subtheme_theme_suggestions_menu_alter() since $variables['elements']
* is not available there.
*/
function uswds_subtheme_preprocess_block(&$variables) {
if (isset($variables['elements']['#id']) && $variables['base_plugin_id'] === 'book_navigation') {
$region = \Drupal\block\Entity\Block::load($variables['elements']['#id'])->getRegion();
$content = $variables['content'];
foreach ($content as $content_key => $content_info) {
if (is_numeric($content_key)) {
$variables['content'][$content_key]['#attributes']['region'] = $region;
}
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Provide region based book suggestions.
*/
function uswds_subtheme_theme_suggestions_book_tree_alter(&$suggestions, array $variables) {
if (isset($variables['attributes']['region'])) {
$suggestion = 'book__' . $variables['theme_hook_original'] . '__' . $variables['attributes']['region'];
$suggestion = str_replace('-', '_', $suggestion);
$suggestions[] = $suggestion;
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Get rid of the region attribute added in uswds_subtheme_preprocess_block() as it's
* not a valid HTML attribute.
*/
function uswds_subtheme_preprocess_book_tree(&$variables) {
if (isset($variables['attributes']['region'])) {
unset($variables['attributes']['region']);
}
}发布于 2021-11-15 04:48:35
使用@NoSssweat和我所提供的所有帮助就可以完成以下工作:
// Add a region variable to a block.
function uswds_subtheme_preprocess_block(&$variables) {
if (isset($variables["elements"]["#id"])) {
$block_id = $variables["elements"]["#id"];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
$variables["content"]["#attributes"]["region"] = $block->getRegion();
}
}
}
// add a template suggestion based on region name
function uswds_subtheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (isset($variables["attributes"]["region"])) {
$suggestions[] = $variables["theme_hook_original"] . "__" . $variables["attributes"]["region"];
}
}哪些产出(第二项建议,包括该区域):
Update到工作版本:
getRegion();
}
}
}
// Adds template suggestion to blocks. Seems to work everywhere, but doesn't target the book tree
function uswds_subtheme_theme_suggestions_block_alter(array &$suggestions, array $variables){
if (!empty($variables['elements']['#id'])) {
$block = \Drupal\block\Entity\Block::load($variables['elements']['#id']);
$region = $block->getRegion();
// adds suggestion with region and block id
$suggestions[] = 'block__' . $region . '__' . $variables['elements']['#id'];
// adds suggestion with region id
$suggestions[] = 'block__' . $region;
}
}
// add a template suggestion based on region name
function uswds_subtheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (isset($variables["attributes"]["region"])) {
$suggestions[] = $variables["theme_hook_original"] . "__" . $variables["attributes"]["region"];
}
}https://drupal.stackexchange.com/questions/308102
复制相似问题