Forums / Developer / Extending eZContentObjectTreeNode object

Extending eZContentObjectTreeNode object

Author Message

H-Works Agency

Tuesday 29 March 2011 3:13:56 am

Hello,

How can we extend a built-in ezpublish object through the class API ?

For exemple creating {$node.like_count} ?

Is it possible through a definition function inside a custom class which extend eZcontentObjectTreeNode ?

Thanx in advance

EZP is Great

Nicolas Pastorino

Tuesday 29 March 2011 3:24:18 am

Hi !

I think this is what you are looking for : https://github.com/ezsystems/ezfind/blob/master/classes/ezfindresultnode.php

Cheers,

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye

H-Works Agency

Tuesday 29 March 2011 3:49:26 am

Hello Nicolas,

I see it look very promising but do you know if there is a documentation about how you make the link between the node custom attribute and this classes ?

Thanx

EZP is Great

Nicolas Pastorino

Tuesday 29 March 2011 4:47:10 am

Hi Martin,

The eZFindResultNode was just an example of how one can extend an existing eZPersistenObject structure in non-intrusive mode. A more generic example would be the following :

class myCustomTreeNode extends eZContentObjectTreeNode
{
    protected $customLocalAttribute;

    function myCustomTreeNode( $rows = array() )
    {
        $this->eZContentObjectTreeNode( $rows );
        $this->LocalAttributeValueList = array();
        $this->LocalAttributeNameList = array( 'my_custom_local_attribute' );
    }

    function attribute( $attr, $noFunction = false )
    {
        $retVal = null;

        switch ( $attr )
        {
            // Un-comment and implement for custom data retrieval.
            /*
            case 'my_custom_local_attribute':
            {
                $retVal = $this->customLocalAttribute;
            } break;
            */

            default:
            {
                if ( in_array( $attr, $this->LocalAttributeNameList ) )
                {
                    $retVal = isset( $this->LocalAttributeValueList[$attr] ) ? $this->LocalAttributeValueList[$attr] : null;
                }
                else
                {
                    $retVal = eZContentObjectTreeNode::attribute( $attr, $noFunction );
                }
            } break;
        }

        return $retVal;
    }

    function attributes()
    {
        return array_merge( $this->LocalAttributeNameList,
                            eZContentObjectTreeNode::attributes() );
    }

    function hasAttribute( $attr )
    {
        return ( in_array( $attr, $this->LocalAttributeNameList ) ||
                 eZContentObjectTreeNode::hasAttribute( $attr ) );
    }

    function setAttribute( $attr, $value )
    {
        switch( $attr )
        {
            // Un-comment and implement for custom data setting.
            /*
            case 'my_custom_local_attribute':
            {
                $this->customLocalAttribute = $value;
            } break;
            */

            default:
            {
                if ( in_array( $attr, $this->LocalAttributeNameList ) )
                {
                    $this->LocalAttributeValueList[$attr] = $value;
                }
                else
                {
                    eZContentObjectTreeNode::setAttribute( $attr, $value );
                }
            }
        }
    }
}

As you can see, this approach does not permit to use the underlying table to store custom values. One rather works with in-memory values here. Another option (see commented-out bits above) is to use tier storage for the new, custom attributes.

I hope this helps,
Cheers,

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye