Last updated
Last updated
This example showcases a Custom field that's composed of links, media elements
The FieldType
is the class that will handle the storage of the field. It will be responsible for storing the data in the database and retrieving it. It will also be responsible for validating the data.
When creating the schema, there are some rules you should follow:
Links need two columns in the database, a column for the uri and another one for the title
URIs are of type varchar and length is 2048
The title of the link is of type varchar and the length is 255
You should add indexes so that Drupal can index the content
The FieldWidget
is the class that will handle the display of the field in the form. It will be responsible for displaying the field in the form and handling the data that is sent from the form.
When working with links, there are certain rules that you should follow:
The link_type attribute means internal, or external. 16 is external, 17 is internal.
We should add a element_validate attribute so that Drupal can validate if the url is correct
The states attribute helps prevent saving the form if the user is trying to submit a link title without a url. In this case we're indicating that the link_title is related to link_uri.
When working with media elements, there are certain rules you should follow:
allowed_bundles will indicate which type of media element we want to show up for the user.
The FieldFormatter
is the class that will handle the display of the field in the view. It will be responsible for displaying the field in the view.
If you want your form field to show as a media library, you should install the [Media Library Form API Element Module]()
Mutiple fields of different types in a custom field