我正在使用 Drupal 6.17 并想摆脱面包屑输出中的“HOME”...
例如:
$breadcrumb= 产品 // 软件 // 功能
代替
主页 // 产品 // 软件 // 功能
我正在使用 Drupal 6.17 并想摆脱面包屑输出中的“HOME”...
例如:
$breadcrumb= 产品 // 软件 // 功能
代替
主页 // 产品 // 软件 // 功能
这是 Drupal 7 版本:
/**
* Get rid of Home in breadcrumb trail.
*/
function <themename>_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users. Make the heading invisible with .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
array_shift($breadcrumb); // Removes the Home item
$output .= '<div class="breadcrumb">' . implode(' » ', $breadcrumb) . '</div>';
return $output;
}
}
覆盖主题的 template.php 文件中的面包屑:
/**
* Return a themed breadcrumb trail.
*
* @param $breadcrumb
* An array containing the breadcrumb links.
* @return a string containing the breadcrumb output.
*/
function phptemplate_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
array_shift($breadcrumb); // Removes the Home item
return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
}
}
(替换yourtheme
为您主题的实际机器名称...)
将以下内容添加到您的主题yourtheme.theme
文件中:
/**
* Prepares variables for `breadcrumb.html.twig`.
*/
function yourtheme_preprocess_breadcrumb(&$variables){
// Remove 'Home' from breadcrumb trail.
if (count($variables['breadcrumb'])) {
array_shift($variables['breadcrumb']);
}
}
将以下内容添加到您的主题中theme-settings.php
:
<?php
/**
* Implements hook_form_system_theme_settings_alter().
*/
function yourtheme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
$form['theme_settings']['hide_home_breadcrumb'] = array(
'#type' => 'checkbox',
'#title' => t('Hide <em>Home</em> in breadcrumb trail'),
'#default_value' => theme_get_setting('hide_home_breadcrumb', 'yourtheme'),
);
}
将以下内容添加到您的主题yourtheme.theme
文件中:
/**
* Prepares variables for `breadcrumb.html.twig`.
*/
function yourtheme_preprocess_breadcrumb(&$variables){
// Remove 'Home' from breadcrumb trail.
if (!empty(theme_get_setting('hide_home_breadcrumb', 'yourtheme')) && count($variables['breadcrumb'])) {
array_shift($variables['breadcrumb']);
}
}
如果您希望在安装主题时默认禁用主页按钮,请将以下内容添加到您的主题中yourtheme.settings.yml
:
# Hide 'Home' in breadcrumb trail by default.
hide_home_breadcrumb: 1
如果您正在使用现有站点,并且正在使用 Drupal 配置与 Drupal 8 同步,您还应该yourtheme.settings.yml
在同步目录中添加/修改文件,然后运行drush cim sync
.
在某些情况下,如果主页链接是面包屑路径中的唯一项目,并且当面包屑路径中有其他项目要在面包屑路径中离开主页时,网站设计可能会要求隐藏主页链接。
以下是实现上述单选按钮的方法:
将以下内容添加到您的主题中theme-settings.php
:
<?php
/**
* Implements hook_form_system_theme_settings_alter().
*/
function yourtheme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
$form['breadcrumbs'] = [
'#type' => 'details',
'#title' => t('Breadcrumb'),
'#open' => TRUE,
];
$form['breadcrumbs']['hide_home_breadcrumb'] = array(
'#type' => 'radios',
'#options' => [
'0' => 'Show <em>Home</em> in breadcrumb trail (Drupal’s default behavior)',
'1' => 'Remove <em>Home</em> from breadcrumb trail',
'2' => 'Remove <em>Home</em> when it is the only breadcrumb in trail',
],
'#default_value' => theme_get_setting('hide_home_breadcrumb', 'yourtheme'),
);
}
将以下内容添加到您的主题yourtheme.theme
文件中:
/**
* Prepares variables for `breadcrumb.html.twig`.
*/
function yourtheme_preprocess_breadcrumb(&$variables){
// Remove 'Home' from breadcrumb trail based on theme settings variable.
//
// Possible values:
// - 0: do not remove
// - 1: remove
// - 2: remove if its the only item
$hide_home_breadcrumb = theme_get_setting('hide_home_breadcrumb', 'yourtheme');
if ($hide_home_breadcrumb == '1' && count($variables['breadcrumb'])) {
array_shift($variables['breadcrumb']);
}
elseif ($hide_home_breadcrumb == '2' && count($variables['breadcrumb']) == 1) {
array_shift($variables['breadcrumb']);
}
}
如果您希望在安装主题时默认禁用主页按钮,请将以下内容添加到您的主题中yourtheme.settings.yml
:
# Remove 'Home' in breadcrumb trail if its the only item.
hide_home_breadcrumb: '2'
如果您正在使用现有站点,并且正在使用 Drupal 配置与 Drupal 8 同步,您还应该yourtheme.settings.yml
在同步目录中添加/修改文件,然后运行drush cim sync
.
在 templated.php 中询问“主页”是否为特定语言。如果您不需要语言查找,请取出(&& $language_url->languag**e 和 **global $language_url; )。还将“href”中的“/htdocs/drupal/de”更改为适当的 URL。
function _YOUR_THEME_NAME_breadcrumb(&$variables) {
$breadcrumb = $variables['breadcrumb'];
global $language_url;
if (drupal_is_front_page()){
return;
} elseif(!empty($breadcrumb) && $language_url->language == 'de') {
$breadcrumb[0] = '<a href="/htdocs/drupal/de">STARTSEITE</a>';
$breadcrumb = implode(' | ', $breadcrumb);
return $breadcrumb;
}
}
如果您安装了路径面包屑模块。转到结构>路径面包屑>路径面包屑设置,然后选中“隐藏单个面包屑的面包屑导航”,保存配置,刷新页面。完毕。