This module is now obsolete

This module won't be upgraded for Drupal 9!
For a replacement see Twig Tweak 3.x.
Twig Tweak is actively maintained and covers most of the functionality that this module has to offer.

------------------------------------------------------------------------------

Description

Some of the Filters and functions, backported from TFD7 theme engine and other things that can help a themer getting
stuff done without to much hassle :)

And some of the other filters and functions are copied from other Twig extensions, mainly for our own convenience
and to prevent from having to enable a lot of Twig extensions in a project.

All the filters are aware of Drupal Render array or Markup objects where needed. But this is not 100% watertight

Extra globals

base_url : the base URL of the site, including http or https (depending on the request being secured or not.)

Filters

Wrap filter

{{ content.title_field|wrap('h3') }}

Gives you

<h3>Title</h3>

For those days you don’t feel like creating a separate field template or use a GUI tool just to add a H3 around a
field.

Truncate filter

A straight port of the Twig Text
Extension
truncate filter

{{ 'Hello World!'|truncate(5) }}
{{ 'Hello World!'|truncate(7, true) }}
{{ 'Hello World!'|truncate(7,  false, '..') }}

Functions

drupal_view()

Embed a view direct in your template.

{{ drupal_view(view_name,display_id,arguments) }}

Return the render array for the default display of the view 'my_view' with contextual parameters /all/167

{{ drupal_view('my_view',['all',167]) }}

Return the render array for the block_1 display of the view 'my_view' with contextual parameters /all/167

{{ drupal_view('my_view','block_1',['all',167]) }}

drupal_block()

Embed a block direct in your template.

{{ drupal_block('block_delta') }}

Experimental feature, return the content of lazybuild block (since 1.7)

{{ drupal_block('block_delta',true) }}

value filter (since 1.7-beta)

Rewriting the renderValueIfNeeded method, this might cause some issues with existing templates (hence the BETA)
Added |value filter that return the raw value from a field.

Assuming that field_paragraph_color is a value list with a field-formatter set to return the key of the key|label pair the code below would set the raw key value to the color

    {% set color = content.field_paragraph_color|value %}

A combination of the |raw with the |wrap filter makes it 'easier' to transform a boolean field into either a H1 or H2

content.field_is_intro is a boolean with a field-formatter set to output 0 or 1

    {% set openingtag = (content.field_is_intro|value)?'h1':'h2' %}
    {{ field_title|wrap(openingtag) }}

Would return the title wrapped in a H1 tag if the is_intro is checked.

Debug features

Extended var dumping

Support for the Vardumper module, because that dumper is
faster then kint (Kint contains to much logic for simply digging trough variables and that makes it slow…)

  {{ dump(variable) }} 
  {{ vdm (variable) }} 

Overloads the default {{dump}} with the Vardumper version, or use {{vpm}} is you want to
dump to the Message area.

For convenience you can use {{vdp}} but that is essentially the same as doing

{{ dump(variable)
    }}



TODO add support for the vdpb (to a dedicated block) and vdpw (to a watchdog logger) dumpers.

xdebug_break()

If xdebug is enabled on your environment you can use {{ breakpoint() }} to trigger an xdebug break, and
inspect the variables in your xdebug client.

For convience the filename of the compiled template and the line of the xdebug_break() function is set to $_xdebug_caller
variable

Inspired by AjglBreakpointTwigExtension

Missing features?

Feel free to do a request in the issue queue

Project information

Releases