Mugo Web main content.

Adding cross-reference data to object relations

By: Xavier Cousin | September 19, 2012 | eZ Publish development tips

The object relations datatype is one of the most powerful datatypes in eZ Publish. In some cases, it is useful to associate data not to either of the objects involved in the relation, but to the relation itself. For example, you might want to mark a person as an author on one book, a contributor on another book, and an editor on another book, all while maintaining only one record for that author. Mugo has created an extension to add cross-reference data to object relations.

Creating a new class with the "Mugo object relations" datatype

The extension Mugo Object Relations provides a new datatype:

Once an attribute of the new datatype is added, it will show the familiar options as for a standard "Object relations" attribute but with an area to configure the cross-reference information:

As shown in the screenshot above, you can allow the cross-reference data to be entered via a text field or you can define a set of options for a dropdown list.

Using a "Mugo object relations" attribute

When you create a new object that has a "Mugo object relations" attribute, you can select the cross reference information...

... or if it was configured as such, you can type it in:

Displaying the cross reference data

The data associated with the relation can be accessed in your templates via the "xrefoptionaldata" key within the "relation_list" attribute. Here is a simple code sample that fetches an object with a "Mugo object relations" attribute and lists the related object names along with the data associated with each relation:

{set $node = fetch( 'content', 'node', hash( 'node_id', 75 ))}

relations for {$node.name} object:<br/><br/>
{def $relation_node = null}
{foreach $node.data_map.relations.content.relation_list as $relation}
    {set $relation_node = fetch( 'content', 'node', hash( 'node_id', $relation.node_id ))}
    {$relation_node.name}: "{$relation.xrefoptionaldata}"<br/>
{/foreach}

eZ Find search ready

The extension comes with a simple eZ Find PHP class for storing the datatype's data in a Solr search index.

Download and contribute to Mugo Object Relations on GitHub: https://github.com/mugoweb/mugoobjectrelations