Lesson 4.4: A datatype of your own
A datatype is a new kind of field for content classes: a rating, a color, a booking period. It is the deepest extension point of the content model, with a PHP class, templates and two settings. In this lesson learners read the anatomy of a kernel datatype, let the RAD Datatype wizard write a working one into an extension, and add it to a class.
At a glance
| Course | Developers |
| Module | Module 4: Content classes and datatypes |
| Duration | 60 minutes (20 presenting, 40 exercise) |
| Level | Advanced |
| Audience | Developers |
| Prerequisites | Lesson 4.2; an administrator account on the training installation |
Learning objectives
After this lesson, learners can:
- name the parts of a datatype: the class, its registration, its templates and its storage columns;
- read
kernel/classes/datatypes/ezstring/ezstringtype.phpand find what each group of methods is for; - generate a datatype extension with Setup > RAD, Datatype wizard, and read the files it wrote;
- switch the extension on, regenerate the autoloads and add the new datatype to a class.
Trainer's notes
How to present it
Sources: RAD tools, kernel/classes/datatypes/ezstring/ezstringtype.php
and settings/content.ini [DataTypeSettings].
- The class (5 minutes). Open
ezstringtype.php:class eZStringType extends eZDataType, the constantDATA_TYPE_STRING = 'ezstring', the constructor callingparent::__construct()with the name shown in the class editor, and at the end of the fileeZDataType::register( eZStringType::DATA_TYPE_STRING, 'eZStringType' );. Walk through the method groups: validating and fetching what an editor typed, storing,objectAttributeContent()(whatcontentreturns),toString()/fromString(),sortKey(),isIndexable(). - Registration (3 minutes). The kernel finds datatypes in
RepositoryDirectories[]and, for extensions, inextension/<ext>/datatypes/<string>/when the extension is inExtensionDirectories[]; the string must also be inAvailableDataTypes[]. Showextension/eztags/settings/content.ini.append.phpas a real example. - Storage (3 minutes). An attribute row has
data_text,data_int,data_float,sort_key_intandsort_key_string; most datatypes fit there. A datatype that needs more keeps a table of its own and cleans it up. - The wizard (6 minutes). Setup > RAD, then the Datatype wizard (
/setup/datatype). Sections: The datatype (extension name, datatype identifier, class name, name in the class editor, group, summary, author, Composer vendor, version, license), What it has to do (capabilities such as Editing, Default value, Sortable, Text in and out), Where the value lives, and What will be written, a list of every file before anything is written. - Switching it on (3 minutes). Activate the extension, run
php bin/php/ezpgenerateautoloads.php -e, clear the caches; the datatype appears in the class editor's attribute list.
Common questions
Do I always need a new datatype? No. Often a class with existing datatypes, or a template override, is enough. Write a datatype when the value needs its own editing, validation or storage.
What are toString() and fromString() for? One-line text in and out: what CSV import and export, packages and
createAndPublishObject() use (lesson 4.3).
Why does the wizard ask for a license? It writes ezinfo.php, extension.xml, composer.json and a LICENSE
file; an extension without a license says nothing about how it may be used (module 7).
Pitfalls
- Identifier with the
ezprefix. New datatypes and classes use your own prefix;ezbelongs to the kernel. - Missing autoloads. The class is not found until
ezpgenerateautoloads.php -ehas run. - Templates not found. Without the design registration the datatype loads and draws nothing; the wizard writes it, a hand-written datatype must too.
- Removing a datatype that is in use. Objects with the attribute then break. Remove the attribute from classes first.
Hands-on exercise
On the training installation, signed in to the administration as an administrator, and in a shell in the installation root.
Read the head and the last lines of the kernel's text line datatype:
sed -n 30,50p kernel/classes/datatypes/ezstring/ezstringtype.phpandtail -5 kernel/classes/datatypes/ezstring/ezstringtype.php.Read a real registration:
cat extension/eztags/settings/content.ini.append.php(if eztags is installed).Open Setup > RAD and choose Datatype in the catalogue; the page Datatype wizard opens.
Fill in The datatype: extension name
mysite_rating, datatype identifiermysiterating, a class name and a name in the class editor of your choice; add yourself as author and choose a license.Under What it has to do, keep Editing and add Text in and out and Sortable.
Press Refresh preview and read What will be written: the list of files.
Write the extension with Create in extension/.
In a shell, list the files:
find extension/mysite_rating -type f | sort. Open the datatype class and itssettings/content.ini.append.php.Switch it on and refresh:
php bin/php/console exp:ini add site.ini/ExtensionSettings/ActiveExtensions mysite_rating global php bin/php/ezpgenerateautoloads.php -e php bin/php/ezcache.php --clear-allEdit your Note class from lesson 4.1, add an attribute of the new datatype and press OK.
Edit a note, fill in the new field and publish.
Put the new extension under Git:
git -C extension/mysite_rating initand a first commit.
Expected result
- Step 6 lists the datatype class, templates, the registration in
content.inianddesign.ini,ezinfo.php,extension.xml,composer.json,README.md,.gitignoreandLICENSE. - Step 8: the class file in a folder below
datatypes/, and[DataTypeSettings]withExtensionDirectories[]andAvailableDataTypes[]. - Step 10: the new datatype is offered in the attribute list of the class editor.
- Step 11: the note shows the value on the site through the generated view template.
- The trainer checks one learner's note on the projector.
Quiz
- Which call at the end of a datatype file makes it known to the kernel?
- Which two settings must name an extension's datatype?
A.
ActiveExtensions[]andDesignExtensions[]B.ExtensionDirectories[]andAvailableDataTypes[]C.RepositoryDirectories[]andExtensionRepositories[]D.ModuleList[]andViewList[] - Which methods turn a value into one line of text and back?
- Which column does the database sort on for a text datatype?
- What must happen after the extension is switched on before the class is found?
Answers
eZDataType::register( <DATA_TYPE_STRING>, '<class name>' );.- B.
[DataTypeSettings] ExtensionDirectories[]andAvailableDataTypes[]incontent.ini. toString()andfromString().sort_key_string, written bysortKey().php bin/php/ezpgenerateautoloads.php -eand clearing the caches.
Further reading
- RAD tools: the wizards, the catalogue and the health check.
- Extension points: every place the kernel can be extended.
- RAD tools security: what the wizards write and never write.
Related pages
- Module 4: Content classes and datatypes
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.