In the following recipes, you will see how to create Content, including complex fields like XmlText or Image. You will also learn about ContentType manipulation.
Identifying to the repository with a login and a password
As seen earlier, the Repository executes operations with a user's credentials. In a web context, the currently logged in user is automatically identified. In a command line context, you need to manually log a user in. We have already seen how to manually load and set a user using its ID. If you would instead like to identify a user using his username and password, this can be achieved as follows.
We will now see how to create Content using the Public API. This example will work with the default Folder (ID 1) Content Type from eZ Publish.
We first need the required services. In this case:
As explained in the Public API Basics, Value Objects are read only. Dedicated objects are provided for Update and Create operations: structs, like ContentCreateStruct or UpdateCreateStruct. In this case, we need to use a ContentCreateStruct.
We first need to get the
ContentType we want to create a
Content with. To do so, we use
ContentTypeService::loadContentTypeByIdentifier(), with the wanted
ContentType identifier, like 'article'. We finally get a ContentTypeCreateStruct using
ContentService::newContentCreateStruct(), providing the ContentType and a Locale Code (eng-GB).
Setting the fields values
Using our create struct, we can now set the values for our Content's fields, using the
setField() method. For now, we will just set the title.
setField() for a TextLine Field simply expects a string as input argument. More complex FieldTypes, like Author or Image, expect different input values.
ContentCreateStruct::setField() method can take several type of arguments.
In any case, whatever the FieldType is, a Value of this type can be provided. For instance, a TextLine\Value can be provided for a TextLine\Type. Depending on the FieldType implementation itself, more specifically on the fromHash() method every FieldType implements, various arrays can be accepted, as well as primitive types, depending on the Type.
Setting the Location
In order to set a Location for our object, we must instanciate a
LocationCreateStruct. This is done with LocationService::newLocationCreateStruct(), with the new Location's parent ID as an argument.
Creating and publishing
To actually create our Content in the Repository, we need to use ContentService::createContent(). This method expects a
ContentCreateStruct, as well as a
LocationCreateStruct. We have created both in the previous steps.
The LocationCreateStruct is provided as an array, since a Content can have multiple locations.
createContent() returns a new Content Value Object, with one version that has the DRAFT status. To make this Content visible, we need to publish it. This is done using
ContentService::publishVersion(). This method expects a
VersionInfo object as its parameter. In our case, we simply use the current version from
$draft, with the
We will now see how the previously created Content can be updated. To do so, we will create a new draft for our Content, update it using a
ContentUpdateStruct, and publish the updated Version.
To create our draft, we need to load the Content's ContentInfo using
ContentService::loadContentInfo(). We can then use
ContentService::createContentDraft() to add a new Draft to our Content.
To set the new values for this version, we request a
ContentUpdateStruct from the
ContentService using the
newContentUpdateStruct() method. Updating the values hasn't changed: we use the
We can now use
ContentService::updateContent() to apply our
ContentUpdateStruct to our draft's
VersionInfo. Publishing is done exactly the same way than for a new content, using
In the two previous examples, you have seen that we set the ContentUpdateStruct's initialLanguageCode property. To translate an object to a new language, set the locale to a new one.
It is possible to create or update content in multiple languages at once. There is one restriction: only one language can be set a version's language. This language is the one that will get a flag in the backoffice. However, you can set values in other languages for your attributes, using the setField method's third argument.
Since we don't specify a locale for the last two fields, they are set for the
Creating a content type
With this snipped a content type with two fields of type 'ezstring' is created. (Full code here).
Creating Content containing an image
This recipe shows how to create a content object containing an image. (Full code here)
Create Content with XML Text
This recipe shows how to create content with xml text. As a 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.
Creating a content type group
This snippet creates a content type group for a given identifier (Full code here).
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.