Extending Existing Objects
Sometimes a developer wants to associate additional properties with an object without being forced to create a sub-class.
Movable Type allows any object’s schema to be extended. This allows plugins and components to insert and associate additional pieces of data, called “meta data,” with a pre-existing data type. Furthermore, these additional pieces of data become a seamless extension of the original data element, allowing that object to be sorted by and filtered by the new data element quickly and easily.
Extending an object is done by declaring the extension within the registry. For example, to add a new “is_featured” field to the core entry object for the purposes of allowing admins to designate if an entry is featured or not, one would use the following config.yaml
:
name: Example Plugin for Movable Type
id: Example
description: This plugin is an example plugin for Movable Type.
version: 1.0
schema_version: 2
object_types:
entry:
is_featured: smallint
This works because whenever a plugin attaches properties to a pre-existing object type, then that object_type
declaration acts as an extension to the pre-existing object type. In addition that additional piece of meta data is accessible directly from the associated object. For example:
use MT::Entry;
my $entry = MT::Entry->load($id);
$entry->is_featured(1);
$entry->save;
About Schema Versions
If a developer ever modifies or adds additional meta data fields to an object, then the developer should increment the schema_version
attribute of their plugin should be incremented to signal to Movable Type that some database maintenance may be required. When this happens, next time Movable Type is accessed the upgrade process will be invoked and Movable Type will automatically make changes to your database schema as necessary.