ezxmltext: How to store and ouput your content?

One of many questions on ez.no forums is how to store and output data from ezxmltext datatype using PHP code. Here are some examples which you can use in your eZ publish scripts.

Input

Example with eZSimplifiedXMLInputParser

include_once ('lib/ezutils/classes/ezfunctionhandler.php');
// Fetch example node
$node = eZFunctionHandler::execute( 'content','node', array( 'node_id' => 2) );
$object =& $node->object();
 
include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinput.php' );
include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinputparser.php' );
include_once( 'kernel/classes/datatypes/ezxmltext/ezxmltexttype.php' );
 
// Some example content
$XMLContent = "<p>some text <b>this is bold text</b> some text <i>some text</i></p>";
 
$parser = new eZSimplifiedXMLInputParser( $object->attribute( 'id' ) );
$parser->setParseLineBreaks( true );
 
$document = $parser->process( $XMLContent );
 
// Create XML structure
$xmlString = eZXMLTextType::domString( $document );
 
// Loop through  contentobject attributes
foreach( $object->contentObjectAttributes() as $contentObjectAttribute )
{
    // Store only in  short_description attribute
    if ( $contentObjectAttribute->attribute( 'contentclass_attribute_identifier' ) == 'short_description' )
    {
        // Store XML structure in ezxmltext attribute
        $contentObjectAttribute->setAttribute( 'data_text', $xmlString );
        $contentObjectAttribute->store();
    }
}

Output

Example 1
include_once ('lib/ezutils/classes/ezfunctionhandler.php');
// Fetch example node
$node = eZFunctionHandler::execute( 'content','node', array( 'node_id' => 2) );
$object =& $node->object();
 
// Loop through  contentobject attributes
foreach( $object->contentObjectAttributes() as $contentObjectAttribute )
{
    // Get only content from short_description attribute
    if ( $contentObjectAttribute->attribute( 'contentclass_attribute_identifier' ) == 'short_description' )
    {
        include_once('kernel/classes/datatypes/ezxmltext/handlers/output/ezxhtmlxmloutput.php');
        // Get XML content from contentobject attribute
        $XMLContent = $contentObjectAttribute->attribute( 'data_text' );
        $outputHandler = new eZXHTMLXMLOutput( $XMLContent, false, $contentObjectAttribute );
        $htmlContent =& $outputHandler->outputText();
        echo $htmlContent;
    }
}
Example 2
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
 
// {def $node=fetch( 'content', 'node', hash( 'node_id', 2 ) )}
$node =& eZContentObjectTreeNode::fetch( 2 );
 
// {$node.object.data_map}
$object =& $node->object();
$dataMap =& $object->dataMap();
 
// {$node.object.data_map.short_description.content.output.output_text}
$shortDescription =& $dataMap['short_description']->content();
$shortDescriptionOutput =& $shortDescription->attribute('output');
$shortDescriptionOutputText = $shortDescriptionOutput->attribute('output_text');
 
echo $shortDescriptionOutputText;

See also blog post "eZ publish content object attributes output rendering" and learn how to access eZ publish content objects attributes of all available datatypes.

Comments

no img tag, but embed tag

Domenico,
it is possible to add images. Create them as an object of type image under the media library tree, then add an embed tag with the node or object id to your xml.

See the discussion at http://ez.no/developer/forum/developer/how_to_import_html_into_an_ezxmlobject
and note the part about the XML tags that are allowed in ezxmlobjects..

Best wishes,
Rainer

Add tag img (<img src="..." />)

Hi,
is possible add tag img?
I try to use <literal><img ...../></literal> but not insert image.
I import article from wordpress site and is not possible add image.
thanks

Storing Data

When a user clicks on a link I would like to update a single integer attribute, to be used as a rating, where would I put the above input php? How would I make the link run that php? Is there a better way todo this?

Many Thanks... in advance

Thanks

Thaks. This is very useful Luke :-)

Appreciation?

My Amazon.com Wish List

eZ publish™ copyright © 1999-2012 eZ systems as