Share » Learn » eZ Publish » Creating Datatypes in eZ Publish 4

Creating Datatypes in eZ Publish 4

Wednesday 21 May 2008 9:14:00 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

One of the most appealing features of eZ Publish is its implementation of content classes and objects. The concept of content classes and objects in eZ Publish is borrowed from object-oriented programming: content classes are blueprints for content objects as are PHP classes for PHP objects. While PHP classes have attributes and methods, there are only attributes for content classes. Content classes can be created and modified at any time – even on production systems – without getting your hands dirty with PHP. This flexibility is possible because content classes are not hard-coded in PHP, but built from smaller pieces: datatypes.

Attributes of PHP classes can be of different types -- either the built-in primitive types like strings, integers, and boolean values or more complex types like classes and arrays.

Attributes of content classes are of datatypes. Consider a product object for a small online store. An attribute of the “Text line” datatype stores the name for your product, an attribute of the “Image” datatype provides a visual representation of the product, and an attribute of the “Price” datatype stores the price. The product ID is stored in an attribute of the “Integer” datatype.

The above datatypes are among the many datatypes included in standard eZ Publish distributions, making it possible to customize many types of content. However, sometimes your site has special needs that cannot be satisfied by one of the existing datatypes.

At Young Media Concepts we have a library of custom datatypes that we re-use for different projects. Some examples from this library are:

  • “ymcdomain” stores a domain name either with or without a subdomain. Domain names can be stored using the “Text line” datatype, but our datatype includes the functionality to validate domain strings.
  • “ymcinstantmessenger” stores users' nicknames for pre-defined IM services.
  • “ymcuniquestring” saves a plain text string, but only accepts the string if it is unique across all objects of the same content class.
  • “ymcenhancedselection” enables a content manager to select pre-defined content nodes under which a secondary location of the object will be automatically stored. As an example, we use this datatype to assign videos to categories, where categories are represented by nodes in the content node tree.

The datatype that we will create in this article will store dates together with their associated timezones. The date is represented in PHP by the DateTime extension, which comes bundled by default with PHP 5. Because PHP 4 uses 32-bit integers to store dates, it was previously not possible to enter dates before 1902 or after 2038. This limitation spurred the creation of the “ezbirthday” datatype which we actually used as a model for the new "ymcdatetime" datatype. The PHP 5 DateTime class used by our datatype not only extends the date range to many billion years by using 64-bit integers, but also stores timezone information.

Let's take a look at the Object Edit Interface of an object containing two attributes in the screenshot below, both of our “ymcdatetime” datatype. Suppose this object was of a class for car rental bookings. The two “ymcdatetime” attributes would represent the rental period, while additional attributes represent pickup and return stations, client name and car type.

Object Edit Interface using attributes of our datatype

Both of the attributes in the screenshot have seven input fields, representing the different elements of a point in time. These input fields are not saved separately inside our content class, but represented in attributes as single DateTime objects.

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)