Message-ID: <2116141717.3692.1485854849332.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3691_612058650.1485854849332" ------=_Part_3691_612058650.1485854849332 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
In the following recipes, you will see how to create Content, including = complex fields like XmlText or Image.
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:
$user =3D $userService->loadUserByCredentials( $user, $password = ); $repository->setCurrentUser( $user );=20
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.
/** @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: As explained in eZ Platform Public PHP API, Value Objects are read only. Dedicated ob=
jects are provided for Update and Create operations: structs, like We first need to get the Using our create struct, we can now set the values for our Content item'=
s Fields, using the The 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=
In order to set a Location for our object, we must instantiate a To actually create our Content in the Repository, we need to use The LocationCreateStruct is provided as an array, since a Content item c=
an have multiple locations. Full code 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 To create our draft, we need to load the Content item's ContentInfo usin=
g To set the new values for this version, we request a We can now use ContentService<=
/code>,
.LocationService
and ContentTypeService
.=
p>
The ContentCreateStruct=
h3>
Co=
ntentCreateStruct
or UpdateCreateStruct
. In this case, =
we need to use a ContentCreateStruct
. $contentType =3D $contentTypeService->loadContentTypeByIdentifie=
r( 'article' );
$contentCreateStruct =3D $contentService->newContentCreateStruct( $conte=
ntType, 'eng-GB' );
=20
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 values=
$contentCreateStruct->setField( 'title', 'My title' );
$contentCreateStruct->setField( 'intro', $intro );
$contentCreateStruct->setField( 'body', $body );
=20
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. ContentCreateStruct::setField() <=
/code> method can take several type of arguments.
fromHash()
method every Field Type implements, various arrays=
can be accepted, as well as primitive types, depending on the Type.Setting the Location
LocationCreateStruct
. This is done with Location=
Service::newLocationCreateStruct()
, with the new Location's parent I=
D as an argument.$locationCreateStruct =3D $locationService->newLocationCreateStr=
uct( 2 );
=20
Creating and publishing
C=
ontentService::createContent()
. This method expects a ContentC=
reateStruct
, as well as a LocationCreateStruct
. We have=
created both in the previous steps.$draft =3D $contentService->createContent( $contentCreateStruct,=
array( $locationCreateStruct ) );
$content =3D $contentService->publishVersion( $draft->versionInfo );<=
/pre>=20
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
ContentUpdateStruct
, and publish the updated Versio=
n.$contentInfo =3D $contentService->loadContentInfo( $contentId );
$contentDraft =3D $contentService->createContentDraft( $contentInfo );=
pre>=20
ContentService::loadContentInfo()
. We can then use Con=
tentService::createContentDraft()
to add a new Draft to our Content.=
// 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
ContentUpdate=
Struct
from the ContentService
using the newConte=
ntUpdateStruct()
method. Updating the values hasn't changed: we use =
the setField()
method.$contentDraft =3D $contentService->updateContent( $contentDraft-=
>versionInfo, $contentUpdateStruct );
$content =3D $contentService->publishVersion( $contentDraft->versionI=
nfo );
=20
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
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.
$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
// 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.=
Full code
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.
$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.
$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.=
$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
Full code
Another very commonly used Field Type is the rich text one, XmlTex=
t
.
$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.= p>
The XSD for the internal XML representation can be found in the kernel: = https://github.com/ezsystems/ezpublish-kernel/= blob/master/eZ/Publish/Core/FieldType/XmlText/Input/Resources/schemas/ezxml= .xsd.
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!
$input =3D new \My\XmlText\CustomInput( 'My custom format string' )= ; $contentCreateStruct->setField( 'body', $input );=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.