# Cheat sheet ## Drupal View ```twig {{ drupal_view('who_s_new', 'block_1') }} ``` ```twig {# Specify additional parameters which map to contextual filters you have configured in your view. #} {{ drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) }} ``` ## Drupal View Result Checks results for a given view. Note that the results themselves are not printable. ```twig {% if drupal_view_result('cart')|length == 0 %} {{ 'Your cart is empty.'|t }} {% endif %} ``` ## Drupal Block In order to figure out the plugin IDs list them using block plugin manager. With Drush, it can be done like follows: ```shell drush ev "print_r(array_keys(\Drupal::service('plugin.manager.block')->getDefinitions()));" ``` ```twig {# Print block using default configuration. #} {{ drupal_block('system_branding_block') }} {# Print block using custom configuration. #} {{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false, id}) }} {# Bypass block.html.twig theming. #} {{ drupal_block('system_branding_block', wrapper=false) }} ``` See [rendering blocks with Twig Tweak](blocks.md#block-plugin) for details. ## Drupal Region ```twig {# Print 'sidebar_first' region of the default site theme. #} {{ drupal_region('sidebar_first') }} {# Print 'sidebar_first' region of Bartik theme. #} {{ drupal_region('sidebar_first', 'bartik') }} ``` ## Drupal Entity ```twig {# Print a content block which ID is 1. #} {{ drupal_entity('block_content', 1) }} {# Print a node's teaser. #} {{ drupal_entity('node', 123, 'teaser') }} {# Print Branding block which was previously disabled on #} {# admin/structure/block page. #} {{ drupal_entity('block', 'bartik_branding', check_access=false) }} ``` ## Drupal Entity Form ```twig {# Print edit form for node 1. #} {{ drupal_entity_form('node', 1) }} {# Print add form for 'article' content type. #} {{ drupal_entity_form('node', values={type: 'article'}) }} {# Print user register form. #} {{ drupal_entity_form('user', NULL, 'register', check_access=false) }} ``` ## Drupal Field ```twig {# Render field_image from node 1 in view_mode "full" (default). #} {{ drupal_field('field_image', 'node', 1) }} {# Render field_image from node 1 in view_mode "teaser". #} {{ drupal_field('field_image', 'node', 1, 'teaser') }} {# Render field_image from node 1 and instead of a view mode, provide an array of display options, which is documented here: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityViewBuilderInterface.php/function/EntityViewBuilderInterface%3A%3AviewField #} {{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }} {# Render field_image from node 1 in view_mode "teaser" in English with access check disabled. #} {{ drupal_field('field_image', 'node', 1, 'teaser', 'en', FALSE) }} {# Render field_image from node 1 in view_mode "full" (default) with access check disabled (named argument). #} {{ drupal_field('field_image', 'node', 1, check_access=false) }} ``` ## Drupal Menu ```twig {# Print the top level of 'main' menu. #} {{ drupal_menu('main') }} {# Expand all menu links. #} {{ drupal_menu('main', expand=true) }} ``` ## Drupal Form ```twig {{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }} ``` ## Drupal Image ```twig {# Render image specified by file ID. #} {{ drupal_image(123) }} {# Render image specified by file UUID. #} {{ drupal_image('9bb27144-e6b2-4847-bd24-adcc59613ec0') }} {# Render image specified by file URI. #} {{ drupal_image('public://ocean.jpg') }} {# Render image using 'thumbnail' image style and custom attributes. #} {{ drupal_image('public://ocean.jpg', 'thumbnail', {alt: 'The alternative text'|t, title: 'The title text'|t}) }} {# Render image using 'thumbnail' image style with lazy/eager loading (by attribute). #} {{ drupal_image('public://ocean.jpg', 'thumbnail', {loading: 'lazy'}) }} {{ drupal_image('public://ocean.jpg', 'thumbnail', {loading: 'eager'}) }} {# Render responsive image (using a named argument). #} {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }} ``` ## Drupal Token ```twig {{ drupal_token('site:name') }} ``` ## Drupal Config ```twig {{ drupal_config('system.site', 'name') }} ``` ## Drupal Dump ```twig {# Basic usage. #} {{ drupal_dump(var) }} {# Same as above but shorter. #} {{ dd(var) }} {# Dump all available variables for the current template. #} {{ dd() }} ``` ## Drupal Title ```twig {# The title is cached per URL. #} {{ drupal_title() }} ``` ## Drupal URL ```twig {# Basic usage. #} {{ drupal_url('node/1') }} {# Complex URL. #} {{ drupal_url('node/1', {query: {foo: 'bar'}, fragment: 'example', absolute: true}) }} ``` ## Drupal Link ```twig {# It supports the same options as drupal_url(), plus attributes. #} {{ drupal_link('View'|t, 'node/1', {attributes: {target: '_blank'}}) }} {# This link will only be shown to the privileged users. #} {{ drupal_link('Example'|t, '/admin', check_access=true) }} ``` ## Drupal Messages ```twig {{ drupal_messages() }} ``` ## Drupal Breadcrumb ```twig {{ drupal_breadcrumb() }} ``` ## Drupal Breakpoint ```twig {# Make Xdebug break on the specific line in the compiled Twig template. #} {{ drupal_breakpoint() }} ``` ## Contextual Links ```twig {# Basic usage. #}