Message-ID: <190981748.3024.1485851671111.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3023_1919815624.1485851671111" ------=_Part_3023_1919815624.1485851671111 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html 3. Managing Content

3. Managing Content

=20 =20

In the following recipes, you will see how to create Content, including = complex fields like XmlText or Image.

Identifying to the repository with a login and a password

As seen earlier, the Repository executes operations with a user's creden= tials. In a web context, the currently logged-in user is automatically iden= tified. In a command line context, you need to manually log a user in. We h= ave already seen how to manually load and set a user using its ID. If you w= ould like to identify a user using their username and password instead, thi= s can be achieved in the following way:

authentication
=20
$user =3D $userService->loadUserByCredentials( $user, $password =
);
$repository->setCurrentUser( $user );
=20

Creating content

Full code

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

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

We first need the required services. In this case: ContentService<= /code>, LocationService and ContentTypeService.

The ContentCreateStr= uct

As explained in the Publi= c API Basics, Value Objects are read only. Dedicated objects are provid= ed for Update and Create operations: structs, like ContentCreateStruc= t or UpdateCreateStruct. In this case, we need to use a= ContentCreateStruct

=20
$contentType =3D $contentTypeService->loadContentTypeByIdentifie=
r( 'article' );
$contentCreateStruct =3D $contentService->newContentCreateStruct( $conte=
ntType, 'eng-GB' );
=20

We first need to get the ContentType we want to c= reate a Content with. To do so, we use ContentTypeService::loadContentTypeByIdentifier() , w= ith the wanted ContentType identifier, like 'article'. We fina= lly get a ContentTypeCreateStruct using ContentSer= vice::newContentCreateStruct() , providing the Content Type and = a Locale Code (eng-GB).

Setting the fields v= alues

=20
$contentCreateStruct->setField( 'title', 'My title' );
$contentCreateStruct->setField( 'intro', $intro );
$contentCreateStruct->setField( 'body', $body );
=20

Using our create struct, we can now set the values for our Content item'= s Fields, using the setField() m= ethod. For now, we will just set the title. setField() for a T= extLine Field simply expects a string as input argument. More complex Field= Types, like Author or Image, expect different input values.

The ContentCreateStruct::setField() <= /code> method can take several type of arguments.

In any case, whatever the Field Type is, a Value of this type can be pro= vided. For instance, a TextLine\Value can be provided for a TextLine\Type. = Depending on the Field Type implementation itself, more specifically on the= fromHash() method every Field Type 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 instantiate a LocationCreateStruct . This is done with Location= Service::newLocationCreateStruct(), with the new Location's parent I= D as an argument.

=20
$locationCreateStruct =3D $locationService->newLocationCreateStr=
uct( 2 );
=20

Creating and publishi= ng

To actually create our Content in the Repository, we need to use C= ontentService::createContent(). This method expects a ContentC= reateStruct, as well as a LocationCreateStruct. We have= created both in the previous steps.

=20
$draft =3D $contentService->createContent( $contentCreateStruct,=
 array( $locationCreateStruct ) );
$content =3D $contentService->publishVersion( $draft->versionInfo );<=
/pre>=20

The LocationCreateStruct is provided as an array, since a Content item c= an 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 t= o publish it. This is done using ContentService::publishVersion(= ). This method expects a VersionInfo object as its para= meter. In our case, we simply use the current version from $draft, with the versionInfo property.

Updating Content

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 Versio= n.

=20
$contentInfo =3D $contentService->loadContentInfo( $contentId );
$contentDraft =3D $contentService->createContentDraft( $contentInfo );=20

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

=20
// instantiate a content update struct and set the new fields
$contentUpdateStruct =3D $contentService->newContentUpdateStruct();
$contentUpdateStruct->initialLanguageCode =3D 'eng-GB'; // set language =
for new version
$contentUpdateStruct->setField( 'title', $newTitle );
$contentUpdateStruct->setField( 'body', $newBody );
=20

To set the new values for this version, we request a ContentUpdate= Struct from the ContentService using the newConte= ntUpdateStruct() method. Updating the values hasn't changed: we use = the setField() method.

