我是Drupal的新手,我非常喜欢它.我是靠自己学习但是现在我遇到了问题.
我创建了一个自定义块,显示标题和链接列表.因此,该块中的唯一字段是类型链接.这是我的页脚区域的一个块.因此,当用户点击时,它会在新标签页中打开一个新网站.
我想改变这个块类型的html输出,所以我拿出了这个主题建议:field - block-content - field-links - footer-links.html.twig
我的问题是如何为每个<a>
标签添加一个类?
我尝试使用模块'link class'.它有效.它只在安装模块之前读取css中已存在的类.它不会读取安装模块后创建的新类.我尝试了不同的东西,多次清理缓存但很奇怪.必须有一种更好的方法来添加没有模块的类.
任何人都可以帮助我并向我展示在我的<a>
标签中添加课程的最佳方法吗?
谢谢 :)
这是默认模板
{% if label_hidden %}
{% if multiple %}
<div{{ attributes }}>
{% for item in items %}
<div{{ item.attributes }}>{{ item.content }}</div>
{% endfor %}
</div>
{% else %}
{% for item in items %}
{{ item.content }} -------------> add class here
{% endfor %}
{% endif %}
{% else %}
<div{{ attributes }}>
<div{{ title_attributes }}>{{ label }}</div>
{% if multiple %}
<div>
{% endif %}
{% for item in items %}
<div{{ item.attributes }}>{{ item.content }}</div>
{% endfor %}
{% if multiple %}
</div>
{% endif %}
</div>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
将{{item.content}}替换为硬编码链接:
<a href="{{ item.content['#url']|render }}" {% if item.content['#options']['external'] %} target="_blank" {% endif %} class="some class here">{{ item.content['#title'] }}</a>
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要,您可以合并类选项:
{{ item.content|merge({'#options': {'attributes': {'class': ['some', 'class', 'here']}}}) }}
Run Code Online (Sandbox Code Playgroud)
对于那些喜欢使用 php 构建 html 渲染元素的人。一个非常典型的链接:
$elements[$delta] = [
'#type' => 'link',
'#title' => $value,
'#url' => Url::fromUri('mailto:' . $value),
'#attributes' => [
'class' => ['myClass']
]
];
Run Code Online (Sandbox Code Playgroud)
这取决于您如何构建自定义块。但就像你说的那样,创建一个新模板只是为了添加一些类有点矫枉过正。
因此,这里有一些关于如何创建一般链接以及如何向链接添加类等属性的好答案: https: //drupal.stackexchange.com/questions/144992/how-do-i-create-a-link
不同情况的答案略有不同,因此请自行寻找最适合您情况的答案。