This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Developer » Extending eZContentObjectTreeNode object
expandshrink

Extending eZContentObjectTreeNode object

Extending eZContentObjectTreeNode object

Tuesday 29 March 2011 12:13:56 pm - 3 replies

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

Tuesday 29 March 2011 12:24:18 pm

Hi !

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

Cheers,

Tuesday 29 March 2011 12:49:26 pm

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

Tuesday 29 March 2011 1:47:10 pm

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,

Modified on Tuesday 29 March 2011 1:48:08 pm by Nicolas Pastorino

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from