eZ Publish 5.x Custom nullable fieldtype


How can use legacy datatypes ?

Posted by Pedro Resende on 08/07/2015 22:28

Today, while working on a project for a customer, I was using a legacy extension inside a Symfony bundle.

Everything was working smoothly, except for the fact that when trying to access a content with a custom "legacy" datatype, eZ Publish was returning the following error

Exception occured. Error: Could not find 'Content' with identifier 'array ( 'id' => '384', 'languages' => NULL, 'versionNo' => 1, )' with code: 404

As you can imagine, this Exception is everything except clear... After investigating I've found out that if the "legacy" datatype was removed the content was returned.

I've concluded that a custom fieldtype was needed, but the value of that fieldtype could be ignored...

I've started by reading the Custom FieldType documentation, but in my case I'd need something to be ignored, like nullable.

After looking into ezpublish-kernel, I've found out that some "legacy" attributes are nullable, for instance "eznum", "ezidentifier", "ezinisetting", etc...

So what I did was declare the following inside my service.yml

service:
    custom.fieldType.legacydatatype:
        class: %ezpublish.fieldType.eznull.class%
        parent: ezpublish.fieldType
        arguments: [ "legacydatatype" ]
        tags:
            - {name: ezpublish.fieldType, alias: legacydatatype}
 
    custom.fieldType.legacydatatype.converter:
        class: %ezpublish.fieldType.eznull.converter.class%
        tags:
            - {name: ezpublish.storageEngine.legacy.converter, alias: legacydatatype, lazy: true, callback: '::create'}

And "magically" the content started being returned correctly :) 

I may think it's pretty simple, but I almost lost 2 hours looking for a clean way to do this.