forked from mortendk/drupaltwigsnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.html.twig
More file actions
236 lines (166 loc) · 5.61 KB
/
snippets.html.twig
File metadata and controls
236 lines (166 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
{#
TWIG SNIPPETS 1.0
#}
{#
a collection of twig snippets for Drupal8
general twig documentation: http://twig.sensiolabs.org/doc/templates.html
if you wanna digg deep:
/core/includes/theme.inc
#}
{# DEBUG #}
{{ dump(_context|keys) }}
<pre>
{{ dump(_context|keys) }}
</pre>
<hr>
<ol>
{% for key, value in _context %}
<li>{{ key }} </li>
{% if loop.index == 11 %}}
<pre>{{ dump( value ) }}</pre>
{% endif %}
{% endfor %}
</ol>
{# ----- Variables ----------------------------------------------------------#}
{# variabels #}
{{ variable }}
{{ variable.drill.into.it }}
{{ variable.drill.into['#somethingsomething'] }}
{# drill into a template and grap data out of it bypassing the "wrapper" tempakte
you need to fix the <p> etc if its a text field
else just create a template like this
#}
{{ content.field_teaser.0["#text"] }}
{# clean print of a fields data #}
{{ content.field_foobar.0|render()|striptags }}
{# Filters #}
{{ variable|filter }}
{# http://twig.sensiolabs.org/doc/filters/index.html #}
{# Drupal Translate #}
{{ variable|t }}
{{ 'string'|t }}
{% trans %}
this will be translated with {{ varibles }}
{% endtrans %}
{% trans %}
only one.
{% plural count %}
the count: {{ count }}
{% endtrans %}
{# usefull filters: #}
{{ a-class-name|clean_class }}
{{ an-id|clean_id }}
{{ translate|t }}
{# replace all of this with that #}
{{ element_id|replace({'this':'that'}) }}
{# --controls --------------------------------------------------------------- #}
{% if something %}
yo im here {{ var }}
{% endif %}
{% if something %}
this
{% else %}
that
{% endif %}
{% if something %}
this
{% elseif somethingelse %}
that
{% endif %}
{# for loop #}
{{ items|length }} items.
{% for item in items %}
{% if loop.first %}
{# first loop #}
{% elseif loop.last %}
{# last loop #}
{% elseif loop.index == "2" %}
{# 2nd loop #}
{% elseif loop.revindex == "2" %}
{# 2nd last reverse counted #}
{% else %}
{# default #}
{% endif %}
{% endfor %}
{# -----attributes & class magic --------------------------------------------- #}
{#
attributes https://www.drupal.org/node/2513632
everything can be changed:
{{ attributes.addClass('rush').removeClass('nickelback').setAttribute() }}
#}
{# add a class <div class="my-class"> #}
<div {{ attributes.addClass('my-class') }}
{# create a var and add that to classes
detect on first loop and if a class is in attributes
#}
{%
set class = [
'item',
'item--' ~ cycle(["green", "gold", "white"], loop.index),
loop.first ? 'first-class',
item.attribute.hasClass('is-this-class-here') ? 'ok-use-this':'not-there-use-this',
isthistrue ? 'nope' : 'yep',
]
%}
{# @TODO can hasClass be used for ex: hasAttribute ? #}
set classes = [
(bundle =='article') and (view_mode=="full") and (field_name=="field_teaser") ? 'hero__text' : '',
(bundle =='article') and (view_mode=="full") and (field_name=="field_content") ? 'f-cap' : '',
'foo'
]
{# add the class, and another my-class #}
<div {{ attributes.addClass('my-class', class) }}
{# addClass #}
<div {{ attributes.removeClass('nickelback') }}
<div {{ attributes.removeClass('nickelback').addClass('rush') }}
{# and they can all be combined #}
<div {{ attributes.addClass('my-class-here', class).removeClass('nope-not-you') }}
{# you can also use attribtes|without(class) & {{ attribtes.class }} #}
<div class=”my-class {{ attributes.class }}”{{attributes|without(‘class’) }}></div>
{# ----- Link -------------------------------------------- #}
{# add a class to a link #}
{{ link(item.title, item.url, { 'class':['foo', 'bar', 'baz']} ) }}
.setAttribute('id', 'top')
{# ----- set / remove attributes -------------------------------------------- #}
{{ attributes.setAttribute('id', 'my-id') }}>
{{ attributes.removeAttribute('id') }}
<img {% set image = 'https://www.drupal.org/files/powered-blue-135x42.png' %}>
<img setAttribute('src', image) }}>
{# ----------- give fiedls more brains -------------------------------------- #}
template suggestions:
basetheme define first so if it loads a template name suggestions thats "higer"
in the chain that will "override" your template.
ex:
x field--node--uid.html.twig (gets loaded in stable)
* field--uid.html.twig' (overwrites this template even if its in your theme)
{# use of include #}
{% include '@vanilla/foo.html.twig' %}
{# -----include stuff ------------------------------------------------------ #}
<img src="{{ base_path ~ directory }}/images/logo.png" alt="My Logo" />
{% include active_theme_path() ~ '/icons/icon.svg' %}
{# --- attach a library #}
{{ attach_library(active_theme()~'/thenameofthelibrary') }}
{# using filter #}
{{ item.content['#text']|striptags }}
{# first letter #}
{{ item.content['#title']|slice(0,1) }}
{# include svg #}
{% include active_theme_path() ~ '/icons/foobar.svg' %}
{# search for stuff #}
{% if ‘stuff' in ‘i do twig stuff in drupal ’ %}
{% endif %}
{# links #}
{{ link(item.title, item.url, { 'class':['foo', 'bar', 'baz']} ) }}
{{ file_url(node.field_example_image.entity.uri.value) }}
{# Link to frontpage view. #}
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>
{# Link to user entity/profile page. #}
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>
{# Link to node page. #}
<a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'View node page'|t }}</a>
{# test if a varialbe exist #}
{{ var|render }}
{{ content.field_teaser.0['#text']|striptags|replace({'&': '&', ' ': ' '}) }}
item.content['#url'].getUri|replace({'http://': '', 'https://': ''})
length of attributes value
{{ attributes.value|render()|length }}