You are here

public function TwigExtension::getFilters in Twig Tweak 8.2

Same name and namespace in other branches
  1. 8 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::getFilters()

File

src/TwigExtension.php, line 251

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function getFilters() {
  $filters = [
    // - Token Replace -
    //
    // @code
    //   {# Basic usage. #}
    //   {{ '<h1>[site:name]</h1><div>[site:slogan]</div>'|token_replace }}
    //
    //   {# This is more suited to large markup (requires Twig >= 1.41). #}
    //   {% apply token_replace %}
    //     <h1>[site:name]</h1>
    //     <div>[site:slogan]</div>
    //   {% endapply %}
    // @endcode
    new TwigFilter('token_replace', [
      $this,
      'tokenReplaceFilter',
    ]),
    // - Preg Replace -
    //
    // @code
    //   {{ 'Drupal - community plumbing!'|preg_replace('/(Drupal)/', '<b>$1</b>') }}
    // @endcode
    //
    // For simple string interpolation consider using built-in 'replace' or
    // 'format' Twig filters.
    new TwigFilter('preg_replace', [
      $this,
      'pregReplaceFilter',
    ]),
    // - Image Style -
    //
    // @code
    //  {{ 'public://images/ocean.jpg'|image_style('thumbnail') }}
    // @endcode
    new TwigFilter('image_style', [
      $this,
      'imageStyle',
    ]),
    // - Transliterate -
    //
    // @code
    //   {{ 'Привет!'|transliterate }}
    // @endcode
    new TwigFilter('transliterate', [
      $this,
      'transliterate',
    ]),
    // - Check Markup -
    //
    // @code
    //   {{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }}
    // @endcode
    new TwigFilter('check_markup', [
      $this,
      'checkMarkup',
    ]),
    // - Format Size -
    //
    // @code
    //   {{ 12345|format_size() }}
    // @endcode
    new TwigFilter('format_size', 'format_size'),
    // - Truncate -
    //
    // @code
    //   {{ 'Some long text'|truncate(10, true) }}
    // @endcode
    new TwigFilter('truncate', [
      $this,
      'truncate',
    ]),
    // - View -
    //
    // @code
    //   {# Do not put this into node.html.twig template to avoid recursion. #}
    //   {{ node|view }}
    //   {{ node|view('teaser') }}
    //
    //   {{ node.field_image|view }}
    //   {{ node.field_image[0]|view }}
    //   {{ node.field_image|view('teaser') }}
    //   {{ node.field_image|view({settings: {image_style: 'thumbnail'}}) }}
    // @endcode
    new TwigFilter('view', [
      $this,
      'view',
    ]),
    // - With -
    //
    // @code
    //   {# Set top level value. #}
    //   {{ content.field_image|with('#title', 'Photo'|t) }}
    //
    //   {# Set nested value. #}
    //   {{ content|with(['field_image', '#title'], 'Photo'|t) }}
    // @endcode
    new TwigFilter('with', [
      $this,
      'with',
    ]),
    // - Children -
    //
    // @code
    //   <ul>
    //     {% for tag in content.field_tags|children %}
    //       <li>{{ tag }}</li>
    //     {% endfor %}
    //   </ul>
    // @endcode
    new TwigFilter('children', [
      $this,
      'children',
    ]),
    // - File URI -
    //
    // When field item list passed the URI will be extracted from the first
    // item. In order to get URI of specific item specify its delta explicitly
    // using array notation.
    // @code
    //   {{ node.field_image|file_uri }}
    //   {{ node.field_image[0]|file_uri }}
    // @endcode
    //
    // Media fields are fully supported including OEmbed resources, in which
    // case it will return the URL to the resource, similar to the `file_url`
    // filter.
    // @code
    //   {{ node.field_media|file_uri }}
    // @endcode
    //
    // Useful to apply the `image_style` filter to Media fields.
    // Remember to check whether a URI is actually returned.
    // @code
    //   {% set media_uri = node.field_media|file_uri %}
    //   {% if media_uri is not null %}
    //     {{ media_uri|image_style('thumbnail') }}
    //   {% endif %}
    // @endcode
    new TwigFilter('file_uri', [
      $this,
      'fileUri',
    ]),
    // - File URL -
    //
    // For string arguments it works similar to core file_url() Twig function.
    // @code
    //   {{ 'public://sea.jpg'|file_url }}
    // @endcode
    //
    // When field item list passed the URL will be extracted from the first
    // item. In order to get URL of specific item specify its delta explicitly
    // using array notation.
    // @code
    //   {{ node.field_image|file_url }}
    //   {{ node.field_image[0]|file_url }}
    // @endcode
    //
    // Media fields are fully supported including OEmbed resources.
    // @code
    //   {{ node.field_media|file_url }}
    // @endcode
    new TwigFilter('file_url', [
      $this,
      'fileUrl',
    ]),
    // - Entity translation -
    //
    // Gets the translation of the entity for the current context.
    // @code
    //   {{ node|translation }}
    // @endcode
    //
    // An optional language code can be specified.
    // @code
    //   {{ node|translation('es') }}
    // @endcode
    new TwigFilter('translation', [
      $this,
      'entityTranslation',
    ]),
  ];
  if (Settings::get('twig_tweak_enable_php_filter')) {

    // - PHP -
    //
    // PHP filter is disabled by default. You can enable it in settings.php
    // file as follows:
    // @code
    //   $settings['twig_tweak_enable_php_filter'] = TRUE;
    // @endcode
    //
    // @code
    //   {{ 'return date('Y');'|php }}
    // @endcode
    //
    // Using PHP filter is discouraged as it may cause security implications.
    // In fact it is very rarely needed.
    //
    // The above code can be replaced with following.
    // @code
    //   {{ 'now'|date('Y') }}
    // @endcode
    $filters[] = new TwigFilter('php', [
      $this,
      'phpFilter',
    ], [
      'needs_context' => TRUE,
    ]);
  }
  return $filters;
}