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.
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(); } }
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; } }
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.
No trackbacks.
Storing Data