Скрыть 'Home' Breadcrumb для Drupal 8
(замените yourtheme
на фактическое имя машины вашей темы ...)
Вариант 1: быстрая и простая предварительная обработка темы
Добавьте в файл 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']);
}
}
или вариант 2: флажок в настройках темы
![Screenshot demonstrating what the theme settings option looks like in the Drupal Admin Appearance UI](https://i.stack.imgur.com/gihNd.png)
Добавьте следующее в 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
.
или вариант 3: более тонкая настройка темы
В некоторых случаях дизайн сайта может потребовать сокрытия ссылки Home , если это единственный элемент на пути крошкии когда в следе хлебных крошек есть другие предметы, которые нужно оставить Дом в следе хлебных крошек.
![Screenshot demonstrating a more nuanced theme setting with 3 radio button options to select home breadcrumb visibility options](https://i.stack.imgur.com/CCocc.png)
Вот как реализовать переключатели, изображенные выше:
Добавьте следующее в 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
.