首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在图书模块图书树块的主题建议中返回区域

尝试在图书模块图书树块的主题建议中返回区域
EN

Drupal用户
提问于 2021-11-09 02:34:45
回答 3查看 284关注 0票数 1

基于@NoSssweat的信息编辑问题

图书模块对图书树块和图书页中呈现的索引视图使用相同的钩子。

我使用了下面的代码来返回文件名建议,这样我就可以覆盖特定区域的图书树块的html输出--“侧栏优先”。我希望下面的代码会显示出类似于book-tree--book-toc-180--sidebar-first.html.twig的内容。相反,它只是提出了一个已经被建议的文件名建议:

代码语言:javascript
复制

此文件名建议与图书页上的主要内容相同。如何针对sidebar-first区域内的任何图书树块?

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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"];
  }
}
EN

回答 3

Drupal用户

发布于 2021-11-15 22:31:28

或者我怎样才能在树枝上做一些事情,比如{% if region == 'sidebar-first' %},然后重写booktree.html.twig

代码语言:javascript
复制
// 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']);
  }
}
票数 3
EN

Drupal用户

发布于 2021-11-13 02:02:03

不错的尝试,因为这是一本书,而不是菜单,过程是相似的,但不完全一样。我希望你能搞清楚。

但不管怎样,给你:

代码语言:javascript
复制
/**
 * 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']);
  }
}
票数 1
EN

Drupal用户

发布于 2021-11-15 04:48:35

使用@NoSssweat和我所提供的所有帮助就可以完成以下工作:

代码语言:javascript
复制
// 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"];
  }
}

哪些产出(第二项建议,包括该区域):

代码语言:javascript
复制

Update到工作版本:

代码语言:javascript
复制
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"];
  }
}
票数 1
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/308102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档