Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   eZ Publish 4.x / legacy

 
eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Excerpt Include
eZ Publish Documentation
eZ Publish Documentation

Table of Contents

Introduction

The public API will give you an easy access to the eZ Publish content repository. This repository is the core component that manages content, locations (former Nodes), sections, content types (former Content Classes), user groups, users and roles. It also provides a new, clear interface for plugging in custom field types (former Datatypes).
 
The public API is built on top of a layered architecture including a new persistence layer for abstracting the storage functionality. By using the public API, your applications will be forward compatible with future releases based on enhanced,  scalable and high-performance storage engines. Applications based on the public API are also fully backwards compatible by using the included storage engine based on the current kernel and database model.


Get Started

Recipe 1 - Setting up a sample command line symfony bundle using the public API

This recipe shows a good practice how to use the public API inside the eZ Publish 5 installation by using the symfony console component. Inside the cookbook,  all code runs inside a symfony command which is inside a symfony bundle. It is now shown how to setup a simple symfony bundle with a commandline script using the public API. The command is executable within the ezpublish/console and dumps a content object for a given content id.

...

Info
titleUsed classes

 

Creating and editing Content

The following recipes show how to create simple content. As we don't want to rely on a specific installation with predefined content types we first show how to create a content type group and a simple content type within this group. Then we will create a content object of the newly created content type. The last two recipes show how to create content containing images and xml text.

Recipe 2 - Creating a content type group

This snippet creates a content type group for a given identifier (Full code here).

...

The solution is described in the next recipe.

Recipe 3 - Setting the user for authorizing actions

By default the repository assumes the anonymous user is acting. To change this the following code can be executed

...

Info
titleused classes


Recipe 4 - Creating a content type

With this snipped a content type with two fields of type 'ezstring' is created. (Full code here).

...

Info
titleused classes

 

Recipe 5 - Creating content

In this recipe content is created under a given parent location. It is assumed that the loaded content type is the one created in recipe 4. (Full code here).

...

Info
titleused classes

 

Recipe 6 - Updating Content

In this recipe the previously created content is updated with a new title and body in the same language. (Full code here)

...

Info
titleused classes

Recipe 7 - Translating content

It is the same code as for updating (see recipe 6). The initial language should be set to the translation language.

Code Block
languagephp
titletranslating
            // set language for new version
            $contentUpdateStruct->initialLanguageCode = $newLanguage;
            // set fields
            $contentUpdateStruct->setField( 'title', $newtitle );
            $contentUpdateStruct->setField( 'body', $newbody );

Recipe 8 - Multiple translations at once

It is possible to to make an update in content  or create content with more than one language. But there is a restriction - only one language can be assigned to the newly created version (which is displayed in the 4.x admin GUI in the translations column).

Code Block
languagephp
titleupdate multiple languages
            // set one language for new version
            $contentUpdateStruct->initialLanguageCode = 'fra-FR';
			// set fields for german - here the language has to be passed in third argument
            $contentUpdateStruct->setField( 'title', $newgermantitle, 'ger-DE' );
            $contentUpdateStruct->setField( 'body', $newgermanbody, 'ger-DE' );
            // set fields for french
            $contentUpdateStruct->setField( 'title', $newfrenchtitle );
            $contentUpdateStruct->setField( 'body', $newfrenchbody );


Recipe 9 - Creating Content containing an image

This recipe shows how to create an  content object containing an image. (Full code here)

Code Block
languagephp
titlecreating an image
        // get the repository from the di container
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );

        // get the services from the repsitory
        $contentService = $repository->getContentService();
        $locationService = $repository->getLocationService();
        $userService = $repository->getUserService();
        $contentTypeService = $repository->getContentTypeService();

        // load the admin user and set it has current user in the repository
        $user = $userService->loadUser( 14 );
        $repository->setCurrentUser( $user );
        try
        {
            // load the image content type and instanciate a content create struct
            $contentType = $contentTypeService->loadContentTypeByIdentifier( "image" );
            $contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' );
            $contentCreateStruct->setField('name',$name); // set name field

            // set image file field
            $value = new \eZ\Publish\Core\FieldType\Image\Value(
                array(
                    'path' => $file,
                    'fileSize' => filesize($file),
                    'fileName' => basename($file),
                    'alternativeText' => $name
                )
            );
            $contentCreateStruct->setField( 'image' , $value );

            // instanciate a location create struct and create and publish the content
            $locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId );
            $draft = $contentService->createContent( $contentCreateStruct,array( $locationCreateStruct ) );
            $content = $contentService->publishVersion( $draft->versionInfo );
            print_r( $content );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            // react on content type or location not found
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException $e )
        {
            // react on remote id exists already
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e )
        {
            // react on a field is not valid
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\ContentValidationException $e )
        {
            // react on a required field is missing or empty
            $output->writeln( $e->getMessage() );
        }
    }

 

