General

  eZ Systems Website
  Technical documentation
  Editor documentation

This Documentation contains:
 
Technical documentation:



⚠ WARNING ! This documentation is deprecated !

Please go to the current Technical Documentation

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 like to identify a user using his their username and password instead, this can be achieved as follows.in the following way:

Code Block
languagephp
titleauthentication
$user = $userService->loadUserByCredentials( $user, $password );
$repository->setCurrentUser( $user );

...

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 PublishPlatform.

Code Block
languagephp
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();

...

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

Code Block
languagephp
$contentType = $contentTypeService->loadContentTypeByIdentifier( 'article' );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' );

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 Content Type and a Locale Code (eng-GB).

...

Using our create struct, we can now set the values for our Content item's fieldsFields, 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 FieldTypesField Types, like Author or Image, expect different input values.

Note

The ContentCreateStruct::setField() method can take several type of arguments.

In any case, whatever the FieldType Field Type 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 Field Type implementation itself, more specifically on the fromHash() method every FieldType Field Type implements, various arrays can be accepted, as well as primitive types, depending on the Type.

...

In order to set a Location for our object, we must instantiate a LocationCreateStruct . This is done with LocationService::newLocationCreateStruct(), with the new Location's parent ID as an argument.

...

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 item can have multiple locations.

...

To create our draft, we need to load the Content item's ContentInfo using ContentService::loadContentInfo(). We can then use ContentService::createContentDraft() to add a new Draft to our Content.

...

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 back office. However, you can set values in other languages for your attributes, using the setField method's third argument.

Code Block
languagephp
titleupdate multiple languages
// set one language for new version
$contentUpdateStruct->initialLanguageCode = 'fre-FR';

$contentUpdateStruct->setField( 'title', $newgermantitle, 'ger-DE' );
$contentUpdateStruct->setField( 'body', $newgermanbody, 'ger-DE' );

$contentUpdateStruct->setField( 'title', $newfrenchtitle );
$contentUpdateStruct->setField( 'body', $newfrenchbody );

...

Info
titleFull code

https://github.com/ezsystems/CookbookBundle/blob/master/Command/CreateImageCommand.php

As explained above, the setField() method can accept various values: an instance of the FieldTypeField Type's Value class, a primitive type, or a hash. The last two depend on what the Type::acceptValue() method is build up to handle. TextLine can, for instance, accept a simple string as an input value. In this example, you will see how to set an Image value.

We assume that we use the default image class. Creating our Content, using the ContentType Content Type and a ContentCreateStruct, has been covered above, and can be found in the full code. Let's focus on how the image is provided.

...

But as said before, whatever you provide setField() with is sent to the acceptValue() method. This method really is the entry point to the input formats a FieldType Field Type accepts. In this case, you could have provided setField with either a hash, similar to the one we provided the Image\Value constructor with, or the path to your image, as a string.

...

Another very commonly used FieldType Field Type is the rich text one, XmlText.

...

ContentService::deleteContent() method expects a ContentInfo as an argument. It will delete the given Content item, all of its Locations, as well as all of the Content item's Locations' descendants and their associated Content. Use with caution caution!