MYTHEME_preprocess_paragraph__PARAGRAPHTYPE not loading



我在实施我的网站时正在慢慢学习 D8,并决定按照 https://www.webwash.net/how-to-create-powerful-container-paragraphs-in-drupal-8/开始我的登录页面。

从hook_preprocess部分开始,我假设这将进入:

THEMES/MYBOOTSTRAPSUBTHEME/MYTHEMENAME.theme

<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/
function MYSUBTHEMENAME_preprocess_paragraph__banner(&$variables) {
$paragraph = $variables['paragraph'];
if (!$paragraph->field_image->isEmpty()) {
$image = $paragraph->field_image->entity->url();
$variables['attributes']['style'][] = 'background-image: url("' . $image . '");';
$variables['attributes']['style'][] = 'background-size: cover;';
$variables['attributes']['style'][] = 'background-position: center center;';
}
}

我已经从配置页面清除了缓存,但没有运气。这是使用 MAMP (PHP 7.1.6( 构建的本地主机 - 如果有任何用途的话。

我已经仔细检查了教程显示的所有配置,并且所有名称都是正确的(横幅,field_image(。我似乎找不到问题!

有什么建议吗?

如果不是错别字,您的主题名称不是MYBOOTSTRAPSUBTHEME,而是MYTHEMENAME,正如您所说:

THEMES/MYBOOTSTRAPSUBTHEME/MYTHEMENAME.theme

因此,应调用该函数:

function MYTHEMENAME_preprocess_paragraph__banner(&$variables) {
// If devel module is enabled you may check if it is working
// by adding some debug output:
// dpm('debug');
}

确保目录文件和函数名称使用小写字母。实现该功能后,不要忘记重建缓存。没有必要有一个树枝模板文件段落-banner.twig,它应该在没有它的情况下工作。

最新更新