Recipe 10 - Create Content with XML Text

This recipe shows how to create content with xml text. As content type the folder is used where the description is filled with xml. It is also shown how to embed the previously created image in the description. The image Id is given by a command line argument.

Code Block
languagephp
titleworking with xml text
             // load a folder content type and instanciate a content creation struct
            $contentType = $contentTypeService->loadContentTypeByIdentifier( "folder" );
            $contentCreateStruct = $contentService->newContentCreateStruct( $contentType, "eng-GB" );
            $contentCreateStruct->setField( "name", $name ); // set name of the folder
            $xmltext = "<?xml version='1.0' encoding='utf-8'?><section><paragraph>This is a <strong>image test</strong></paragraph>
                        <paragraph><embed view='embed' size='medium' object_id='$imageId'/></paragraph></section>";
            $contentCreateStruct->setField( "description", $xmltext ); // set description of the folder

            // instanciate a location create struct and create and publsidh the content
            $locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId );
            $draft = $contentService->createContent( $contentCreateStruct, array( $locationCreateStruct ) );
            $content = $contentService->publishVersion( $draft->versionInfo );
            print_r( $content );

 

Browsing, Finding, Viewing

Recipe 11 - Viewing Content

This recipe shows how to view fields of the content int the main language.

...

$content->getField( $fieldDefinition->identifier, $otherlanguage)

 

Recipe 12 - Browsing Locations

 

This recipe shows how to browse a subtree starting from a given location. (Full code here)

...

Info
titleUsed classes

 

Recipe 13 - Viewing Content Meta Data

This recipe shows how to read content meta data: Locations, UrlAliases, Relations, Versions, Contenttype, Section, Owner, RemoteId, Several Timestamps. (Full code here)

...

Code Block
languagenone
Locations:
  Location: /1/2/103/  URLAlias: /testrootfolder
  Location: /1/2/94/106/  URLAlias: /Partner-Section/testrootfolder
Relations:
  Relation of type EMBED to content Multivariate Testing
  Relation of type EMBED to content test
Name: testrootfolder
Type: folder
Last modified: 2012-11-12
Published: 2012-11-06
RemoteId: 32d63a1493208f6d0e2d40ab25749af4
Main Language: eng-US
Always avaialble: Yes
Owner: Administrator User
Section: Standard
Version 1: 'Administrator User' ARCHIVED eng-US
Version 2: 'Administrator User' PUBLISHED eng-US


 

Recipe 14 - Performing a simple full text search

In this recipe a simple full text search is performed. (Full code here)

...

Info
titleused classes

 

Recipe 15 - Performing an advanced search

In this recipe different criteria is combined using a logic 'AND' operation. The result is restricted additional (see recipe 9) to a given content type and subtree. (Full code here)

Code Block
languagephp
        // create the query with three critions and print out the result
        $query = new \eZ\Publish\API\Repository\Values\Content\Query();
        $criterion1 = new Criterion\FullText( $text );
        $location = $locationService->loadLocation( $locationId );
        $criterion2 = new Criterion\Subtree( $location->pathString ); // restrict results to belong to the given subtree
        $criterion3 = new Criterion\ContentTypeId( $contentTypeId ); // restrict to the given content type
        $query->criterion = new Criterion\LogicalAND(
                array( $criterion1, $criterion2, $criterion3 )
        );

 

 

Working with locations

Recipe 16 - Adding a new location to content

This recipe shows how to add a new location to a given content object. (Full code here)

...

Info
titleUsed classes

Recipe 17 - Move or Copy Subtree

This recipe shows how to move or copy a subtree to a given parent location (Full code here)

...

Info
titleUsed classes

Recipe 18 - Hide/Unhide Location

This recipe shows how to hide/unhide a location. (Full code here)

...

Info
titleUsed classes

Recipe 19 - Deleting locations

If a content has more than one location the method

...

TrashService::trash(Location $location)


Other Recipes

Recipe 20 - Deleting Content

The result of deleting content permanently  is equivalent to deleting all locations of content (see recipe 15).

...

ContentService::delete(ContentInfo $contentInfo)

Recipe 21 - Assinging section to content

On creation of content  the section of the parent location's content is assigned by default to the new content. However it is possible to assign a specific section on creation by setting it in the ContentCreateStruct (recipe 5):

...