Skip to content

Instantly share code, notes, and snippets.

View AnneTee's full-sized avatar

Anne Tomasevich AnneTee

  • Wikimedia Foundation
View GitHub Profile
<?php
# This file was automatically generated by the MediaWiki 1.35.0-alpha
# installer. If you make manual changes, please keep track in case you
# need to recreate them later.
#
# See includes/DefaultSettings.php for all configurable settings
# and their default values, but don't forget to make changes in _this_
# file, not there.
#
# Further documentation for configuration settings may be found at:
@AnneTee
AnneTee / twig-cheat-sheet.md
Last active April 26, 2024 06:22
Drupal 8 Twig cheat sheet

D8 Twig cheat sheet

Conditionals

To check if a field has a value, check to see if the 0 array item is populated. Simply checking if content.field_name is truthy will not work since that variable will be defined even if the field is empty.

{% if content.field_name.0 %}
  {{ field_name }}
{% endif %}

Node: An entity (we can think of it as a page, although it can be displayed in other ways too) associated with a content type. Content editors create nodes to build the site.

Content Type: An entity type that has defined fields. Developers create content types and define the fields.

View Mode: Each content type can have various view modes. This is a way of outputting a node in a specific format with a specific set of fields. What we're calling a Preview is a view mode. So a preview is a pre-defined way of outputting a node that can be used all over the site. View modes are built by developers and are not configurable by content editors, but they can edit nodes to change the content of a preview. Another good example of a view mode is "search result" which will define, for each content type, how to display that content on the search page.

Paragraph: An entity type that has defined fields, just like a content type, except paragraphs are not nodes and do not have their own pages. Paragraph typ