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.

...

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

Code Block
languagephp
titlecreating an image
// get the content service from the repsitory
        $contentService = $repository->getContentService();


        // get the location service from the repsitory
        $locationService = $repository->getLocationService();

        // get the content type service from the repsitory
        $contentTypeService = $repository->getContentTypeService();

        try
        {
            // load the content type with identifier
            $contentType = $contentTypeService->loadContentTypeByIdentifier("image");

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

            // instanciate a content creation struct
            $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');

            // set title field
            $contentCreateStruct->setField('name',$name);

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

            // create a draft using the content and location create structs
            $draft = $contentService->createContent($contentCreateStruct,array($locationCreateStruct));

            // publish the content draft
            $content = $contentService->publishVersion($draft->versionInfo);

            // print out the content
            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());
        }

...