Message-ID: <405429564.3092.1485851974567.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3091_744086852.1485851974567" ------=_Part_3091_744086852.1485851974567 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Full code
The Section a Content belongs to can be set during creation, using the <=
code>ContentCreateStruct::$sectionId property. However, as for many =
Repository objects properties, the section can't be changed using a C=
ontentUpdateStruct
. The reason is still the same: changing a Content=
's section will affect the subtrees referenced by its Locations. For this r=
eason, it is required that you use the SectionService to change the Section=
of a Content.
$contentInfo =3D $contentService->loadContentInfo( $contentId ); $section =3D $sectionService->loadSection( $sectionId ); $sectionService->assignSection( $contentInfo, $section );=20
This operation involves the SectionService
, as well as the =
ContentService
.
$contentInfo =3D $contentService->loadContentInfo( $contentId );==20
We use ContentService::loadContentInfo()
to get the Content=
we want to update the Section for.
$section =3D $sectionService->loadSection( $sectionId );= =20
SectionService::loadSection() is then used to load the Section we want t=
o assign our Content to. Note that there is no SectionInfo object. Sections=
are quite simple, and we don't need to separate their metadata from their =
actual data. However, SectionCreateStruct
and SectionUpdateStruct
objects must still be used to crea=
te and update sections.
$sectionService->assignSection( $contentInfo, $section );= =20
The actual update operation is done using SectionService::assignSection(= ), with the ContentInfo and the Section as arguments.
SectionService::assignSection() won't return the updated Content, as it =
has no knowledge of those objects. To get the Content with the newly assign=
ed Location, you need to reload the ContentInfo using Cont=
entService::loadContentInfo()
. This is also valid for descendant=
s of Content. If you have any stored in your execution state, you need to r=
eload them. Otherwise you would be using outdated Content data.
Creating a ContentType
is actually almost more comp=
lex than creating Content. It really isn't as common, and didn't "deserve" =
the same kind of API as Content did.
Let's split the code in three major parts.
try { $contentTypeGroup =3D $contentTypeService->loadContentTypeGroupByIde= ntifier( 'content' ); } catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) { $output->writeln( "content type group with identifier $groupIdentifi= er not found" ); return; } $contentTypeCreateStruct =3D $contentTypeService->newContentTypeCreateSt= ruct( 'mycontenttype' ); $contentTypeCreateStruct->mainLanguageCode =3D 'eng-GB'; $contentTypeCreateStruct->nameSchema =3D '<title>'; $contentTypeCreateStruct->names =3D array( 'eng-GB' =3D> 'My content type' ); $contentTypeCreateStruct->descriptions =3D array( 'eng-GB' =3D> 'Description for my content type', );=20
First, we need to load the ContentTypeGroup
our Conte=
ntType
will be created in. We do this using ContentTypeService::loadContentTypeGroupByIdentifier()
=
, which gives us back a ContentTypeGroup
obje=
ct. As for content, we then request a ContentTypeCreateStruct
=
from the ContentTypeService
, using ContentTypeService::newContentTypeCreateStruct()
, with the =
desired identifier as the argument.
Using the create struct's properties, we can set the type's properties:<= /p>
mainLanguageCode
) for the type is set t=
o eng-GB,nameSchema
) is set to=
'<title>': content of this type will be named as their 'title' field=
.names
property. We give it a hash, indexed by the locale ('eng-GB') the name is=
set in. This locale must exist in the system.names
property, we can s=
et human readable descriptions, again as hashes indexed by locale code.The next big part is to add FieldDefinition objects to our ContentType.<= /p>
// add a TextLine Field with identifier 'title' $titleFieldCreateStruct =3D $contentTypeService->newFieldDefinitionCreat= eStruct( 'title', 'ezstring' ); $titleFieldCreateStruct->names =3D array( 'eng-GB' =3D> 'Title' ); $titleFieldCreateStruct->descriptions =3D array( 'eng-GB' =3D> 'The T= itle' ); $titleFieldCreateStruct->fieldGroup =3D 'content'; $titleFieldCreateStruct->position =3D 10; $titleFieldCreateStruct->isTranslatable =3D true; $titleFieldCreateStruct->isRequired =3D true; $titleFieldCreateStruct->isSearchable =3D true; $contentTypeCreateStruct->addFieldDefinition( $titleFieldCreateStruct ); // add a TextLine Field body field $bodyFieldCreateStruct =3D $contentTypeService->newFieldDefinitionCreate= Struct( 'body', 'ezstring' ); $bodyFieldCreateStruct->names =3D array( 'eng-GB' =3D> 'Body' ); $bodyFieldCreateStruct->descriptions =3D array( 'eng-GB' =3D> 'Descri= ption for Body' ); $bodyFieldCreateStruct->fieldGroup =3D 'content'; $bodyFieldCreateStruct->position =3D 20; $bodyFieldCreateStruct->isTranslatable =3D true; $bodyFieldCreateStruct->isRequired =3D true; $bodyFieldCreateStruct->isSearchable =3D true; $contentTypeCreateStruct->addFieldDefinition( $bodyFieldCreateStruct );<= /pre>=20
We need to create a FieldDefinitionCreateStruct
object for =
each FieldDefinition
our ContentType
will be made=
of. Those objects are obtained using C=
ontentTypeService::newFieldDefinitionCreateStruct()
. This method=
expects the FieldDefinition identifier and its type as arguments. The iden=
tifiers match the ones from eZ Publish 4 (ezstring
for TextLin=
e, etc).
Each field's properties are set using the create struct's properties:
names
and descriptions
are set using hashes i=
ndexed by the locale code, and with the name or description as an argument.=
fieldGroup
is set to 'content'position
property, ordered nu=
merically in ascending order. We set it to an integer.isTranslatable
, isRequired and isSearchable
.
Once the properties for each create struct are set, the field is added t=
o the ContentType create struct using ContentTypeCreateStruct::addFieldDefinition()
.
try { $contentTypeDraft =3D $contentTypeService->createContentType( $conte= ntTypeCreateStruct, array( $contentTypeGroup ) ); $contentTypeService->publishContentTypeDraft( $contentTypeDraft ); } catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) { $output->writeln( "<error>" . $e->getMessage() . "</erro= r>" ); } catch ( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e ) { $output->writeln( "<error>" . $e->getMessage() . "</erro= r>" ); }=20
The last step is the same as for Content: we create a content type draft=
using ContentTypeService::createContentType()
, with the ContentTypeCreateStruct
and an array of ContentTypeGroup objects are arguments. We then publish the ContentType draft using
ContentTypeService::publishContentTypeDraft()
.