Skip to content

Instantly share code, notes, and snippets.

@dspe
Created May 14, 2014 19:49
Show Gist options
  • Save dspe/152c27d0dc8f71b421be to your computer and use it in GitHub Desktop.
Save dspe/152c27d0dc8f71b421be to your computer and use it in GitHub Desktop.
Controller example
<?php
namespace Ez\ExampleBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Serializer;
class ExportController extends Controller
{
public function exampleAction( $contentId )
{
$serializer = new Serializer(array(),array( 'xml' => new XmlEncoder()));
// language is optional
$language = array( "fre-FR" );
$contentVersion = $this->getRepository()->getContentService()->loadContent( $contentId, $language );
$fields = $contentVersion->getFields();
$contentType = $this->getRepository()->getContentTypeService()->loadContentType(
$contentVersion->getVersionInfo()->getContentInfo()->contentTypeId
);
// Transform to array ( fieldDef => value)
$fieldsList = array();
foreach( $fields as $field )
{
$fieldsList[ $field->fieldDefIdentifier ] = $field->value;
}
$finalResult = array();
foreach( $contentType->fieldDefinitions as $fieldDefinition )
{
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier;
if ( $fieldTypeIdentifier == "ezxmltext" )
{
// We transform xml to html with html5 converter
$htmlConverter = $this->container->get('ezpublish.fieldType.ezxmltext.converter.html5');
$value = $htmlConverter->convert( $fieldsList[$fieldDefinition->identifier]->xml );
}
else
{
$value = $fieldsList[$fieldDefinition->identifier];
}
$finalResult[] = array( "identifier" => $fieldTypeIdentifier, "value" => $value );
echo "<hr />";
}
var_dump($finalResult);
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment