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

Other recipes

=20
=20
=20
=20

Assigning Section to cont= ent

The Section that a Content item belongs to can be set during creation, u= sing the ContentCreateStruct::$sectionId property. However, as= for many Repository objects properties, the Section can't be changed using= a ContentUpdateStruct. The reason is still the same: changing= a Content item's Section will affect the subtrees referenced by its Locati= ons. For this reason, it is required that you use the SectionService to cha= nge the Section of a Content item.

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 Sect= ion we want to assign our Content to. Note that there is no SectionIn= fo object. Sections are quite simple, and we don't need to separate = their metadata from their actual data. However, SectionCreate= Struct and SectionUpdateStruct objects = must still be used to create and update Sections.

assign section to content
=20
$sectionService->assignSection( $contentInfo, $section );
= =20

The actual update operation is done using SectionService::assignSe= ction(), with the ContentInfo and the Section as argume= nts.

SectionService::assignSection() won't return the updated Co= ntent, as it has no knowledge of those objects. To get the Content with the= newly assigned Location, you need to reload the ContentInfo u= sing ContentService::loadContentInfo(). This is= also valid for descendants of the Content item. If you have any stored in = your execution state, you need to reload 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 main language (mainLanguageCode) for the Type is set t= o eng-GB,
  • the content name generation pattern (nameSchema) is set to= '<title>': Content items of this type will be named the same as thei= r 'title' field.
  • the human-readable name for our Type is set using the 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.
  • the same way that we have set the 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 Content Type.=

=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 and 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 Content Type 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 Content Type draft using ContentTypeService::publishContentTypeDraft().

 

=20
=20
=20
=20

In this topic:

=20
=20
=20
------=_Part_3695_1124599950.1485854858350--