This accepts views arguments as well:
{{ drupal_view('who_s_new', 'block_1') }}
{{ drupal_view('who_s_new', 'block_1' 'contextarg1', 'contextarg2', '...') }}
This might be useful to check if a view returns nothing:
{{ drupal_view_result('who_s_new', 'block_1') }}
No need to configure this block on "admin/structure/block" page:
{{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false}) }}
Bypass block.html.twig theming:
{{ drupal_block('system_branding_block', wrapper=false) }}
Pass theme name as a second argument if needed:
{{ drupal_region('sidebar_first') }}
It can be any content entity:
{{ drupal_entity('block_content', 1) }}
Form mode can be passed in third parameter:
{{ drupal_entity_form('node', 1) }}
Bundle (type) property is required for creating a new entity:
{{ drupal_entity_form('node', values={type: 'article'}) }}
Render a single entity field:
{{ drupal_field('field_image', 'node', 1) }}
Define view mode of the rendered field:
{{ drupal_field('field_image', 'node', 1, 'teaser') }}
Expand menu items to display the entire menu tree:
{{ drupal_menu('main') }}
Specify menu level, depth and expand all children:
{{ drupal_menu('admin', 2, 3, TRUE) }}
Slashes should be escaped:
{{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }}
The argument can be FID, UUID or URI. Renders an tag:
{{ drupal_image('public://ocean.jpg', null, {alt: 'Insert alternative text here', title: 'The title text'}) }}
Check out 'admin/config/media/responsive-image-style' page for available responsive image styles. Renders an <img srcset>
or <picture>
:
{{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }}
The filter processes either path or URI to original image. Renders a plain URL:
{{ 'public://images/ocean.jpg'|image_style('thumbnail') }}
Use token API to deliver data to your templates:
{{ drupal_token('site:name') }}
Tokens can be extracted from the context:
{{ drupal_token('node:title', {node: node}) }}
Replace multiple tokens at once:
{{ '<h1>[site:name]</h1><div>[site:slogan]</div>'|token_replace }}
Another way to get site name:
{{ drupal_config('system.site', 'name') }}
Without arguments this would dump all available Twig variables:
{{ drupal_dump(var) }}
Same as previous but shorter:
{{ dd(var) }}
The title is cached per URL:
{{ drupal_title() }}
Unlike core URL function it accepts path as an argument (not route).
{{ drupal_url('node/1', {absolute: true}) }}
The arguments are pretty much the same as in drupal_url():
{{ drupal_link('Example'|t, 'node/1') }}
Prints Drupal status messages without block wrapper:
{{ drupal_messages() }}
Same as above but for breadcrumbs:
{{ drupal_breadcrumb() }}
The wrapper element must have 'contextual-region' class:
<div class="contextual-region">
{{ contextual_links('entity.view.edit_form:view=frontpage&display_id=page_1') }}
{{ drupal_view('frontpage') }}
</div>
This makes Xdebug break on the specific line in the compiled Twig template:
{{ drupal_breakpoint() }}
Welcome back old friend! Refer to Notes (below) to enable:
© {{ 'return date("Y");'|php }}
The easiest way to alter their output:
{{ 'Drupal - community plumbing!'|preg_replace('/(Drupal)/', '<b>$1</b>') }}
Input language can be passed through arguments:
{{ 'Привет!'|transliterate }}
This supports file, image and media fields:
{{ node.field_media|file_url }}
Check out available input formats on admin/config/content/formats page:
{{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }}
See Drupal\Component\Utility\Unicode::truncate():
{{ 'Some long text'|truncate(10, true) }}
This is an opposite of core 'without' filter:
{{ item|with('#title', 'Example') }}
Do not put this into node.html.twig template to avoid recursion:
{{ node|view }}
You can supply display settings or view mode if necessary:
{{ node.field_image|view }}
Field view in 'teaser' view mode:
{{ node.field_image|view('teaser') }}
This is useful for processing individual field items:
{{ node.field_example|children }}
This is so helpful! Wanted to add for the views section. Using multiple contextual arguments needs to be formatted like so:
{{ drupal_view('who_s_new', 'block_1' 'contextarg1', 'contextarg2', 'etc') }}