2

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

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

主题/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) 构建的 localhost - 如果有任何用途。

我已经仔细检查了教程显示的所有配置,并且所有名称都正确(横幅、字段图像)。我似乎无法找到问题!

有什么建议么?

4

1 回答 1

1

如果不是拼写错误,您的主题名不是 MYBOOTSTRAPSUBTHEME,而是您所说的 MYTHEMENAME:

主题/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');
}

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

于 2018-10-13T02:30:28.547 回答