| description | Create, publish, update and translate content items by using the PHP API. |
|---|
!!! note
Creating most objects is impossible for an anonymous user.
Make sure to [authenticate](php_api.md#setting-the-repository-user) as a user with sufficient permissions.
!!! tip "Content REST API"
To learn how to create content items using the REST API, see [REST API reference](/api/rest_api/rest_api_reference/rest_api_reference.html#tag/Objects/operation/api_contentobjects_post).
Value objects such as content items are read-only, so to create or modify them you need to use structs.
ContentService::newContentCreateStruct
returns a new ContentCreateStruct object.
[[= include_file('code_samples/api/public_php_api/src/Command/CreateContentCommand.php', 51, 60) =]]This command creates a draft using ContentService::createContent (line 6).
This method must receive a ContentCreateStruct and an array of location structs.
ContentCreateStruct (which extends ContentStruct) is created through ContentService::newContentCreateStruct (line 1),
which receives the content type and the primary language for the content item.
For information about translating a content item into other languages, see Translating content.
ContentStruct::setField (line 2) enables you to define the field values.
When the field accepts a simple value, you can provide it directly, as in the example above.
For some field types, for example images, you need to provide an instance of a Value type.
Image field type requires an instance of its Value type, which you must provide to the ContentStruct::setField method.
Therefore, when creating a content item of the Image type (or any other content type with an image field type),
the ContentCreateStruct is slightly more complex than in the previous example:
[[= include_file('code_samples/api/public_php_api/src/Command/CreateImageCommand.php', 51, 63) =]]Value of the Image field type contains the path to the image file and other basic information based on the input file.
The RichText field accepts values in a custom flavor of DocBook format. For example, to add a RichText paragraph, provide the following as input:
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ibexa.co/xmlns/dxp/docbook/xhtml" xmlns:ezcustom="http://ibexa.co/xmlns/dxp/docbook/custom" version="5.0-variant ezpublish-1.0"><para>Description of your content item.</para></section>To learn more about the format and how it represents different elements of rich text, see RichText field type reference.
ContentService::createContent creates a content item with only one draft version.
To publish it, use ContentService::publishVersion.
This method must get the VersionInfo object of a draft version.
[[= include_file('code_samples/api/public_php_api/src/Command/CreateContentCommand.php', 62, 63) =]]To update an existing content item, you need to prepare a ContentUpdateStruct
and pass it to ContentService::updateContent.
This method works on a draft, so to publish your changes you need to use ContentService::publishVersion as well:
[[= include_file('code_samples/api/public_php_api/src/Command/UpdateContentCommand.php', 44, 53) =]]Content translations are created per version. By default every version contains all existing translations.
To translate a content item to a new language, you need to update it and provide a new initialLanguageCode:
[[= include_file('code_samples/api/public_php_api/src/Command/TranslateContentCommand.php', 49, 55) =]]
[[= include_file('code_samples/api/public_php_api/src/Command/TranslateContentCommand.php', 60, 62) =]]You can also update content in multiple languages at once using the setField method's third argument.
Only one language can still be set as a version's initial language:
[[= include_file('code_samples/api/public_php_api/src/Command/TranslateContentCommand.php', 57, 58) =]]You can delete a single translation from a content item's version using ContentService::deleteTranslationFromDraft.
The method must be provided with a VersionInfo object and the code of the language to delete:
$this->contentService->deleteTranslationFromDraft($versionInfo, $language);