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.
Recipe 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 ezpublish/console and dumps a content object for a given content id.
Go to the eZ publish installation
cd <ezpublish installation root>
Generate a new Bundle
php ezpublish/console generate:bundle
Now follow the instructions. This will create a bundle EzSystems/CookBookBundle in the src directory of the installation root (Note: this bundle exists already so a different namespace will be required).
Add a Command directory to the bundle
cd <ezpublish installation root>/src/EzSystems/CookbookBundle
mkdir Command
add the following class file CookbookCommand.php in the directory Command
run f.e.
php ezpublish/console cookbook:run 5
Used classes
Recipe 2 - Creating a content type group
This snippet creates a content type group for a given identifier (Full code here).
used classes
If this snipped is run with the same init code from receipt 1 we will get an UnauthorizedException.
The solution is described in the next receipt.
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
If the user is identified by other mechanisms the user also can be loaded by its id via the service method
$userService->loadUser($id)
used classes
Recipe 4 - Creating a content type
With this snipped a content type with two fields of type 'ezstring' is created. (Full code here).
used classes
Recipe 5 - Creating content
In this receipt content is created under a given parent location. It is assumed that the loaded content type is the one created in receipt 4. (Full code here).
used classes
Recipe 6 - Updating Content
In this receipt the previously created content is updated with a new title and body in the same language. (Full code here)
used classes
Recipe 7 - Translating content
It is the same code as for updating (see Receipt 6). The initial language should be set to the translation language.
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).
Recipe 9 - Performing a simple full text search
In this receipt a simple full text search is performed. (Full code here)
used classes
Recipe 10 - Performing an advanced search
In this receipt different criteria is combined using a logic and operation. The result is restricted additional (see Receipt 9) to a given content type and subtree. (Full code here)
Recipe 11 - Browsing Locations
This receipt shows how to browse a subtree starting from a given location. (Full code here)
Used classes
Recipe 12 - Adding a new location to content
This receipt shows how to add a new location to a given content object. (Full code here)
Used classes
Recipe 13 - Move or Copy Subtree
This receipt shows how to move or copy a subtree to a given parent location (Full code here)
Used classes
Recipe 14 - Hide/Unhide Location
This receipt shows how to hide/unhide a location. (Full code here)
Used classes
Recipe 15 - Deleting locations
If a content has more than one location the method
LocationService::delete(Location $location)
removes the location and if exists all descendants of the location. However the content itself is untouched as it has still other locations.
If the deleted location is the last one of the content the content itself is deleted. This applies also to all descendants of the location.
Alternatively a location can also moved to the trash via the method:
TrashService::trash(Location $location)
Recipe 16 - Deleting Content
The result of deleting content is equivalent to deleting all locations of content (see Receipt 15).
It is done via the method:
ContentService::delete(ContentInfo $contentInfo)
Recipe 17 - 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 (Receipt 5):
$contentCreateStruct->section = $sectionId
Later on sections can be assigned with the following code (Full code here):
Used classes
Recipe 18 - Viewing Content
This receipt shows how to view fields of the content int the main language.
Used classes
Other languages are accessible via the the language parameter in the method
$content->getField( $fieldDefinition->identifier, $otherlanguage)
Recipe 19 - Creating an Image
This receipt shows how to create an image content object.
Recipe 20 - Create Content with XML Text
This receipt 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.