=20
$contentDraft =3D $contentService->updateContent( $contentDraft-=
>versionInfo, $contentUpdateStruct );
$content =3D $contentService->publishVersion( $contentDraft->versionI=
nfo );
=20

We can now use ContentService::updateContent() to apply our= ContentUpdateStruct to our draft's VersionInfo.&= nbsp;Publishing is done exactly the same way as for a new content, using ContentService::publishVersion().

Handling translations<= /h2>

In the two previous examples, you have seen that we set the ContentUpdat= eStruct's initialLanguageCode property. To translate an object= to a new language, set the locale to a new one.

translating
=20
$contentUpdateStruct->initialLanguageCode =3D 'ger-DE';
$contentUpdateStruct->setField( 'title', $newtitle );
$contentUpdateStruct->setField( 'body', $newbody );
=20

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 langua= ge. This language is the one that will get a flag in the back office. Howev= er, you can set values in other languages for your attributes, using the setField method's third argument.

update multiple languages
=20
// set one language for new version
$contentUpdateStruct->initialLanguageCode =3D 'fre-FR';

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

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

Since we don't specify a locale for the last two fields, they are set fo= r the UpdateStruct's initialLanguageCode, fre-FR.=

Creating C= ontent containing an image

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

We assume that we use the default image class. Creating our Content, usi= ng the 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.

=20
$file =3D '/path/to/image.png';

$value =3D new \eZ\Publish\Core\FieldType\Image\Value(
    array(
        'path' =3D> '/path/to/image.png',
        'fileSize' =3D> filesize( '/path/to/image.png' ),
        'fileName' =3D> basename( 'image.png' ),
        'alternativeText' =3D> 'My image'
    )
);
$contentCreateStruct->setField( 'image', $value );
=20

This time, we create our image by directly providing an Image\Value = object. The values are directly provided to the constructor using a hash wi= th predetermined keys that depend on each Type. In this case: the path wher= e the image can be found, its size, the file name, and an alternative text.=

Images also implement a static fromString() method= that will, given a path to an image, return an Image\Value ob= ject.

=20
$value =3D \eZ\Publish\Core\FieldType\Image\Value::fromString( '/pa=
th/to/image.png' );
=20

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

=20
$contentCreateStruct->setField( 'image', '/path/to/image.png' );
 
// or
 
$contentCreateStruct->setField( 'image', array(
    'path' =3D> '/path/to/image.png',
    'fileSize' =3D> filesize( '/path/to/image.png' ),
    'fileName' =3D> basename( 'image.png' ),
    'alternativeText' =3D> 'My image'
);
=20

Create Content wit= h XML Text

Another very commonly used Field Type is the rich text one, XmlTex= t.

working with xml text
=20
$xmlText =3D <<< EOX
<?xml version=3D'1.0' encoding=3D'utf-8'?>
<section>
<paragraph>This is a <strong>image test</strong></para=
graph>
<paragraph><embed view=3D'embed' size=3D'medium' object_id=3D'$ima=
geId'/></paragraph>
</section>
EOX;
$contentCreateStruct->setField( 'body', $xmlText );
=20

As for the last example above, we use the multiple formats accepted by <= code>setField(), and provide our XML string as is. The only accepted= format as of 5.0 is internal XML, the one stored in the Legacy database.

We embed an image in our XML, using the <embed> tag, = providing an image Content ID as the object_id attribute.

Using a custom format as input

More input formats will be added later. The API for that is actually alr= eady available: you simply need to implement the XmlText\Input interf= ace. It contains one method, getInternalRepresen= tation() , that must return an internal XML string. Create your = own bundle, add your implementation to it, and use it in your code!

=20
$input =3D new \My\XmlText\CustomInput( 'My custom format string' )=
;
$contentCreateStruct->setField( 'body', $input );
=20

Deleting Content

 

=20
$contentService->deleteContent( $contentInfo );
=20

ContentService::deleteContent() method exp= ects a ContentInfo as an argument. It will delete the given Co= ntent item, all of its Locations, as well as all of the Content item's Loca= tions' descendants and their associated Content. Use with caution!<= /strong>


------=_Part_3023_1919815624.1485851671111--