eZ Community » Forums » eZ Publish 5 Platform » has_content with ez5 ?
expandshrink

has_content with ez5 ?

has_content with ez5 ?

Monday 21 January 2013 4:35:39 pm - 11 replies

Hello,

I'm trying to reproduce a 'has_content' command in a twig template, but i haven't suceed.

I look to the FieldTypes function 'isEmptyValue', but i found no way to access to a FieldType class from a FieldValue. 

I thought about accessing using the FieldTypeService, but i think it's not a good way to use it in a twig template.

So, how can i implement a 'has_content' function ?

Thanks in advance

Friday 25 January 2013 11:30:21 pm

Good question.

I would think a twig extension adding needed functions that depends on the mentioned services would be the way to go. This should be in the core bundle actually. Are you up for doing it as a pull request? happy.gif Emoticon

Saturday 26 January 2013 10:05:20 am

Good one Jonathan!

Indeed, if you feel like hacking this in, it would be lovely to share this via a pull-request.

@Andre: is there a spec (or seed thereof) in our Confluence/JIRA to which we could point Jonathan, and all other interested parties?

Keep-up guys,
Cheers,

Wednesday 13 February 2013 11:15:23 am

Hi !

Sorry for the late answer.

I would be ok to do it as a pull request.

Is there some recommandations to do it well ? Can you give me some specs ?

 

Thanks in advance

Thursday 14 February 2013 11:55:42 pm

We didn't get time to add a story to spec this up this sprint, so in the meantime here is some information to hopefully get you started:

FieldType has one "SPI"* part (interfaces, then a "FieldType.php" abstract and implementations in sub folders here), and one "API" part ( interface, impl and exposed in Public API via FieldService ).

It works like this: API wraps around SPI to make sure only "safe" methods are exposed, and never the SPI value directly.

What you need is only on the SPI FieldType interface, "isEmptyValue( $value )".
This is probably not exposed since it assumed $value is valid by calling "acceptValue( $inputValue )".

But if we assume the needs for this function usually deals with data from the database that should have been accepted already, API interface and implementation could be extended to provide something like this (this being in the implementation):

/**
     * Returns if the given $value is considered empty by the field type
     *
     * This method does not check if value is accepted or not, just if it
     * equals empty value.
     *
     * @param mixed $value
     *
     * @return boolean
     */
    public function isEmptyValue( $value )
    {
        return $this->internalFieldType->isEmptyValue( $value );
    }

 

The next step is then to create a twig function in our CoreBundle that gets FieldService via Repository via injected Container for lazy loading, and takes API Field object in a function to in the end do something like:

public function fieldIsEmpty( Field $field )
{
    $fieldTypeService = $this->container->get('repository')->getFieldTypeService();
    $fieldType = $fieldTypeService->getFieldType( $field->fieldDefIdentifier );
    return $fieldType->isEmptyValue( $field->value );
}

But for the conventions in Symfony and our Bundles for it, Jérôme will need to comment on this.

* meaning something you can extend to provide own implementation

Modified on Friday 15 February 2013 4:13:18 pm by André R

Thursday 03 October 2013 12:19:19 pm

Hi,

Is there any news about this topic please?

I'm using this at the moment :

{% if(content.getField('text').value.xml|xmltext_to_html5 is not empty  )  %}

Is it the right way?

What would be the solution for a date attribute? (I can't manage to get anything else but 01/01/1970) 

Thanks

Thursday 03 October 2013 2:27:09 pm

Hi Irma

We indeed lack a Twig helper for this. Can you please report an issue on https://jira.ez.no/browse/EZP ?

Anyway the code André showed is perfectly valid happy.gif Emoticon

Thursday 03 October 2013 2:59:15 pm

Issue created: https://jira.ez.no/browse/EZP-21680

Thursday 03 October 2013 3:07:23 pm

Thanks for reporting, it could have take some more time if you had wait for me, sorry...

Thursday 03 October 2013 3:57:30 pm

It's me who has to apologize, Irma happy.gif Emoticon. It's just that we're in a rush to build 2013.09 and I really want this to be in !

Thursday 03 October 2013 5:30:12 pm

And... ez_is_field_empty() helper implemented in master !

https://github.com/ezsystems/ezpublish-kernel/commit/1efee5d

Documentation: https://confluence.ez.no/display/EZP/ez_is_field_empty

Modified on Saturday 05 October 2013 2:11:34 pm by Jérôme Vieilledent

Thursday 05 June 2014 4:38:17 pm

Hi,

ez_is_field_empty() works perfectly thanks but I still can't manage to use it with ezdatetime datatype. 

I tried "{% if not ez_is_field_empty( content, "publish_date" ) %}" but "1970-01-01 00:00:00" still appears if no date is filled in. 

Is there a nicer solution than this please? :

{% if content.getField('publish_date').value.value.date != "1970-01-01 00:00:00" %}

Thanks,

Irma

Modified on Thursday 05 June 2014 4:38:45 pm by Irma Vrignaud

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from