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.
Comment: Migrated to Confluence 5.3

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 Nodesnodes in eZ Publish 4.x), sections, content types (former Content Classes in eZ Publish 4.x), users, user groups, users and roles. It also provides a new, clear interface for plugging in custom field types (former Datatypesdatatypes in eZ Publish 4.x).

 
The public API is built on top of a layered architecture, including a new persistence layer for abstracting the storage functionalitypersistence API that abstracts storage. By using the public API, you are sure that your applications code 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.

Receipt 1 - Setting up a sample commandline symfony bundle which uses the public API

This receipt shows how to setup a simple symfony bundle with a commandline script using the public API. The command is executable within the app/console and dumps a content object for a given content id.

Go to the eZ publish installation

cd <ezpublish installation root>

Generate a new Bundl

follow the instructions 

add a Command directory to the bundle

add the following class file

 

Code Block
languagephp
titlemy first command
<?php
/**
 * File containing the CookbookCommand class.
 *
 * @copyright Copyright (C) 2012 eZ Systems AS. All rights reserved.
 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 * @version //autogentag//
 */
namespace eZ\Publish\Bundle\CookbookBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class CookbookCommand extends ContainerAwareCommand
{
    protected function configure()
    {
         $this->setName( 'cookbook:run' )
               ->setDefinition(
                   array(
                       new InputArgument( 'contentId', InputArgument::REQUIRED, 'An existing content id' )
                   )
                );
   }

    protected function execute( InputInterface $input, OutputInterface $output )
    {
        $contentId = $input->getArgument( 'contentId' );

        /** @var $repository \eZ\Publish\API\Repository\Repository */
        $repository = $this->getContainer()->get( 'ezpublish.api.repository' );

        $contentService = $repository->getContentService();
        try
        {
            print_r( $contentService->loadContentInfo( $contentId ) );
        }
        catch( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
        {
            $output->writeln( "No content with id $contentId" );
        }
    }
}

 

 

run php app/console cookbook:run

Receipt 2 - Creating a content type group and a simple content type

Code Block
languagephp
// start a transaction
$repository->beginTransaction();
try 
{
    // create a new content type group
    $contentTypeGroupInput = new ezp\Content\Type\Group\Concrete();
    $contentTypeGroupInput->identifier = "API_ContentTypeGroup";
    // multi language names and description are supported in future
    // due to a bug it has to be provided by now for names
    $contentTypeGroupInput->name = array(
        'eng-GB' => ('API_CreateGroupTest'),
        'always-available' => 'eng-GB'
    );
    // create the group 
    $contentTypeGroup = $contentTypeService->createGroup(
        $contentTypeGroupInput
    );
    
    // create a new content type
    $contentTypeInput = new ezp\Content\Type\Concrete();
    $contentTypeInput->identifier = "NewContentType";
 
    // thiss will be possible in upcoming builds
    // $contentType->remoteId = "myRemoteId-1234567890";
    $contentTypeInput->defaultAlwaysAvailable = true;
    $contentTypeInput->description = array(
        'eng-GB' => ( 'This is a new content type' ),
        'always-available' => 'eng-GB'
    );
    $contentTypeInput->isContainer = false;
    $contentTypeInput->name = array(
        'eng-GB' => ( 'NewContentType' ),
        'always-available' => 'eng-GB'
    );
    $contentTypeInput->nameSchema = "<name>";
    $contentTypeInput->urlAliasSchema = "<name>";
    // has to be changed to a language code not id
    $contentTypeInput->initialLanguageId = 2;
    $fields = array();
 
    // add field definitions
    $fieldDefinition = new FieldDefinition($contentType, "ezstring");
    $fieldDefinition->setDefaultValue(
        new \ezp\Content\FieldType\TextLine\Value( "New Name" )
    );
    $fieldDefinition->identifier = "name";
    $fieldDefinition->name = array(
        'eng-GB' => ( 'NameField' ),
        'always-available' => 'eng-GB'
    );
    $fieldDefinition->isRequired = true;
    $fieldDefinition->isSearchable = true;
    $fieldDefinition->fieldGroup = "TestCategory";
    $fieldDefinition->isTranslatable = true;
    $fieldDefinition->position = 1;
    
    // add a string length validator         
    $strLenValidator = new StringLengthValidator();
    $strLenValidator->initializeWithConstraints(
            array( 'maxStringLength' => 20, 'minStringLength' => 4 )
    );
    $fieldDefinition->setValidator($strLenValidator);
            
    $fields[] = $fieldDefinition;
 
    // add second field definitions
    $fieldDefinition = new FieldDefinition($contentType, "ezinteger");
    $fieldDefinition->setDefaultValue(
        new \ezp\Content\FieldType\Integer\Value( 2 )
    );
    $fieldDefinition->identifier = "Number";
    $fieldDefinition->name = array(
        'eng-GB' => 'NumberField',
        'always-available' => 'eng-GB'
    );
    $fieldDefinition->isRequired = false;
    $fieldDefinition->isSearchable = false;
    $fieldDefinition->fieldGroup = "MetaInfo";
    $fieldDefinition->isTranslatable = false;
    $fieldDefinition->position = 2;
 
    $fields[] = $fieldDefinition;
    
    // creates the content type in the given group and fields and publishes it
    $contentType = $contentTypeService->createAndPublish(
        $contentType, array( $contentTypeGroup ), $fields
    );
     
    // add a content object of type NewContentType
 
 
    $repository->commit();
}
catch(Exception $e) 
{
    $repository->rollback();
    // do handling here
}

 

Receipt 3  Update content type

This receipt shows how to read and update a content type
 
$contentType = $contentService->loadByIdentifier("NewContentType");
 
 
 TO BE CONTINUED

 

About this Cookbook

The objective of this Public API Cookbook is to progressively guide you through useful, everyday business logic, using the API in concrete recipes: obtaining a Location from a Content, fetching a set of Content, creating a User, and so on.

For each recipe, newly introduced elements will be explained in detail, including the required API components (services, value objects...). 

Info

Did we forget an important recipe ?

Let us know in the comments.

Suggested tools

In addition to this cookbook, we strongly recommend that you use a full featured PHP IDE, such as Eclipse or PhpStorm. It will provide you information on every piece of code you use, including objects and classes documentation. We have paid very careful attention to PHPDoc throughout this API, and such a tool is a very valuable help when using this API.

On top of this, generated public API documentation can be found online, in various formats:

Children Display