Last active
July 16, 2024 16:20
drupal twig cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#sources | |
# https://www.drupal.org/docs/contributed-modules/twig-tweak-2x/cheat-sheet | |
# https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig | |
# Render a view | |
{{ drupal_view('who_s_new', 'block_1') }} | |
# Get the result of a view | |
{{ drupal_view_result('who_s_new', 'block_1') }} | |
# Render a Plugin Block | |
{{ drupal_block('plugin_id') }} | |
# you can get a list of plug in IDs using drush | |
# $ drush ev "print_r(array_keys(\Drupal::service('plugin.manager.block')->getDefinitions()));" | |
# Examples: | |
{{ drupal_block('system_breadcrumb_block') }} | |
{{ drupal_block('views_block:view_name-display_machine_name') }} | |
{{ drupal_block('views_exposed_filter_block:view_name-display_machine_name') }} | |
# Render a Configuration Entity Block | |
{{ drupal_entity('block', 'block_id') }} | |
# The following Drush command will list all available block entities. | |
# drush ev 'print_r(\Drupal::configFactory()->listAll("block.block."));' | |
# Render a Content Entity Block | |
{{ drupal_entity('block_content', 'content_block_id') }} | |
# Getting content block IDs is as simple as executing a single SQL query. | |
# drush sqlq 'SELECT id, info FROM block_content_field_data' | |
# Render a field | |
{{ node.field_xxxx.value }} | |
{{ order_entity.field_xxxx.value }} | |
# Render preformated HTML using RAW | |
{{ content | raw }} | |
# Conditions - Does evelate to TRUE? | |
{% if order_entity.field_external_id.value %} | |
{{ 'Order exported with ID'|t }} {{ order_entity.field_external_id.value }} | |
{% else %} | |
{{ 'Order was not exported'|t }} | |
{% endif %} | |
# Conditions - Works on node fields (tested) | |
{% if content.field_some_test_field[0] is not empty %} | |
{{ content.field_some_test_field }} | |
{% endif %} | |
# Conditions - Does evelate to TRUE? | |
{% if online == false %} | |
<p>Our website is in maintenance mode. Please, come back later.</p> | |
{% endif %} | |
# Specific/Typed checkes | |
# is defined | |
{% if foo is defined %} | |
{{ 'The Foo var exists (is defined) ' }} | |
{% endif %} | |
# is null | |
{% if foo is null %} | |
{{ 'The Foo var is not null. if it did not exist we would get an error' }} | |
{% endif %} | |
# is not empty | |
{% if foo is not empty %} | |
{{ 'foo is not empty in the following cases: any string, any number including 0, Boolean TRUE' }} | |
{{ 'foo is empty in the following cases: empty string, Boolean FALSE, null' }} | |
{{ 'Will error if does not exist' }} | |
{% endif %} | |
# For more specific cases check https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig | |
# Debug a variable: exposed_form_style_classes | |
{{ dump(exposed_form_style_classes) }} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views-exposed-form--{view-name}--{display-name}.html.twig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment