We want to add the option to configure a list of authors whose tweets are allowed. To achieve this, we have to:
- implement validateValidatorConfiguration() and validate() methods in the Type class
- implement the FormMapper
- add field definition edit view
- implement toStorageFieldDefinition() and toFieldDefinition() methods in LegacyConverter

Implement validateValidatorConfiguration() and validate() methods in the Type class¶

validateValidatorConfiguration() will be called when an instance of the Field Type is added to a Content Type, to ensure that the validator configuration is valid.

For a TextLine (length validation), it means checking that both minimum and maximum length are positive integers, and that minimum is lower than maximum.

When an instance of the type is added to a Content Type, validateValidatorConfiguration() method receives the configuration for the validators used by the Type as an array. This method must return an array of error messages if errors are found in the configuration, and an empty array if no errors were found.

For the TextLine Field Type included in eZ Platform, an example array passed to validateValidatorConfiguration() method looks like this:

Earlier we validated the URL with a regular expression. Now, if the configuration of your Field Type's instance contains a TweetValueValidator key, you will check that the username in the status URL matches one of the valid authors.

We would like to offer a way to the user to input a list of authors (upon which the data will be validated). To achieve this, we will implement a FormMapper that allows us to define the necessary input field.
This is a minimal example of our FormMapper:

In our case, the TweetValueValidator expects authorList to be an array. On the other hand, our input field has TextType, so it will return a string. To solve this, we will transform data from an array to a comma-separated string and in the other way using a DataTransformer:

Next thing is to register the FormMapper as a service, so the system would know to use it to automatically add the input field to the Content Type edit form. You can read more about services and service container in the documentation. To register the FormMapper as a service, let's add the following lines to fieldtypes.yml:

toStorageFieldDefinition() converts a field definition to a legacy one using the dataText1 field (which in this example means converting it to json), and toFieldDefinition() converts a stored legacy field definition to an API field definition (which means converting it back to an array according to validation schema).

You should now be able to configure a list of authors when editing a Content Type with the Tweet Field Type.
You should also have your tweets validated in accordance to this list when you create or edit Content Item with this Field Type.