【问题标题】:Remove stylesheet selectively in Drupal for page在 Drupal 中为页面选择性地删除样式表
【发布时间】:2013-01-02 11:27:12
【问题描述】:

我正在尝试为首页制作不同的布局。在那个过程中,我声明了名为“front-page.css”和 page--front.tpl.php 的新样式表。我正在使用加载 responsive-sidebar.css 的 Zen 子主题。我想删除“responsive-sidebar.css”并加载“front-page.css”。 我这样做的原因是后面样式表中的研磨列数与前者不同。

我不想使用面板模块。我正在使用 Drupal 7。

【问题讨论】:

    标签: php drupal drupal-7 drupal-theming drupal-zen


    【解决方案1】:

    Drupal 7 的方式是使用hook_css_alter():

    function MYMODULE_css_alter(&$css) {
      // Remove defaults.css file. The path will probably change for your theme obviously.
      unset($css[drupal_get_path('theme', 'MYTHEME') . '/css/responsive-sidebar.css']);
    }
    

    该钩子可以在模块或主题中实现。

    【讨论】:

      【解决方案2】:

      在主题的template.php 文件和template_preprocess_page($vars) 中,在$vars['stylesheets'] 中找到要删除的CSS 文件,然后使用PHP 的unset 函数将其从$vars['stylesheets'] 数组中删除。

      【讨论】:

      • 这可以在 Drupal 6 中工作,但在 Drupal 7 中的 $vars 数组中不存在样式表键。D7 改变了这样做的方式!
      【解决方案3】:

      我刚刚找到了更好的方法来做到这一点。它有点 hacky,但允许在几乎任何地方取消设置 css:

      $css = &drupal_static('drupal_add_css', array());
      unset($css['some_css_path']);
      

      【讨论】:

        【解决方案4】:

        Drupal 8 方式。也许它在 Drupal 7 中有效。要删除某个/特定页面上的样式表,您需要:

        function MYTHEME_css_alter(&$css) {
        
        // Get me path:
          $current_path = \Drupal::service('path.current')->getPath();
          $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
        
            if ($result == 'your/path' ){
              unset($css[drupal_get_path('theme', 'THEMENAME') . '/css/style.css']);
            }
        }
        

        它在 D8 中适用于我,可以将它放在 .theme 或 .module 中。您需要删除所有缓存(或模块缓存)。

        【讨论】:

          猜你喜欢
          • 2018-10-16
          • 1970-01-01
          • 1970-01-01
          • 2016-03-21
          • 1970-01-01
          • 2021-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多