$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$contentTypeService = $this$repository->getRepository()->getContentTypeService();
$fieldTypeService = $repository->getFieldTypeService();
try
{
$content = $contentService->loadContent( 66 );
$contentType = $contentTypeService->loadContentType( $content->contentInfo->contentTypeId );
// iterate over the field definitions of the content type and print out each field's identifier and value
foreach( $contentType->fieldDefinitions as $fieldDefinition )
{
$output->write( $fieldDefinition->identifier . ": " );
$fieldType = $fieldTypeService->getFieldType( $fieldDefinition->fieldTypeIdentifier );
$field = $content->getField( $fieldDefinition->identifier );
// We use the Field's toHash() method to get readable content out of the Field
$valueHash = $fieldType->toHash( $field->value );
$output->writeln( $valueHash );
}
}
catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
// if the id is not found
$output->writeln( "No content with id $contentId" );
}
catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
{
// not allowed to read this content
$output->writeln( "Anonymous users are not allowed to read content with id $contentId" );
} |