【问题标题】:Drupal-8, Adding CSS to a custom ModuleDrupal-8,将 CSS 添加到自定义模块
【发布时间】:2018-06-21 14:12:25
【问题描述】:

更新 我按照建议修复了 preprocess_html 钩子,并添加了模块结构的图片,可能有什么问题吗??

我刚刚为 drupal-8 创建了一个自定义模块,其中包含一个可自定义的块。非常简单,目前正在工作,但现在我想为块的内容添加一些外观。

所以我最后一次尝试是添加一个libraries.yml 到链接block_header.css 文件的模块中,并在渲染数组中添加了带有css 标签的#prefix 和#suffix (div class='foo')。

代码没有给我任何错误,但它没有应用 css 文件的字体粗细。

你能给我指出正确的方向吗?

这是文件:

block_header.libraries.yml

block_header:
version: 1.x
css:
    theme:
        css/block_header.css: {}

BlockHeader.php

<?php

namespace Drupal\block_header\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'Header' Block.
 *
 * @Block(
 *   id = "block_header",
 *   admin_label = @Translation("Block Header"),
 *   category = @Translation("Block Header"),
 * )
 */
class BlockHeader extends BlockBase implements BlockPluginInterface {

    function block_header_preprocess_html(&$variables) {
        $variables['page']['#attached']['library'][] = 'Fussion_Modules/block_header/block_header';
    }


  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this->getConfiguration();

    if (!empty($config['block_header_title']) && ($config['block_header_text'])) {
      $title = $config['block_header_title'];
      $descp = $config['block_header_text'];
    }
    else {
      $title = $this->t('<div>Atención! Titulo no configurado!</div> </p>');
      $descp = $this->t('<div>Atención! Descripción no configurada!</div>');
    }
    $block = array
        (
            'title' => array
            (
             '#prefix' => '<div class="title"><p>',
             '#suffix' => '</p></div>',
             '#markup' => t('@title', array('@title' => $title,)),
            ),
            'description' => array
            (
             '#prefix' => '<div class="descp"><p>',
             '#suffix' => '</p></div>',
             '#markup' => t('@descp', array('@descp' => $descp,))
            ),
        );
    return $block;  

  }


