Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I thought it would be useful to have an issue to track functions/filters that themers miss when working on the conversion and perhaps later on.
And just another thought: we will probably need a way to let other modules provide additional functions/filters. But we can save that for later.
We'll need to decide weather we want things like t or count to be filters or functions in twig.
In addition to the functions and filters already provided by twig, at the moment Drupal twig.engine maps the following:
Current state
Functions:
- render_var
- url
- path
- link
- file_url
- attach_library
- active_theme_path
- active_theme
Filters:
- t
- trans
- placeholder
- drupal_escape
- safe_join
- without
- clean_class
- clean_id
- render
- format_date
Candidates
Drupal functions to Twig functions
- attributes/drupal_attributes
- render
- format_interval
PHP functions to Twig functions
- unset
Drupal functions to Twig filters
Excluded/Replaced
show()/hide()
become the|without
filter. @see https://www.drupal.org/node/2212845
Related issues
#1778968: Convert theme_table to Twig - _theme_table_cell
Comments
Comment #1
daggerhart CreditAttribution: daggerhart commentedVery late to the game here, sorry if I'm posting this out of place. I'm excited what I am learning about Twig, and want to become involved and help.
Twig filters look incredibly useful, especially since they chain. To answer your questions, I would like to see t() and count() as filters.
Chaining with the replace filter.
{{ node.title|replace({"foo":"bar"})|replace({"bar":"buz"}) }}
current t()
twig filter |t()
{{ "2 days in the future is @date"|t({ "@date" : '+2days'|date('M-d-Y') }) }}
Seems reasonable to me.
count()
With count, there is already a filter in twig for length. http://twig.sensiolabs.org/doc/filters/length.html
Is there another need count() fulfills that |length doesn't ?
Example iterate using length:
Comment #2
jenlamptonIn Munich, we decided to always use filters over functions, but I'm hesitant to make that decision for one major reason.
Twig functions work just like PHP functions, and Drupal is built on PHP. The learning curve to becoming a Drupal developer starts in the theme layer, and eventually even theme devs will need to know PHP to use preprocess functions. If we start by giving people only filters they'll need to learn yet another syntax.
Right now, the twig engine provides all PHP functions both as functions and as filters. We may decide to change this later on :)
Also - Moving to the active sandbox.
Comment #3
jenlamptonadding tags
Comment #4
jenlamptonComment #5
Fabianx CreditAttribution: Fabianx commentedThe syntax of:
'Hello'|t(...)
is very nice, but: Can the translation scripts _find_ and detect these.
That might be a reason to stick to t('Hello', ...).
Comment #5.0
Fabianx CreditAttribution: Fabianx commentedadding issue summary from duplicate issue
Comment #6
joelpittetConsider adding
format_plural
to twig filters, or better yethttp://twig.sensiolabs.org/doc/extensions/i18n.html
With plural and trans blocks
Comment #7
steveoliver CreditAttribution: steveoliver commentedMoving this to the core queue with lower priority and marking as task.
Comment #8
steveoliver CreditAttribution: steveoliver commentedUpdating title. :/
Comment #9
Sutharsan CreditAttribution: Sutharsan commentedIMO twig documentation is not very clear about what the difference between a function and a filter is. My conclusion is that a Twig filter uses something that can already be outputted and converts it into desired output.
For example:
What is left of the pipe symbol (
name
) is meaning full output.While a Twig function uses or acts upon its parameter. The function will return output, but the parameter is (usually) no valid output. This discussion helped me understanding this difference between functions and filters.
From the functions mentioned in the issue summary,
t
,show
andhide
fit in the filter definition:Comment #10
ksenzeeRe #5, the issue for that is #2011442: Support for Drupal 8 twig t filter translatables. It shouldn't be too hard to pick up the new syntax - just a new regex or two. Certainly not a big enough deal that Twig should compromise on the right thing to do here.
Comment #10.0
ksenzeeAdd current state
Comment #11
joelpittetUpdated the current state.
Comment #12
sergeypavlenko CreditAttribution: sergeypavlenko commentedHi, all
Now function "hide" not work. There are plans to add it?
I'm use function so:
{% hide(content.links) %}
{% content|hide(content.links) %}
{% content|hide('content.links') %}
Comment #13
star-szrPlease see https://www.drupal.org/node/2212845.
Comment #14
joelpittetRepurposed this issue to track things as a plan.
Comment #15
joelpittetComment #16
joelpittetComment #17
joelpittet