eZ Community » Forums » eZ Publish 5 Platform » Suggestion: Add twig filter for check...
expandshrink

Suggestion: Add twig filter for check if field exists?

Suggestion: Add twig filter for check if field exists?

Tuesday 20 January 2015 6:49:41 pm - 5 replies

Hi. We have some twig filters like the one to check if a field is empty or not. 

How about create a new one, called maybe "ez_field_exists" that could return a boolean sayin as if the content has a field called "whatever"?

the use case for this is having a template used for several content types but some of the content types doesn't have all fields that others have. 

so, we can have something like 

Or maybe this is already possible and i'm missing something? if not already done do you think we can add this to the kernel?

Wednesday 21 January 2015 9:46:11 am

I do it so:

 {% if content.getField('field_identifier') %} ...

 

My templates have a lot of checks like that:

{% if content.getField('field_identifier') and not ez_is_field_empty( content, 'field_identifier' ) %}

 

So I'd prefer checking of field existence in ez_is_field_empty. Why actually not?

Modified on Wednesday 21 January 2015 9:46:43 am by Andrey Astakhov

Wednesday 21 January 2015 10:01:34 am

so, content.getField('field_identifier') is enough?

edit. you mean https://github.com/ezsystems/ezpu...ory/Values/Content/Content.php#L137?

Modified on Wednesday 21 January 2015 10:04:11 am by Carlos Revillo

Thursday 22 January 2015 1:22:28 pm

Hi,

Personnaly I use this one:

{% if content.fields.field_identifier is defined %} ...

But it would be cool if we can have a new filter that does this.

Thursday 22 January 2015 3:10:11 pm

Quote from Carlos Revillo :

you mean https://github.com/ezsystems/ezpu...ory/Values/Content/Content.php#L137?

Yes, that's what I meant. But I like the Soufian's variant even more.

Modified on Thursday 22 January 2015 3:56:27 pm by Andrey Astakhov

Thursday 22 January 2015 10:07:46 pm

How about this one

 
{% if content.fields['title'] is defined and ez_is_field_empty(content, 'title') is same as(false) %}
    {{ 'do this' }}
{% else %}
    {{ 'do that' }}
{% endif %}
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from