/**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);

    $config = $this->getConfiguration();

    $form['block_header_title'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Titulo del Bloque'),
      '#description' => $this->t('Titulo del Bloque'),
      '#default_value' => isset($config['block_header_title']) ? $config['block_header_title'] : '',
    );

    $form['block_header_text'] = array(
      '#type' => 'textarea',
      '#title' => $this->t('Descripción'),
      '#description' => $this->t('Descripción del bloque'),
      '#default_value' => isset($config['block_header_text']) ? $config['block_header_text'] : '',
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();
    $this->configuration['block_header_title'] = $values['block_header_title'];
    $this->configuration['block_header_text'] = $values['block_header_text'];
    $this->configuration['block_header_title'] = $form_state->getValue('block_header_title');
    $this->configuration['block_header_text'] = $form_state->getValue('block_header_text');
  }
}

block_header.css

.title{
    font-weight: 500;
    color:darkblue;
}

这是我的模块结构

任何想法我做错了什么?

【问题讨论】:

  • 您是否将block_header.css 添加到css 文件夹中?
  • 当然可以! css 文件夹位于模块文件夹内(Module/css/css_file)
  • 那么你必须附加库$build['#attached']['library'][] = 'block_header/block_header';
  • 应该在 BlockHeader.php 函数 build() 中使用? public function build() { $config = $this-&gt;getConfiguration(); $build['#attached']['library'][] = 'block_header/block_header';

标签: css drupal drupal-modules drupal-8


【解决方案1】:

在模块文件中重写以下函数

function block_header_preprocess_html(&$variables)       { $variables['page']['#attached']['library'][] =     'block_header/block_header'; }

【讨论】:

  • 不工作......它没有应用css格式......我做错了什么...... X|
【解决方案2】:

尝试更新您正在返回的 $block 数组并删除预处理函数:

    $block = array
(
    'title' => array
    (
     '#prefix' => '<div class="title"><p>',
     '#suffix' => '</p></div>',
     '#markup' => t('@title', array('@title' => $title,)),
    ),
    'description' => array
    (
     '#prefix' => '<div class="descp"><p>',
     '#suffix' => '</p></div>',
     '#markup' => t('@descp', array('@descp' => $descp,))
    ),
    '#attached' => array
    (
      'library' => array
      (
        'block_header/block_header'
      )
    )
);

【讨论】:

    【解决方案3】:

    所以,我终于找到了问题所在。我试图实现的 HOOK 以附加 *.css 文件,它需要位于 *.module 文件中,而我没有。

    所以我用这个 HOOK 创建了 block_header.module

    <?php
    
    /**
     * Implements hook_preprocess_HOOK()
     */
    function block_header_preprocess_block(&$variables) {
      if ($variables['plugin_id'] == 'block_header') {
        $variables['#attached']['library'][] = 'block_header/block_header';
      }
    }
    

    之后我只是删除了我正在使用的HOOK,所以BlockHeader.php的最终版本是:

    <?php
    
    namespace Drupal\block_header\Plugin\Block;
    
    use Drupal\Core\Block\BlockBase;
    use Drupal\Core\Block\BlockPluginInterface;
    use Drupal\Core\Form\FormStateInterface;
    
    /**
     * Provides a 'Header' Block.
     *
     * @Block(
     *   id = "block_header",
     *   admin_label = @Translation("Block Header"),
     *   category = @Translation("Block Header"),
     * )
     */
    
    class BlockHeader extends BlockBase implements BlockPluginInterface {
    
      /**
       * {@inheritdoc}
       */
      public function build() {
        $config = $this->getConfiguration();
    
        if (!empty($config['block_header_title']) && ($config['block_header_text'])) {
          $title = $config['block_header_title'];
          $descp = $config['block_header_text'];
        }
        else {
          $title = $this->t('<div>Atención! Titulo no configurado!</div> </p>');
          $descp = $this->t('<div>Atención! Descripción no configurada!</div>');
        }
        $block = array
            (
                'title' => array
                (
                 '#prefix' => '<div class="title"><p>', /* HERE I ADD THE CSS TAGS */
                 '#suffix' => '</p></div>',
                 '#markup' => t('@title', array('@title' => $title,)),
                ),
                'description' => array
                (
                 '#prefix' => '<div class="descp"><p>', /* HERE I ADD THE CSS TAGS */
                 '#suffix' => '</p></div>',
                 '#markup' => t('@descp', array('@descp' => $descp,))
                ),
            );
        return $block;  
    
      }
    
    
       /**
       * {@inheritdoc}
       */
      public function blockForm($form, FormStateInterface $form_state) {
        $form = parent::blockForm($form, $form_state);
    
        $config = $this->getConfiguration();
    
        $form['block_header_title'] = array(
          '#type' => 'textfield',
          '#title' => $this->t('Titulo del Bloque'),
          '#description' => $this->t('Titulo del Bloque'),
          '#default_value' => isset($config['block_header_title']) ? $config['block_header_title'] : '',
        );
    
        $form['block_header_text'] = array(
          '#type' => 'textarea',
          '#title' => $this->t('Descripción'),
          '#description' => $this->t('Descripción del bloque'),
          '#default_value' => isset($config['block_header_text']) ? $config['block_header_text'] : '',
        );
    
        return $form;
      }
    
      /**
       * {@inheritdoc}
       */
      public function blockSubmit($form, FormStateInterface $form_state) {
        parent::blockSubmit($form, $form_state);
        $values = $form_state->getValues();
        $this->configuration['block_header_title'] = $values['block_header_title'];
        $this->configuration['block_header_text'] = $values['block_header_text'];
        $this->configuration['block_header_title'] = $form_state->getValue('block_header_title');
        $this->configuration['block_header_text'] = $form_state->getValue('block_header_text');
      }
    }
    

    就是这样,现在我将创建的 *.css 文件应用于模块创建的块。

    特别感谢@No Sssweat

    【讨论】:

      【解决方案4】:

      遵循this page at drupal.org 建议的另一种方法是将 css 文件附加到块的构建数组中。所以在 block_header.libraries.yml 你可以写

      block_header.tree:
        css:
          theme:
            css/block_header.css: {}
      

      而在BlockHeader.php

      public function build() {
        [...]
        block = array (
          '#attached' => array(
            'library' => array(
              'block_header/block_header.tree',
            ),
          ),
          [...]
        ),
      }
      

      【讨论】:

        【解决方案5】:

        一种方法是:

        1. 将库添加到模块中的block_header.info.yml 文件:

          libraries:
          
            - block_header/block_header
          
        2. 创建block_header.libraries.yml文件并添加:

          block_header:
          version: 1.x
          css:
            module:
              css/block_header.css: {}
          
        3. 将你要附加的css文件放到css/block_header.css

        4. 在自定义表单语句中包含 css

        5. 将以下行置于模块的表单构建部分:$form['#attached']['library'][] = 'block_header/block_header';

        【讨论】:

          猜你喜欢
          • 2016-10-05
          • 2013-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-03
          • 2020-05-06
          • 2015-02-21
          • 2023-03-29
          相关资源
          最近更新 更多