首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过钩子调试添加库?

如何通过钩子调试添加库?
EN

Drupal用户
提问于 2019-09-12 09:35:24
回答 1查看 45关注 0票数 0

我试图通过钩子在主题中附加简单的css,但它不起作用?在我看来密码是正确的。它不附着。我做错什么了?

代码语言:javascript
复制
global-styling:
  version: 8.x
  header: true
  css:
    base:
      css/main.css: 

  js:
    js/main.min.js: {}
    js/scrollreveal.js: {attributes: { defer: true} }
    src/js/vendor/picturefill/picturefill.min.js: {attributes: { defer: true} }
    js/custom.js: {attributes: { defer: true, async: true} }
    js/tp.widget.bootstrap.min.js: {attributes: { defer: true} }
    js/js.cookie.min.js: {attributes: { defer: true} }
    js/api.js: {attributes: { defer: true} }

  dependencies:
    - core/drupal
    - core/jquery

content-styling:
  css:
    base:
      css/content.css: {}

carousel-styling:
  css:
    base:
      css/carousel.css: {}

只有在显示对段落的实体引用时,我才试图附加css/content.css。只有在显示某个段落类型(滑块)时,我才试图附加css/carousel.css

代码语言:javascript
复制
function porto_sub_preprocess_field(array &$variables, $hook) {

  if(  $variables['element']['#entity_type'] == 'paragraph' ) {
     \Drupal::logger('my_module')->notice('content should be');
        // tried both these on all
        $variables['element']['#attached']['library'][] = 'porto_sub/content-styling';
        $variables['#attached']['library'][] = 'porto_sub/content-styling';
  } 
}

function porto_sub_preprocess_paragraph(&$variables) {
  $paragraph = $variables['paragraph'];
  if($paragraph->getParentEntity()->bundle()=='slider') {
    \Drupal::logger('my_module')->notice('carousel should be');
    $variables['element']['#attached']['library'][] = 'porto_sub/carousel-styling';

  }
}

然后我就什么都不做了

代码语言:javascript
复制
function porto_sub_page_attachments(array &$attachments) {
      $attachments['#attached']['library'][] = 'porto_sub/content-styling';
    $attachments['#attached']['library'][] = 'porto_sub/carousel-styling';

X}

有什么方法可以调试吗?我不知道这是怎么回事。

EN

回答 1

Drupal用户

发布于 2019-09-12 10:47:40

要将库附加到特定的段落类型,可以使用template_preprocess_paragraph__PARAGRAPH_TYPE(),如下所示。注意它是如何被添加到$variables数组中的,而不是元素。

代码语言:javascript
复制
function MYTHEME_preprocess_paragraph__slider(&$variables) {
  $variables['#attached']['library'][] = 'MYTHEME/carousel-styling';
}

田野也是如此。只需使用template_preprocess_field__FIELD_NAME()并直接将库附加到$variables数组。

您的content-stylingcarousel-styling库可能更像下面的样子。请注意,它是如何作为theme类别添加的,而不是作为基础添加的。有关SMACSS的一些信息,请参阅文件结构上的文档。

代码语言:javascript
复制
content-styling:
  css:
    theme:
      css/content.css: {}

And终于:刷新缓存,刷新缓存并再次刷新缓存!

票数 1
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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