This project is not covered by Drupal’s security advisory policy.

Extension module to Markup Field module to use Twig in Markup fields.

How to use:

  1. Install this module like any other, see this documentation.
  2. Set 'Administer markup fields' permission for trusted roles
  3. Add a markup field to your entity
  4. Use twig syntax in the markup field
  5. Select "Markup Twig" as
    • Field widget (admin/structure/types/manage/XXX/form-display)
    • Field formatter (admin/structure/types/manage/XXX/display) in your field administration. Otherwise twig will be rendered as plain text.

Dependencies:

Typical use-cases & examples

Still TODO, see #3026770: Provide typical examples and use-cases on the module page
You may help and suggest snippets:

Display all available twig variables

{{ kint() }}
Requires twig debug to be enabled, see Debugging Twig templates
We're planning to provide a smarter solution: #3026576: Integrate with codemirror_editor

Get URL from file / image field

{{ node.field_name.0.entity.uri.value }}
("node" may be "user", "term", ... it's the dynamic name of the entity type)

Render image from image field (requires Twig Tweak)

{{ node.field_name.0.entity.uri.value|image_style('thumbnail') }}
("node" may be "user", "term", ... it's the dynamic name of the entity type)

Render an other field from the entity (requires Twig Tweak)

{{ node.field_name|view }}
("node" may be "user", "term", ... it's the dynamic name of the entity type)

Render entity title

{{ node.title }}
("node" may be "user", "term", ... it's the dynamic name of the entity type)

Extensions

Favourite: You may want to use the famous Twig Tweak module to enhance twig with further filters and functionalities, e.g. {{ node.field_tags|view}} (see https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x)
Alternatively: Twig Render This may be very useful if you'd like to render a field from the given entity by simply writing: {{ node.field_tags | renderThis }} (see https://www.drupal.org/docs/8/modules/twig-render-this/tutorial)
Additionally: Twig Extensions: Adds further filters and functionalities from the twig extensions lib:

Alternatives:

  • Field Token Value: Could be used for simple cases where Computed Field is too heavy.
  • Computed field: The sladge hammer for dynamic fields
  • Extra field: Provides extra fields programmatically via plugin.
    Extra fields do not store data and do not have a field widget. Extra fields typically combine existing entity data and format it for display.
  • Twig field: Provides twig syntax field in the entity create / edit form (like body field, not on meta-level).
  • Filter Twig: Provides an input filter with twig support

Important:

This module adds a permission "Administer markup fields". Do not allow untrusted users to edit Twig templates as it may cause security implications. Hopefully this functionality will be integrated into Markup field one day #3026539: Add "administer markup fields" permission.


Development proudly sponsored by German Drupal Friends & Companies:

webks: websolutions kept simple (https://www.webks.de)
and
DROWL: Drupalbasierte Lösungen aus Ostwestfalen-Lippe (OWL), Germany (https://www.drowl.de)

Supporting organizations: 
proudly developed this module for the community!

Project information

Releases