【问题标题】:Colorbox JS and CSS not added into all pagesColorbox JS 和 CSS 未添加到所有页面中
【发布时间】:2020-02-23 07:04:01
【问题描述】:

我在基于 Drupal 8 的网站上设置了一个 Colorbox 模块,但我发现它不适用于每种内容类型,因为与 Colorbox 相关的 CSS 和 JS 文件并未包含在所有页面中。

我有两种不同的内容类型,我们称之为“好”和“坏”。两者都在标准 html.html.twig 模板中呈现 CSS 和 JS,并使用 <js-placeholder token="{{ placeholder_token }}">

它们之间的唯一区别是,在 GOOD 内容类型中,有一个常见的 Image 字段,我将 Colorbox 设置为视图“处理程序”,同时在 BAD 模板中,图像是在其代码中创建的。所以我在等效元素(<a class="colorbox .." rtc.)中添加了一个 CSS 类,但问题是 BAD 输出 html 中没有链接 Colorbox JS 文件。

来自 GOOD 的示例 putput(缩短):

<!-- BEGIN OUTPUT from 'themes/cav/templates/html.html.twig' -->


<!DOCTYPE html>
<html lang="cs" dir="ltr" prefix="og: http://ogp.me/ns#">
  <head>
...
<link rel="stylesheet" media="all" href="/core/modules/quickedit/css/quickedit.icons.theme.css?q01vyb" />
<link rel="stylesheet" media="all" href="/core/themes/seven/css/components/quickedit.css?q01vyb" />
<link rel="stylesheet" media="all" href="/modules/colorbox/styles/plain/colorbox_style.css?q01vyb" />
<link rel="stylesheet" media="all" href="/core/modules/toolbar/css/toolbar.theme.css?q01vyb" />
...

从坏处:

<!-- BEGIN OUTPUT from 'themes/cav/templates/html.html.twig' -->


<!DOCTYPE html>
<html lang="cs" dir="ltr" prefix="og: http://ogp.me/ns#">
  <head>
    <meta charset="utf-8" />
...
<link rel="stylesheet" media="all" href="/core/modules/quickedit/css/quickedit.icons.theme.css?q01ykj" />
<link rel="stylesheet" media="all" href="/core/themes/seven/css/components/quickedit.css?q01ykj" />
<link rel="stylesheet" media="all" href="/core/modules/toolbar/css/toolbar.theme.css?q01ykj" />
...

我不知何故觉得那些与 Colorbox 相关的 JS 和 CSS 文件没有链接到 BAD,因为我没有在 BAD 内容类型中“设置 Colorbox”(因为没有正式的图像字段,所以我不能)但是有没有办法“强制”以其他方式将它们添加到页面中?

谢谢。 TC

【问题讨论】:

    标签: php drupal-8 colorbox drupal-theming drupal-templates


    【解决方案1】:

    Colorbox 以及所有模块和库仅在必要时加载(因为它是一个字段格式化程序,它仅在您以该格式显示该字段时才加载)。这是仅向浏览器发送必要代码的默认行为。 您可以使用 https://www.drupal.org/project/colorbox_inline 模块在每个页面中加载 Colorbox。

    【讨论】:

    • 谢谢,我试试看。
    • 好的,我已经安装了这个模块,但问题仍然存在。所以我可以向元素添加必需的属性,但是在放置元素的页面上,没有加载颜色框所需的 CSS 和 JS。我已经验证了模板,它应该是模板/html.html.twig 的一部分,但正如我在描述中所写的那样,它不在我想要的页面上:(
    • 我已经在 simpletest.me 上进行了尝试,并且成功了。我在想有什么区别。您是否将 jquery.colorbox-min.js 添加到库中?你有生成的 HTML 中的所有 js 文件吗? &lt;script src="/libraries/colorbox/jquery.colorbox-min.js?v=8.8.0-rc1"&gt;&lt;/script&gt; &lt;script src="/modules/contrib/colorbox/js/colorbox.js?v=8.8.0-rc1"&gt;&lt;/script&gt; &lt;script src="/modules/contrib/colorbox/styles/plain/colorbox_style.js?v=8.8.0-rc1"&gt;&lt;/script&gt; &lt;script src="/modules/contrib/colorbox_inline/js/colorbox_inline.js?0"&gt;&lt;/script&gt;
    • 这就是问题所在 - 我在页面中没有与 Colorbox 相关的 JS(和 CSS)文件,也不知道为什么 :( 我在 Drupal 论坛帖子中更好地描述了我的问题:@ 987654322@如果有帮助的话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2014-06-28
    • 1970-01-01
    • 2021-08-21
    • 2021-11-29
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多