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.

Table of Contents

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.

Creating a content type group

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

Code Block
languagephp
titlecreate content type group
linenumberstrue
        // get the services from the repsitory
        $contentTypeService = $repository->getContentTypeService();
        try
        {
            // instanciate a create struct and create the group
            $contentTypeGroupCreateStruct = $contentTypeService->newContentTypeGroupCreateStruct( $contentTypeGroupIdentifier );
            $contentTypeGroup =  $contentTypeService->createContentTypeGroup( $contentTypeGroupCreateStruct );
            print_r( $contentTypeGroup );
        }
        catch ( UnauthorizedException $e )
        {
            // react on permission denied
            $output->writeln( $e->getMessage() );
        }
        catch ( ForbiddenException $e )
        {
            // react on identifier already exists
            $output->writeln( $e->getMessage() );
        }
Info
titleused classes

 

If this snipped is run with the same init code from recipe 1 we will get an UnauthorizedException.

The solution is described in the next recipe.

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

Code Block
languagephp
titleauthentication
        // set specific user
		$userService = $repository->getUserService();
        $user = $userService->loadUserByCredentials( $user, $password );
        $repository->setCurrentUser( $user );

If the user is identified by other mechanisms the user also can be loaded by its id via the service method

$userService->loadUser($id)

Info
titleused classes


Creating a content type

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

Code Block
languagephp
titlecreate content type
        // get the repository from the di container
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );

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

        // load the admin user and set it has current user in the repository
        $user = $userService->loadUser( 14 );
        $repository->setCurrentUser( $user );

        // load the content type group
        try
        {
            $contentTypeGroup = $contentTypeService->loadContentTypeGroupByIdentifier( $groupIdentifier );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            $output->writeln( "content type group with identifier $groupIdentifier not found" );
            return;
        }

        // instanciate a ContentTypeCreateStruct with the given content type identifier and set parameters
        $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct( $contentTypeIdentifier );
        $contentTypeCreateStruct->mainLanguageCode = 'eng-GB'; // the main language code for names and description
        $contentTypeCreateStruct->nameSchema = '<title>'; // the name schema for generating the content name by using the title attribute

        // set names for the content type
        $contentTypeCreateStruct->names = array(
            'eng-GB' => $contentTypeIdentifier . 'eng-GB',
            'ger-DE' => $contentTypeIdentifier . 'ger-DE',
        );

        // set description for the content type
        $contentTypeCreateStruct->descriptions = array(
            'eng-GB' => 'Description for ' . $contentTypeIdentifier . 'eng-GB',
            'ger-DE' => 'Description for ' . $contentTypeIdentifier . 'ger-DE',
        );

        /********************** add fields ***************************************/

        // add a title field
        $titleFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( 'title', 'ezstring' );
        $titleFieldCreateStruct->names = array( 'eng-GB' => 'Title', 'ger-DE' => 'Titel' ); // set names
        $titleFieldCreateStruct->descriptions = array( 'eng-GB' => 'The Title', 'ger-DE' => 'Der Titel' ); // set descriptions
        $titleFieldCreateStruct->fieldGroup = 'content'; // set an group for the field definition
        $titleFieldCreateStruct->position = 10; // set position inside the content type
        $titleFieldCreateStruct->isTranslatable = true; // enable translation
        $titleFieldCreateStruct->isRequired = true; // require this field to set on content creation
        $titleFieldCreateStruct->isSearchable = true; // enabled to find field via content search

        $contentTypeCreateStruct->addFieldDefinition( $titleFieldCreateStruct );

        // add a body field
        $bodyFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct( 'body', 'ezstring' );
        $bodyFieldCreate->names = array( 'eng-GB' => 'Body', 'ger-DE' => 'Text' );
        $bodyFieldCreate->descriptions = array( 'eng-GB' => 'Description for Body', 'ger-DE' => 'Beschreibung Text' );
        $bodyFieldCreate->fieldGroup = 'content';
        $bodyFieldCreate->position = 20;
        $bodyFieldCreate->isTranslatable = true;
        $bodyFieldCreate->isRequired = true;
        $bodyFieldCreate->isSearchable = true;

        $contentTypeCreateStruct->addFieldDefinition( $bodyFieldCreate );

        // set the content type group for the content type
        $groups = array( $contentTypeGroup );
        try
        {
            // create the content type draft and publish it
            $contentTypeDraft = $contentTypeService->createContentType( $contentTypeCreateStruct,$groups );
            $contentTypeService->publishContentTypeDraft( $contentTypeDraft );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e )
        {
            // react on permission denied
            $output->writeln( $e->getMessage() );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e )
        {
            // react on identifier already exists
            $output->writeln( $e->getMessage() );
        }
        catch( \Exception $e )
        {
            $output->writeln( $e->getMessage() );
        }
    }
Info
titleused classes

 

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).

Code Block
languagephp
titlecreate content
        // 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();
        $contentTypeService = $repository->getContentTypeService();
        $userService = $repository->getUserService();

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

            // instanciate a location create struct from the parent location
            $locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId );

            // create a draft using the content and location create structs and publish it
            $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\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() );
        }
    }
Info
titleused classes

 

Updating Content

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

Code Block
languagephp
titleupdate content
         try
        {
            // create a content draft from the current published version
            $contentInfo = $contentService->loadContentInfo( $contentId );
            $contentDraft = $contentService->createContentDraft( $contentInfo );

            // instanciate a content update struct and set the new fields
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
            $contentUpdateStruct->initialLanguageCode = 'eng-GB'; // set language for new version
            $contentUpdateStruct->setField( 'title', $newtitle );
            $contentUpdateStruct->setField( 'body', $newbody );

            // update and publish draft
            $contentDraft = $contentService->updateContent( $contentDraft->versionInfo, $contentUpdateStruct );
            $content = $contentService->publishVersion( $contentDraft->versionInfo );
            print_r( $content );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            // react on content not found
            $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() );
        }
    }
Info
titleused classes

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 );

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 );


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() );
        }
    }

 

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 );