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

5. Other recipes

=20 =20

Assigning section to= content

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.

assign section to content
=20
$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.

assign section to content
=20
$contentInfo =3D $contentService->loadContentInfo( $contentId );=
=20

We use ContentService::loadContentInfo() to get the Content= we want to update the Section for.

assign section to content
=20
$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.

assign section to content
=20
$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 content type

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.

=20
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>

The next big part is to add FieldDefinition objects to our ContentType.<= /p>

=20
// 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.=
  • The fieldGroup is set to 'content'
  • Fields are ordered using the position property, ordered nu= merically in ascending order. We set it to an integer.
  • The translatable, required ans searchable boolean flags are set using t= heir respective property: 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().

=20
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().

------=_Part_4213_417191443.1485861829689--