Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active March 12, 2024 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BBGuy/4cee2ccb33f3d8eb9adbfcd4c4927158 to your computer and use it in GitHub Desktop.
Save BBGuy/4cee2ccb33f3d8eb9adbfcd4c4927158 to your computer and use it in GitHub Desktop.
drupal twig cheat sheet
#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 }}
# 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) }}
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